Jiabao-Sun commented on code in PR #1: URL: https://github.com/apache/flink-connector-mongodb/pull/1#discussion_r1032955663
########## flink-connector-mongodb/src/main/java/org/apache/flink/connector/mongodb/source/reader/split/MongoScanSourceSplitReader.java: ########## @@ -0,0 +1,201 @@ +/* + * 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.mongodb.source.reader.split; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.connector.source.SourceReaderContext; +import org.apache.flink.connector.base.source.reader.RecordsBySplits; +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.mongodb.common.config.MongoConnectionOptions; +import org.apache.flink.connector.mongodb.source.config.MongoReadOptions; +import org.apache.flink.connector.mongodb.source.split.MongoScanSourceSplit; +import org.apache.flink.connector.mongodb.source.split.MongoSourceSplit; +import org.apache.flink.util.CollectionUtil; + +import com.mongodb.MongoException; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCursor; +import org.bson.BsonDocument; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.List; + +import static org.apache.flink.connector.mongodb.common.utils.MongoUtils.project; + +/** An split reader implements {@link SplitReader} for {@link MongoScanSourceSplit}. */ +@Internal +public class MongoScanSourceSplitReader implements MongoSourceSplitReader<MongoSourceSplit> { + + private static final Logger LOG = LoggerFactory.getLogger(MongoScanSourceSplitReader.class); + + private final MongoConnectionOptions connectionOptions; + private final MongoReadOptions readOptions; + private final SourceReaderContext readerContext; + @Nullable private final List<String> projectedFields; + private final int limit; + + private boolean closed = false; + private boolean finished = false; + private MongoClient mongoClient; + private MongoCursor<BsonDocument> currentCursor; + private MongoScanSourceSplit currentSplit; + + public MongoScanSourceSplitReader( + MongoConnectionOptions connectionOptions, + MongoReadOptions readOptions, + @Nullable List<String> projectedFields, + int limit, + SourceReaderContext context) { + this.connectionOptions = connectionOptions; + this.readOptions = readOptions; + this.projectedFields = projectedFields; + this.limit = limit; + this.readerContext = context; + } + + @Override + public RecordsWithSplitIds<BsonDocument> fetch() throws IOException { + if (closed) { + throw new IllegalStateException("Cannot fetch records from a closed split reader"); + } + + RecordsBySplits.Builder<BsonDocument> builder = new RecordsBySplits.Builder<>(); + + // Return when no split registered to this reader. + if (currentSplit == null) { + return builder.build(); + } + + currentCursor = getOrCreateCursor(); + int fetchSize = readOptions.getFetchSize(); + + try { + for (int recordNum = 0; recordNum < fetchSize; recordNum++) { + if (currentCursor.hasNext()) { + builder.add(currentSplit, currentCursor.next()); + } else { + builder.addFinishedSplit(currentSplit.splitId()); + finished = true; + break; + } + } Review Comment: We use a [cursor](https://www.mongodb.com/docs/manual/reference/method/cursor.batchSize/#mongodb-method-cursor.batchSize) to request a batch of data from mongodb, the size of the batch depends on the configuration of `scan.cursor.batch-size`. No request will be made to mongodb until a batch of data in the cursor has been processed. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org