TyrantLucifer commented on code in PR #2596:
URL: 
https://github.com/apache/incubator-seatunnel/pull/2596#discussion_r962158458


##########
seatunnel-e2e/seatunnel-flink-connector-v2-e2e/src/test/java/org/apache/seatunnel/e2e/flink/v2/mongodb/MongodbSourceToConsoleIT.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.seatunnel.e2e.flink.v2.mongodb;
+
+import static org.testcontainers.shaded.org.awaitility.Awaitility.given;
+import static java.net.HttpURLConnection.HTTP_OK;
+import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
+
+import org.apache.seatunnel.e2e.flink.FlinkContainer;
+
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoClients;
+import com.mongodb.client.MongoCollection;
+import lombok.extern.slf4j.Slf4j;
+import org.bson.Document;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.Container;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
+import org.testcontainers.lifecycle.Startables;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+
+@Slf4j
+public class MongodbSourceToConsoleIT extends FlinkContainer {
+
+    private static final String MONGODB_IMAGE = "mongo:latest";
+    private static final String MONGODB_CONTAINER_HOST = "flink_e2e_mongodb";
+    private static final String MONGODB_HOST = "localhost";
+    private static final int MONGODB_PORT = 27017;
+    private static final String MONGODB_DATABASE = "test_db";
+    private static final String MONGODB_COLLECTION = "test_table";
+    private static final String MONGODB_URL = 
String.format("mongodb://%s:%d/%s?retryWrites=true&writeConcern=majority", 
MONGODB_HOST, MONGODB_PORT, MONGODB_DATABASE);
+
+    private GenericContainer<?> mongodbContainer;
+
+    private MongoClient client;
+
+    @BeforeEach
+    public void startRedisContainer() {

Review Comment:
   Please change to `startMongoContainer`



##########
seatunnel-e2e/seatunnel-flink-connector-v2-e2e/src/test/java/org/apache/seatunnel/e2e/flink/v2/mongodb/MongodbSourceToConsoleIT.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.seatunnel.e2e.flink.v2.mongodb;
+
+import static org.testcontainers.shaded.org.awaitility.Awaitility.given;
+import static java.net.HttpURLConnection.HTTP_OK;
+import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
+
+import org.apache.seatunnel.e2e.flink.FlinkContainer;
+
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoClients;
+import com.mongodb.client.MongoCollection;
+import lombok.extern.slf4j.Slf4j;
+import org.bson.Document;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.testcontainers.containers.Container;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
+import org.testcontainers.lifecycle.Startables;
+
+import java.io.IOException;
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+
+@Slf4j
+public class MongodbSourceToConsoleIT extends FlinkContainer {
+
+    private static final String MONGODB_IMAGE = "mongo:latest";
+    private static final String MONGODB_CONTAINER_HOST = "flink_e2e_mongodb";
+    private static final String MONGODB_HOST = "localhost";
+    private static final int MONGODB_PORT = 27017;
+    private static final String MONGODB_DATABASE = "test_db";
+    private static final String MONGODB_COLLECTION = "test_table";
+    private static final String MONGODB_URL = 
String.format("mongodb://%s:%d/%s?retryWrites=true&writeConcern=majority", 
MONGODB_HOST, MONGODB_PORT, MONGODB_DATABASE);
+
+    private GenericContainer<?> mongodbContainer;
+
+    private MongoClient client;
+
+    @BeforeEach
+    public void startRedisContainer() {
+        mongodbContainer = new GenericContainer<>(MONGODB_IMAGE)
+            .withNetwork(NETWORK)
+            .withNetworkAliases(MONGODB_CONTAINER_HOST)
+            .withExposedPorts(MONGODB_PORT)
+            .waitingFor(new HttpWaitStrategy()
+                .forPort(MONGODB_PORT)
+                .forStatusCodeMatching(response -> response == HTTP_OK || 
response == HTTP_UNAUTHORIZED)
+                .withStartupTimeout(Duration.ofMinutes(2)))
+            .withLogConsumer(new Slf4jLogConsumer(log));
+        Startables.deepStart(Stream.of(mongodbContainer)).join();
+        log.info("Mongodb container started");
+        given().ignoreExceptions()
+            .await()
+            .atMost(180, TimeUnit.SECONDS)
+            .untilAsserted(this::initConnection);
+        this.generateTestData();
+    }
+
+    private void initConnection() {
+        client = MongoClients.create(MONGODB_URL);
+    }
+
+    private void generateTestData() {
+        MongoCollection<Document> mongoCollection = client
+            .getDatabase(MONGODB_DATABASE)
+            .getCollection(MONGODB_COLLECTION);
+        mongoCollection.deleteMany(new Document());
+
+        HashMap<String, Object> map = new HashMap<>();
+        map.put("id", 1);
+        map.put("key_aa", "value_aa");
+        map.put("key_bb", "value_bb");
+        Document doc = new Document(map);
+        mongoCollection.insertOne(doc);
+    }
+
+    @Test
+    public void testRedisSource() throws IOException, InterruptedException {

Review Comment:
   Please change to `testMongoSource`



##########
seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/source/MongodbSourceReader.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.mongodb.source;
+
+import org.apache.seatunnel.api.serialization.DeserializationSchema;
+import org.apache.seatunnel.api.source.Boundedness;
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.common.utils.JsonUtils;
+import 
org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitReader;
+import 
org.apache.seatunnel.connectors.seatunnel.common.source.SingleSplitReaderContext;
+import 
org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbParameters;
+
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoClients;
+import com.mongodb.client.MongoCursor;
+import org.bson.Document;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+public class MongodbSourceReader extends 
AbstractSingleSplitReader<SeaTunnelRow> {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(MongodbSourceReader.class);
+
+    protected final SingleSplitReaderContext context;
+
+    private MongoClient client;
+
+    protected final MongodbParameters params;
+
+    protected final DeserializationSchema<SeaTunnelRow> deserializationSchema;
+
+    MongodbSourceReader(SingleSplitReaderContext context, MongodbParameters 
params, DeserializationSchema<SeaTunnelRow> deserializationSchema) {
+        this.context = context;
+        this.params = params;
+        this.deserializationSchema = deserializationSchema;
+    }
+
+    @Override
+    public void open() throws Exception {
+        client = MongoClients.create(params.getUri());
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (client != null) {
+            client.close();
+        }
+    }
+
+    @Override
+    public void pollNext(Collector<SeaTunnelRow> output) throws Exception {
+        try (MongoCursor<Document> mongoCursor = 
client.getDatabase(params.getDatabase()).getCollection(params.getCollection()).find().iterator())
 {
+
+            while (mongoCursor.hasNext()) {
+                Document doc = mongoCursor.next();
+                HashMap<String, Object> map = new HashMap<>(doc.size());
+                Set<Map.Entry<String, Object>> entries = doc.entrySet();
+                for (Map.Entry<String, Object> entry : entries) {
+                    if (!"_id".equalsIgnoreCase(entry.getKey())) {
+                        String key = entry.getKey();
+                        Object value = entry.getValue();
+                        map.put(key, value);
+                    }
+                }
+                String content = JsonUtils.toJsonString(map);
+                if (deserializationSchema != null) {
+                    deserializationSchema.deserialize(content.getBytes(), 
output);
+                } else {
+                    // TODO: use seatunnel-text-format
+                    output.collect(new SeaTunnelRow(new Object[]{content}));
+                }
+            }
+        } finally {
+            if (Boundedness.BOUNDED.equals(context.getBoundedness())) {
+                // signal to the source that we have reached the end of the 
data.
+                LOGGER.info("Closed the bounded http source");

Review Comment:
   Mongo source.



##########
docs/en/connector-v2/source/MongoDB.md:
##########
@@ -0,0 +1,73 @@
+# MongoDb

Review Comment:
   Change doc as pr #2625 



-- 
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]

Reply via email to