wuyunfeng commented on a change in pull request #3454: URL: https://github.com/apache/incubator-doris/pull/3454#discussion_r439274920
########## File path: fe/src/main/java/org/apache/doris/external/elasticsearch/EsFieldInfo.java ########## @@ -0,0 +1,47 @@ +// 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. + +package org.apache.doris.external.elasticsearch; + +import java.util.Map; + +/** + * EsFieldInfo + * + * @author stalary + * @since 2020/06/10 + */ +public class EsFieldInfo { + + private Map<String, String> fetchFields; + + private Map<String, String> docValueFields; + + public EsFieldInfo(Map<String, String> fetchFields, Map<String, String> docValueFields) { + this.fetchFields = fetchFields; + this.docValueFields = docValueFields; + } + + public Map<String, String> getFetchFields() { + return fetchFields; + } + + public Map<String, String> getDocValueFields() { + return docValueFields; + } + +} Review comment: Add new line for this file ########## File path: fe/src/main/java/org/apache/doris/external/elasticsearch/EsRestClient.java ########## @@ -17,92 +17,183 @@ package org.apache.doris.external.elasticsearch; +import org.apache.doris.catalog.Column; +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.Lists; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHeaders; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.util.Strings; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig; - +import org.json.JSONArray; +import org.json.JSONObject; import java.io.IOException; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; - import okhttp3.Credentials; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class EsRestClient { + private static final Logger LOG = LogManager.getLogger(EsRestClient.class); private ObjectMapper mapper; - + { mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.USE_ANNOTATIONS, false); mapper.configure(SerializationConfig.Feature.USE_ANNOTATIONS, false); } - + private static OkHttpClient networkClient = new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS) .build(); - - private String basicAuth; - - private int nextClient = 0; + + private Request.Builder builder; private String[] nodes; private String currentNode; - + private int currentNodeIndex = 0; + public EsRestClient(String[] nodes, String authUser, String authPassword) { this.nodes = nodes; + this.builder = new Request.Builder(); if (!Strings.isEmpty(authUser) && !Strings.isEmpty(authPassword)) { - basicAuth = Credentials.basic(authUser, authPassword); + this.builder.addHeader(HttpHeaders.AUTHORIZATION, + Credentials.basic(authUser, authPassword)); } - selectNextNode(); + this.currentNode = nodes[currentNodeIndex]; } - - private boolean selectNextNode() { - if (nextClient >= nodes.length) { - return false; + + private void selectNextNode() { + currentNodeIndex++; + // reroute, because the previously failed node may have already been restored + if (currentNodeIndex >= nodes.length) { + currentNodeIndex = 0; } - currentNode = nodes[nextClient++]; - return true; + currentNode = nodes[currentNodeIndex]; } - + public Map<String, EsNodeInfo> getHttpNodes() throws Exception { Map<String, Map<String, Object>> nodesData = get("_nodes/http", "nodes"); if (nodesData == null) { return Collections.emptyMap(); } - Map<String, EsNodeInfo> nodes = new HashMap<>(); + Map<String, EsNodeInfo> nodesMap = new HashMap<>(); for (Map.Entry<String, Map<String, Object>> entry : nodesData.entrySet()) { EsNodeInfo node = new EsNodeInfo(entry.getKey(), entry.getValue()); if (node.hasHttp()) { - nodes.put(node.getId(), node); + nodesMap.put(node.getId(), node); } } - return nodes; + return nodesMap; } - - public String getIndexMetaData(String indexName) throws Exception { - String path = "_cluster/state?indices=" + indexName - + "&metric=routing_table,nodes,metadata&expand_wildcards=open"; - return execute(path); - + + public EsFieldInfo getFieldInfo(String indexName, String mappingType, List<Column> colList) throws Exception { Review comment: these method can be put into EsFieldInfo ########## File path: fe/src/main/java/org/apache/doris/external/elasticsearch/EsFieldInfo.java ########## @@ -0,0 +1,47 @@ +// 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. + +package org.apache.doris.external.elasticsearch; + +import java.util.Map; + +/** + * EsFieldInfo + * + * @author stalary + * @since 2020/06/10 + */ +public class EsFieldInfo { + + private Map<String, String> fetchFields; + + private Map<String, String> docValueFields; + + public EsFieldInfo(Map<String, String> fetchFields, Map<String, String> docValueFields) { + this.fetchFields = fetchFields; + this.docValueFields = docValueFields; + } + + public Map<String, String> getFetchFields() { + return fetchFields; + } + + public Map<String, String> getDocValueFields() { + return docValueFields; + } + Review comment: Prefer add static `parseXXX` or `fromXXXX` for this plain object to parse from response ---------------------------------------------------------------- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
