alpreu commented on a change in pull request #18153: URL: https://github.com/apache/flink/pull/18153#discussion_r803632122
########## File path: flink-connectors/flink-connector-elasticsearch7/src/main/java/org/apache/flink/connector/elasticsearch/source/reader/Elasticsearch7SplitReader.java ########## @@ -0,0 +1,238 @@ +/* + * 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.flink.connector.elasticsearch.source.reader; + +import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds; +import org.apache.flink.connector.base.source.reader.splitreader.SplitReader; +import org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition; +import org.apache.flink.connector.base.source.reader.splitreader.SplitsChange; +import org.apache.flink.connector.elasticsearch.common.NetworkClientConfig; +import org.apache.flink.connector.elasticsearch.source.Elasticsearch7SourceConfiguration; +import org.apache.flink.connector.elasticsearch.source.split.Elasticsearch7Split; +import org.apache.flink.util.Collector; + +import org.elasticsearch.search.SearchHit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Queue; +import java.util.Set; + +/** + * A {@link SplitReader} implementation that reads {@link Elasticsearch7Record}s from {@link + * Elasticsearch7Split}s. + * + * @param <T> the type of the record to be emitted from the Source. + */ +public class Elasticsearch7SplitReader<T> + implements SplitReader<Elasticsearch7Record<T>, Elasticsearch7Split> { + private static final Logger LOG = LoggerFactory.getLogger(Elasticsearch7SplitReader.class); + private final Elasticsearch7SourceConfiguration sourceConfiguration; + private final NetworkClientConfig networkClientConfig; + private final Queue<Elasticsearch7Split> splits; + + @Nullable private Elasticsearch7SearchHitReader currentReader; + @Nullable private String currentSplitId; + + private final Elasticsearch7SearchHitDeserializationSchema<T> deserializationSchema; + private final SimpleCollector<T> collector; + + public Elasticsearch7SplitReader( + Elasticsearch7SourceConfiguration sourceConfiguration, + NetworkClientConfig networkClientConfig, + Elasticsearch7SearchHitDeserializationSchema<T> deserializationSchema) { + this.sourceConfiguration = sourceConfiguration; + this.networkClientConfig = networkClientConfig; + this.deserializationSchema = deserializationSchema; + this.splits = new ArrayDeque<>(); + this.collector = new SimpleCollector<>(); + } + + private RecordsWithSplitIds<Elasticsearch7Record<T>> createRecordsFromSearchResults( + Collection<SearchHit> searchHits) throws IOException { + Collection<Elasticsearch7Record<T>> recordsForSplit = new ArrayList<>(); + for (SearchHit hit : searchHits) { + try { + deserializationSchema.deserialize(hit, collector); + collector + .getRecords() + .forEach(r -> recordsForSplit.add(new Elasticsearch7Record<>(r))); + } catch (Exception e) { + throw new IOException("Failed to deserialize consumer record due to", e); + } finally { + collector.reset(); + } + } + + return ElasticsearchSplitRecords.forRecords(currentSplitId, recordsForSplit); + } + + @Override + public RecordsWithSplitIds<Elasticsearch7Record<T>> fetch() throws IOException { Review comment: Thank you for the clarification, I'll separate it :) -- 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]
