imay commented on a change in pull request #800: Feature doris on es URL: https://github.com/apache/incubator-doris/pull/800#discussion_r268481858
########## File path: be/src/util/es_scan_reader.cpp ########## @@ -0,0 +1,208 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <string> +#include <sstream> +#include "es_scan_reader.h" +#include "rapidjson/writer.h" +#include "rapidjson/document.h" +#include "rapidjson/stringbuffer.h" +#include "common/logging.h" +#include "common/status.h" +#include <map> + +namespace doris { +const std::string REUQEST_SCROLL_FILTER_PATH = "filter_path=_scroll_id,hits.hits._source,hits.total,_id,hits.hits._source.fields"; +const std::string REQUEST_SCROLL_PATH = "_scroll"; +const std::string REQUEST_PREFERENCE_PREFIX = "&preference=shards:"; +const std::string REQUEST_SEARCH_SCROLL_PATH = "/_search/scroll"; +const std::string REQUEST_SEPARATOR = "/"; +const std::string REQUEST_SCROLL_TIME = "5m"; + +const char* FIELD_SCROLL_ID = "_scroll_id"; +const char* FIELD_HITS = "hits"; +const char* FIELD_INNER_HITS = "hits"; +const char* FIELD_SOURCE = "_source"; +const char* FIELD_TOTAL = "total"; + +ESScanReader::ESScanReader(const std::string& target, uint16_t size,std::map<std::string, std::string>& props) { + LOG(INFO) << "ESScanReader "; + _target = target; + _batch_size = size; + _index = props.at(KEY_INDEX); + _type = props.at(KEY_TYPE); + if (props.find(KEY_USER_NAME) != props.end()) { + _user_name = props.at(KEY_USER_NAME); + } + if (props.find(KEY_PASS_WORD) != props.end()){ + _passwd = props.at(KEY_PASS_WORD); + } + if (props.find(KEY_SHARDS) != props.end()) { + _shards = props.at(KEY_SHARDS); + } + if (props.find(KEY_QUERY) != props.end()) { + _query = props.at(KEY_QUERY); + } + _init_scroll_url = _target + REQUEST_SEPARATOR + _index + REQUEST_SEPARATOR + _type + "/_search?scroll=" + REQUEST_SCROLL_TIME + REQUEST_PREFERENCE_PREFIX + _shards + "&" + REUQEST_SCROLL_FILTER_PATH; + _next_scroll_url = _target + REQUEST_SEARCH_SCROLL_PATH + "?" + REUQEST_SCROLL_FILTER_PATH; + _eos = false; +} + +ESScanReader::~ESScanReader() { + if (_cached_response != nullptr) { + free(_cached_response); + _cached_response = nullptr; + } +} + +Status ESScanReader::open() { + _is_first = true; + RETURN_IF_ERROR(_network_client.init(_init_scroll_url)); + _network_client.set_basic_auth(_user_name, _passwd); + _network_client.set_content_type("application/json"); + // phase open, we cached the first response for `get_next` phase + _network_client.execute_post_request(_query, _cached_response); + long status = _network_client.get_http_status(); + if (status != 200) { + LOG(WARNING) << "invalid response http status for open: " << status; + return Status(*_cached_response); + } + VLOG(1) << "open _cached response: " << *_cached_response; + rapidjson::Document document_node; Review comment: You'd better to write a EsResultParser to parse json, and it is easy to do unit test for this parser ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
