-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Feature][Connector-V2] MongodbSink Support SaveMode #9883
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
Open
chl-wxp
wants to merge
8
commits into
apache:dev
Choose a base branch
from
chl-wxp:feature/mongodbSupportSaveMode
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92c7527
[feature] MongodbSink Support SaveMode
chl-wxp 7a9114e
[improve] add ut
chl-wxp d021fce
[bugfix] fix unknow host bug
chl-wxp 542e734
[bugfix] sourceCatalogTable to sinkCatalogTable
chl-wxp b8f1180
[bugfix] sourceCatalogTable to sinkCatalogTable 2
chl-wxp cc18c45
[bugfix] fix npe bug
chl-wxp d302159
[feature] add doc
chl-wxp 468cfde
[feature] add doc 2
chl-wxp 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 hidden or 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
211 changes: 211 additions & 0 deletions
211
...c/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/catalog/MongodbCatalog.java
This file contains hidden or 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,211 @@ | ||
/* | ||
* 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.catalog; | ||
|
||
import org.apache.seatunnel.api.table.catalog.Catalog; | ||
import org.apache.seatunnel.api.table.catalog.CatalogTable; | ||
import org.apache.seatunnel.api.table.catalog.TablePath; | ||
import org.apache.seatunnel.api.table.catalog.exception.CatalogException; | ||
import org.apache.seatunnel.api.table.catalog.exception.DatabaseAlreadyExistException; | ||
import org.apache.seatunnel.api.table.catalog.exception.DatabaseNotExistException; | ||
import org.apache.seatunnel.api.table.catalog.exception.TableAlreadyExistException; | ||
import org.apache.seatunnel.api.table.catalog.exception.TableNotExistException; | ||
import org.apache.seatunnel.common.exception.CommonError; | ||
|
||
import org.bson.Document; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MongodbCatalog implements Catalog { | ||
|
||
private final String catalogName; | ||
private final String baseUrl; | ||
private transient MongoClient mongoClient; | ||
private final String defaultDatabase; | ||
|
||
public MongodbCatalog(String catalogName, String baseUrl, String defaultDatabase) { | ||
this.catalogName = catalogName; | ||
this.baseUrl = baseUrl; | ||
this.defaultDatabase = defaultDatabase; | ||
} | ||
|
||
@Override | ||
public void open() throws CatalogException { | ||
try { | ||
if (mongoClient == null) { | ||
mongoClient = MongoClients.create(baseUrl); | ||
} | ||
} catch (Exception e) { | ||
throw new CatalogException("Failed to open MongoDB Catalog: " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return catalogName; | ||
} | ||
|
||
@Override | ||
public String getDefaultDatabase() throws CatalogException { | ||
return defaultDatabase; | ||
} | ||
|
||
@Override | ||
public boolean databaseExists(String databaseName) throws CatalogException { | ||
try { | ||
return listDatabases().contains(databaseName); | ||
} catch (Exception e) { | ||
throw new CatalogException("Failed to check database existence: " + databaseName, e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> listDatabases() throws CatalogException { | ||
try { | ||
List<String> dbs = new ArrayList<>(); | ||
for (String name : mongoClient.listDatabaseNames()) { | ||
dbs.add(name); | ||
} | ||
return dbs; | ||
} catch (Exception e) { | ||
throw new CatalogException("Failed to list databases", e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> listTables(String databaseName) | ||
throws CatalogException, DatabaseNotExistException { | ||
if (!databaseExists(databaseName)) { | ||
throw new DatabaseNotExistException(name(), databaseName); | ||
} | ||
try { | ||
MongoDatabase db = mongoClient.getDatabase(databaseName); | ||
return db.listCollectionNames().into(new ArrayList<>()); | ||
} catch (Exception e) { | ||
throw new CatalogException("Failed to list tables for database: " + databaseName, e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean tableExists(TablePath tablePath) throws CatalogException { | ||
try { | ||
return listTables(tablePath.getDatabaseName()).contains(tablePath.getTableName()); | ||
} catch (DatabaseNotExistException e) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public CatalogTable getTable(TablePath tablePath) | ||
throws CatalogException, TableNotExistException { | ||
throw CommonError.unsupportedOperation(name(), "get table with tablePath "); | ||
} | ||
|
||
@Override | ||
public void createTable(TablePath tablePath, CatalogTable table, boolean ignoreIfExists) | ||
throws TableAlreadyExistException, DatabaseNotExistException, CatalogException { | ||
if (!databaseExists(tablePath.getDatabaseName())) { | ||
throw new DatabaseNotExistException(name(), tablePath.getDatabaseName()); | ||
} | ||
if (tableExists(tablePath)) { | ||
if (ignoreIfExists) return; | ||
throw new TableAlreadyExistException(name(), tablePath); | ||
} | ||
try { | ||
MongoDatabase db = mongoClient.getDatabase(tablePath.getDatabaseName()); | ||
db.createCollection(tablePath.getTableName()); | ||
} catch (Exception e) { | ||
throw new CatalogException( | ||
"Failed to create collection: " + tablePath.getFullName(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public void dropTable(TablePath tablePath, boolean ignoreIfNotExists) | ||
throws TableNotExistException, CatalogException { | ||
if (!tableExists(tablePath)) { | ||
if (ignoreIfNotExists) return; | ||
throw new TableNotExistException(name(), tablePath); | ||
} | ||
try { | ||
MongoDatabase db = mongoClient.getDatabase(tablePath.getDatabaseName()); | ||
db.getCollection(tablePath.getTableName()).drop(); | ||
} catch (Exception e) { | ||
throw new CatalogException("Failed to drop collection: " + tablePath.getFullName(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public void createDatabase(TablePath tablePath, boolean ignoreIfExists) | ||
throws DatabaseAlreadyExistException, CatalogException { | ||
throw CommonError.unsupportedOperation(name(), "create database "); | ||
} | ||
|
||
@Override | ||
public void dropDatabase(TablePath tablePath, boolean ignoreIfNotExists) | ||
throws DatabaseNotExistException, CatalogException { | ||
throw CommonError.unsupportedOperation(name(), "drop database "); | ||
} | ||
|
||
@Override | ||
public void truncateTable(TablePath tablePath, boolean ignoreIfNotExists) | ||
throws TableNotExistException, CatalogException { | ||
try { | ||
if (!tableExists(tablePath)) { | ||
if (ignoreIfNotExists) { | ||
return; | ||
} | ||
throw new TableNotExistException(name(), tablePath); | ||
} | ||
MongoDatabase db = mongoClient.getDatabase(tablePath.getDatabaseName()); | ||
MongoCollection<Document> collection = db.getCollection(tablePath.getTableName()); | ||
collection.deleteMany(new Document()); | ||
} catch (Exception e) { | ||
throw new CatalogException( | ||
"Failed to truncate collection: " + tablePath.getFullName(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isExistsData(TablePath tablePath) { | ||
try { | ||
if (!tableExists(tablePath)) { | ||
return false; | ||
} | ||
MongoDatabase db = mongoClient.getDatabase(tablePath.getDatabaseName()); | ||
MongoCollection<Document> collection = db.getCollection(tablePath.getTableName()); | ||
return collection.estimatedDocumentCount() > 0; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws CatalogException { | ||
if (mongoClient != null) { | ||
mongoClient.close(); | ||
mongoClient = null; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...java/org/apache/seatunnel/connectors/seatunnel/mongodb/catalog/MongodbCatalogFactory.java
This file contains hidden or 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,48 @@ | ||
/* | ||
* 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.catalog; | ||
|
||
import org.apache.seatunnel.api.configuration.ReadonlyConfig; | ||
import org.apache.seatunnel.api.configuration.util.OptionRule; | ||
import org.apache.seatunnel.api.table.catalog.Catalog; | ||
import org.apache.seatunnel.api.table.factory.CatalogFactory; | ||
import org.apache.seatunnel.api.table.factory.Factory; | ||
import org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig; | ||
|
||
import com.google.auto.service.AutoService; | ||
|
||
import static org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig.CONNECTOR_IDENTITY; | ||
|
||
@AutoService(Factory.class) | ||
public class MongodbCatalogFactory implements CatalogFactory { | ||
@Override | ||
public Catalog createCatalog(String catalogName, ReadonlyConfig options) { | ||
return new MongodbCatalog( | ||
catalogName, options.get(MongodbConfig.URI), options.get(MongodbConfig.DATABASE)); | ||
} | ||
|
||
@Override | ||
public String factoryIdentifier() { | ||
return CONNECTOR_IDENTITY; | ||
} | ||
|
||
@Override | ||
public OptionRule optionRule() { | ||
return OptionRule.builder().required(MongodbConfig.URI, MongodbConfig.DATABASE).build(); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -19,12 +19,18 @@ | |
|
||
import org.apache.seatunnel.api.configuration.Option; | ||
import org.apache.seatunnel.api.configuration.Options; | ||
import org.apache.seatunnel.api.sink.DataSaveMode; | ||
|
||
import org.bson.json.JsonMode; | ||
import org.bson.json.JsonWriterSettings; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.apache.seatunnel.api.sink.DataSaveMode.APPEND_DATA; | ||
import static org.apache.seatunnel.api.sink.DataSaveMode.DROP_DATA; | ||
import static org.apache.seatunnel.api.sink.DataSaveMode.ERROR_WHEN_DATA_EXISTS; | ||
|
||
public class MongodbConfig { | ||
|
||
public static final String CONNECTOR_IDENTITY = "MongoDB"; | ||
|
@@ -153,4 +159,12 @@ public class MongodbConfig { | |
|
||
public static final Option<Boolean> TRANSACTION = | ||
Options.key("transaction").booleanType().defaultValue(false).withDescription("."); | ||
|
||
public static final Option<DataSaveMode> DATA_SAVE_MODE = | ||
Options.key("data_save_mode") | ||
.singleChoice( | ||
DataSaveMode.class, | ||
Arrays.asList(DROP_DATA, APPEND_DATA, ERROR_WHEN_DATA_EXISTS)) | ||
.defaultValue(APPEND_DATA) | ||
.withDescription("data_save_mode"); | ||
|
||
} |
This file contains hidden or 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
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.
Describing him in the "Description" section of the table would be more in line with the previous format