-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature][connector-v2][mongodb] mongodb support cdc sink #4833
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d60a51
[Feature][connector-v2][mongodb] mongodb support cdc sink
MonsterChenzhuo 6608fcc
Rewrite ci logic
MonsterChenzhuo 60fe820
Ignore UPDATE_BEFORE
MonsterChenzhuo cd091d0
Fix the review problem
MonsterChenzhuo 2a080a2
Merge branch 'dev' into mongodb-cdc
EricJoy2048 2c461e7
Merge branch 'dev' into mongodb-cdc
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,13 @@ | |
package org.apache.seatunnel.connectors.seatunnel.mongodb.serde; | ||
|
||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.connectors.seatunnel.mongodb.exception.MongodbConnectorException; | ||
import org.apache.seatunnel.connectors.seatunnel.mongodb.sink.MongodbWriterOptions; | ||
|
||
import org.bson.BsonDocument; | ||
import org.bson.conversions.Bson; | ||
|
||
import com.mongodb.client.model.DeleteOneModel; | ||
import com.mongodb.client.model.Filters; | ||
import com.mongodb.client.model.InsertOneModel; | ||
import com.mongodb.client.model.UpdateOneModel; | ||
|
@@ -33,6 +35,8 @@ | |
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.apache.seatunnel.common.exception.CommonErrorCode.ILLEGAL_ARGUMENT; | ||
|
||
public class RowDataDocumentSerializer implements DocumentSerializer<SeaTunnelRow> { | ||
|
||
private final RowDataToBsonConverters.RowDataToBsonConverter rowDataToBsonConverter; | ||
|
@@ -52,6 +56,20 @@ public RowDataDocumentSerializer( | |
|
||
@Override | ||
public WriteModel<BsonDocument> serializeToWriteModel(SeaTunnelRow row) { | ||
switch (row.getRowKind()) { | ||
case INSERT: | ||
case UPDATE_AFTER: | ||
return upsert(row); | ||
case UPDATE_BEFORE: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignore UPDATE_BEFORE if upsert is enabled There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If upsert is not enabled, update_before needs to as delete and update_after needs to as insert |
||
case DELETE: | ||
return delete(row); | ||
default: | ||
throw new MongodbConnectorException( | ||
ILLEGAL_ARGUMENT, "Unsupported message kind: " + row.getRowKind()); | ||
} | ||
} | ||
|
||
private WriteModel<BsonDocument> upsert(SeaTunnelRow row) { | ||
final BsonDocument bsonDocument = rowDataToBsonConverter.convert(row); | ||
if (isUpsertEnable) { | ||
Bson filter = generateFilter(filterConditions.apply(bsonDocument)); | ||
|
@@ -63,6 +81,12 @@ public WriteModel<BsonDocument> serializeToWriteModel(SeaTunnelRow row) { | |
} | ||
} | ||
|
||
private WriteModel<BsonDocument> delete(SeaTunnelRow row) { | ||
final BsonDocument bsonDocument = rowDataToBsonConverter.convert(row); | ||
Bson filter = generateFilter(filterConditions.apply(bsonDocument)); | ||
return new DeleteOneModel<>(filter); | ||
} | ||
|
||
public static Bson generateFilter(BsonDocument filterConditions) { | ||
List<Bson> filters = | ||
filterConditions.entrySet().stream() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
...db-e2e/src/test/java/org/apache/seatunnel/e2e/connector/v2/mongodb/AbstractMongodbIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
/* | ||
* 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.connector.v2.mongodb; | ||
|
||
import org.apache.seatunnel.e2e.common.TestResource; | ||
import org.apache.seatunnel.e2e.common.TestSuiteBase; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.bson.Document; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; | ||
import org.testcontainers.lifecycle.Startables; | ||
import org.testcontainers.utility.DockerImageName; | ||
import org.testcontainers.utility.DockerLoggerFactory; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoCursor; | ||
import com.mongodb.client.model.Sorts; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Stream; | ||
|
||
import static java.net.HttpURLConnection.HTTP_OK; | ||
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; | ||
|
||
@Slf4j | ||
public abstract class AbstractMongodbIT extends TestSuiteBase implements TestResource { | ||
|
||
protected static final Random RANDOM = new Random(); | ||
|
||
protected static final List<Document> TEST_MATCH_DATASET = generateTestDataSet(5); | ||
|
||
protected static final List<Document> TEST_SPLIT_DATASET = generateTestDataSet(10); | ||
|
||
protected static final String MONGODB_IMAGE = "mongo:latest"; | ||
|
||
protected static final String MONGODB_CONTAINER_HOST = "e2e_mongodb"; | ||
|
||
protected static final int MONGODB_PORT = 27017; | ||
|
||
protected static final String MONGODB_DATABASE = "test_db"; | ||
|
||
protected static final String MONGODB_MATCH_TABLE = "test_match_op_db"; | ||
|
||
protected static final String MONGODB_SPLIT_TABLE = "test_split_op_db"; | ||
|
||
protected static final String MONGODB_MATCH_RESULT_TABLE = "test_match_op_result_db"; | ||
|
||
protected static final String MONGODB_SPLIT_RESULT_TABLE = "test_split_op_result_db"; | ||
|
||
protected static final String MONGODB_SINK_TABLE = "test_source_sink_table"; | ||
|
||
protected static final String MONGODB_UPDATE_TABLE = "test_update_table"; | ||
|
||
protected static final String MONGODB_FLAT_TABLE = "test_flat_table"; | ||
|
||
protected static final String MONGODB_CDC_RESULT_TABLE = "test_cdc_table"; | ||
|
||
protected GenericContainer<?> mongodbContainer; | ||
|
||
protected MongoClient client; | ||
|
||
public void initConnection() { | ||
String host = mongodbContainer.getContainerIpAddress(); | ||
int port = mongodbContainer.getFirstMappedPort(); | ||
String url = String.format("mongodb://%s:%d/%s", host, port, MONGODB_DATABASE); | ||
client = MongoClients.create(url); | ||
} | ||
|
||
protected void initSourceData() { | ||
MongoCollection<Document> sourceMatchTable = | ||
client.getDatabase(MONGODB_DATABASE).getCollection(MONGODB_MATCH_TABLE); | ||
|
||
sourceMatchTable.deleteMany(new Document()); | ||
sourceMatchTable.insertMany(TEST_MATCH_DATASET); | ||
|
||
MongoCollection<Document> sourceSplitTable = | ||
client.getDatabase(MONGODB_DATABASE).getCollection(MONGODB_SPLIT_TABLE); | ||
|
||
sourceSplitTable.deleteMany(new Document()); | ||
sourceSplitTable.insertMany(TEST_SPLIT_DATASET); | ||
} | ||
|
||
protected void clearDate(String table) { | ||
client.getDatabase(MONGODB_DATABASE).getCollection(table).drop(); | ||
} | ||
|
||
public static List<Document> generateTestDataSet(int count) { | ||
List<Document> dataSet = new ArrayList<>(); | ||
|
||
for (int i = 0; i < count; i++) { | ||
dataSet.add( | ||
new Document( | ||
"c_map", | ||
new Document("OQBqH", randomString()) | ||
.append("rkvlO", randomString()) | ||
.append("pCMEX", randomString()) | ||
.append("DAgdj", randomString()) | ||
.append("dsJag", randomString())) | ||
.append( | ||
"c_array", | ||
Arrays.asList( | ||
RANDOM.nextInt(), | ||
RANDOM.nextInt(), | ||
RANDOM.nextInt(), | ||
RANDOM.nextInt(), | ||
RANDOM.nextInt())) | ||
.append("c_string", randomString()) | ||
.append("c_boolean", RANDOM.nextBoolean()) | ||
.append("c_int", i) | ||
.append("c_bigint", RANDOM.nextLong()) | ||
.append("c_double", RANDOM.nextDouble() * Double.MAX_VALUE) | ||
.append( | ||
"c_row", | ||
new Document( | ||
"c_map", | ||
new Document("OQBqH", randomString()) | ||
.append("rkvlO", randomString()) | ||
.append("pCMEX", randomString()) | ||
.append("DAgdj", randomString()) | ||
.append("dsJag", randomString())) | ||
.append( | ||
"c_array", | ||
Arrays.asList( | ||
RANDOM.nextInt(), | ||
RANDOM.nextInt(), | ||
RANDOM.nextInt(), | ||
RANDOM.nextInt(), | ||
RANDOM.nextInt())) | ||
.append("c_string", randomString()) | ||
.append("c_boolean", RANDOM.nextBoolean()) | ||
.append("c_int", RANDOM.nextInt()) | ||
.append("c_bigint", RANDOM.nextLong()) | ||
.append( | ||
"c_double", | ||
RANDOM.nextDouble() * Double.MAX_VALUE))); | ||
} | ||
return dataSet; | ||
} | ||
|
||
protected static String randomString() { | ||
int length = RANDOM.nextInt(10) + 1; | ||
StringBuilder sb = new StringBuilder(length); | ||
for (int i = 0; i < length; i++) { | ||
char c = (char) (RANDOM.nextInt(26) + 'a'); | ||
sb.append(c); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
protected List<Document> readMongodbData(String collection) { | ||
MongoCollection<Document> sinkTable = | ||
client.getDatabase(MONGODB_DATABASE).getCollection(collection); | ||
MongoCursor<Document> cursor = sinkTable.find().sort(Sorts.ascending("c_int")).cursor(); | ||
List<Document> documents = new ArrayList<>(); | ||
while (cursor.hasNext()) { | ||
documents.add(cursor.next()); | ||
} | ||
return documents; | ||
} | ||
|
||
@BeforeAll | ||
@Override | ||
public void startUp() { | ||
DockerImageName imageName = DockerImageName.parse(MONGODB_IMAGE); | ||
mongodbContainer = | ||
new GenericContainer<>(imageName) | ||
.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(DockerLoggerFactory.getLogger(MONGODB_IMAGE))); | ||
// For local test use | ||
// mongodbContainer.setPortBindings(Collections.singletonList("27017:27017")); | ||
Startables.deepStart(Stream.of(mongodbContainer)).join(); | ||
log.info("Mongodb container started"); | ||
|
||
Awaitility.given() | ||
.ignoreExceptions() | ||
.atLeast(100, TimeUnit.MILLISECONDS) | ||
.pollInterval(500, TimeUnit.MILLISECONDS) | ||
.atMost(180, TimeUnit.SECONDS) | ||
.untilAsserted(this::initConnection); | ||
this.initSourceData(); | ||
} | ||
|
||
@AfterAll | ||
@Override | ||
public void tearDown() { | ||
if (client != null) { | ||
client.close(); | ||
} | ||
if (mongodbContainer != null) { | ||
mongodbContainer.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update Changelog & release-note