odbozhou commented on code in PR #347: URL: https://github.com/apache/rocketmq-connect/pull/347#discussion_r1005277182
########## connectors/rocketmq-connect-elasticsearch/src/main/java/org/apache/rocketmq/connect/elasticsearch/replicator/source/ElasticsearchQuery.java: ########## @@ -0,0 +1,172 @@ +/* + * 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.rocketmq.connect.elasticsearch.replicator.source; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import io.openmessaging.connector.api.data.RecordOffset; +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; +import org.apache.http.HttpHost; +import org.apache.http.client.methods.HttpGet; +import org.apache.rocketmq.connect.elasticsearch.config.ElasticsearchConfig; +import org.apache.rocketmq.connect.elasticsearch.config.ElasticsearchConstant; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchScrollRequest; +import org.elasticsearch.client.Node; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.RangeQueryBuilder; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ElasticsearchQuery { + + private Logger logger = LoggerFactory.getLogger(this.getClass()); + + private ElasticsearchReplicator replicator; + + private ElasticsearchConfig config; + + private RestHighLevelClient client; + + private RestClient restClient; + + private ExecutorService executorService = Executors.newFixedThreadPool(5); + + public ElasticsearchQuery(ElasticsearchReplicator replicator) { + this.replicator = replicator; + this.config = replicator.getConfig(); + } + + public void start(RecordOffset recordOffset) { + HttpHost httpHost = new HttpHost(config.getElasticsearchHost(), config.getElasticsearchPort()); + Node node = new Node(httpHost); + RestClientBuilder restClientBuilder = RestClient.builder(node); + restClient = restClientBuilder.build(); + client = new RestHighLevelClient(restClientBuilder); + final JSONObject jsonObject = JSON.parseObject(config.getIndex()); + for (Map.Entry<String, Object> entry : jsonObject.entrySet()) { + final String indexName = entry.getKey(); + final JSONObject value = (JSONObject) entry.getValue(); + for (Map.Entry<String, Object> field : value.entrySet()) { + final String fieldName = field.getKey(); + final Object fieldValue = field.getValue(); + config.getIndexMap().put(indexName, fieldName); + try { + Request request = new Request(HttpGet.METHOD_NAME, "/" + indexName + "/_search_shards"); + final Response response = restClient.performRequest(request); + final InputStream content = response.getEntity().getContent(); + byte arr[] = new byte[1024]; + int len = content.read(arr); + String shardStr = new String(arr, 0, len); + final JSONObject shardJsonObject = JSON.parseObject(shardStr); + final List<Map.Entry<String, Object>> shards = shardJsonObject.entrySet().stream().filter(shardEntry -> shardEntry.getKey().equals("shards")).collect(Collectors.toList()); + final JSONArray shardArray = (JSONArray) shards.get(0).getValue(); + final Iterator<Object> iterator = shardArray.iterator(); + while (iterator.hasNext()) { + final Object next = iterator.next(); + final JSONObject object = (JSONObject) ((JSONArray) next).get(0); + if (object.getBoolean("primary")) { + final Integer shard = object.getInteger("shard"); + executorService.execute(() -> { Review Comment: Consider using taskConfigs of the connector to generate multiple TaskConfigs, so that runtime will depend on taskconfig creates multiple tasks and improves task parallelism without requiring the connector implementation to deal with multiple threads. Refer to the implementation of the jdbc source connector ########## connectors/rocketmq-connect-elasticsearch/src/main/java/org/apache/rocketmq/connect/elasticsearch/replicator/source/ElasticsearchQuery.java: ########## @@ -0,0 +1,172 @@ +/* + * 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.rocketmq.connect.elasticsearch.replicator.source; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import io.openmessaging.connector.api.data.RecordOffset; +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.Collectors; +import org.apache.http.HttpHost; +import org.apache.http.client.methods.HttpGet; +import org.apache.rocketmq.connect.elasticsearch.config.ElasticsearchConfig; +import org.apache.rocketmq.connect.elasticsearch.config.ElasticsearchConstant; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchScrollRequest; +import org.elasticsearch.client.Node; +import org.elasticsearch.client.Request; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.RangeQueryBuilder; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ElasticsearchQuery { + + private Logger logger = LoggerFactory.getLogger(this.getClass()); + + private ElasticsearchReplicator replicator; + + private ElasticsearchConfig config; + + private RestHighLevelClient client; + + private RestClient restClient; + + private ExecutorService executorService = Executors.newFixedThreadPool(5); + + public ElasticsearchQuery(ElasticsearchReplicator replicator) { + this.replicator = replicator; + this.config = replicator.getConfig(); + } + + public void start(RecordOffset recordOffset) { Review Comment: This method is too long, consider refactoring, extract method is easy to read and maintain -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
