-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature][Connector-V2] Add mongodb connecter source (#2596)
* Add mongodb connecter v2 source * Add docs * Add license header * Add mongodb e2e * Add license header * Add codestyle * change config file * Resolve conflicts * Add mongodb connecter v2 source * fix * fix * fix * fix * fix * fix * Fix codestyle
- Loading branch information
Showing
17 changed files
with
695 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# MongoDb | ||
|
||
> MongoDb source connector | ||
## Description | ||
|
||
Read data from MongoDB. | ||
|
||
## Key features | ||
|
||
- [x] [batch](../../concept/connector-v2-features.md) | ||
- [ ] [stream](../../concept/connector-v2-features.md) | ||
- [ ] [exactly-once](../../concept/connector-v2-features.md) | ||
- [x] [schema projection](../../concept/connector-v2-features.md) | ||
- [ ] [parallelism](../../concept/connector-v2-features.md) | ||
- [ ] [support user-defined split](../../concept/connector-v2-features.md) | ||
|
||
## Options | ||
|
||
| name | type | required | default value | | ||
|----------------|--------|----------|---------------| | ||
| uri | string | yes | - | | ||
| database | string | yes | - | | ||
| collection | string | yes | - | | ||
| schema | object | yes | - | | ||
| common-options | string | yes | - | | ||
|
||
### uri [string] | ||
|
||
MongoDB uri | ||
|
||
### database [string] | ||
|
||
MongoDB database | ||
|
||
### collection [string] | ||
|
||
MongoDB collection | ||
|
||
### schema [object] | ||
|
||
Because `MongoDB` does not have the concept of `schema`, when engine reads `MongoDB` , it will sample `MongoDB` data and infer the `schema` . In fact, this process will be slow and may be inaccurate. This parameter can be manually specified. Avoid these problems. | ||
|
||
such as: | ||
|
||
``` | ||
schema { | ||
fields { | ||
id = int | ||
key_aa = string | ||
key_bb = string | ||
} | ||
} | ||
``` | ||
|
||
### common options [string] | ||
|
||
Source Plugin common parameters, refer to [Source Plugin](common-options.md) for details | ||
|
||
## Example | ||
|
||
```bash | ||
mongodb { | ||
uri = "mongodb://username:password@127.0.0.1:27017/mypost?retryWrites=true&writeConcern=majority" | ||
database = "mydatabase" | ||
collection = "mycollection" | ||
schema { | ||
fields { | ||
id = int | ||
key_aa = string | ||
key_bb = string | ||
} | ||
} | ||
result_table_name = "mongodb_result_table" | ||
} | ||
``` |
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
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,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.seatunnel</groupId> | ||
<artifactId>seatunnel-connectors-v2</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
|
||
<artifactId>connector-mongodb</artifactId> | ||
|
||
<properties> | ||
<mongodb.version>3.12.11</mongodb.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.seatunnel</groupId> | ||
<artifactId>connector-common</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.seatunnel</groupId> | ||
<artifactId>seatunnel-format-json</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.seatunnel</groupId> | ||
<artifactId>seatunnel-api</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongodb-driver</artifactId> | ||
<version>${mongodb.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
39 changes: 39 additions & 0 deletions
39
...src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/config/MongodbConfig.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,39 @@ | ||
/* | ||
* 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.config; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* The config of mongodb | ||
*/ | ||
public class MongodbConfig implements Serializable { | ||
|
||
public static final String URI = "uri"; | ||
|
||
public static final String DATABASE = "database"; | ||
|
||
public static final String COLLECTION = "collection"; | ||
|
||
public static final String SCHEMA = "schema"; | ||
|
||
public static final String FORMAT = "format"; | ||
|
||
public static final String DEFAULT_FORMAT = "json"; | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/config/MongodbParameters.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,33 @@ | ||
/* | ||
* 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.config; | ||
|
||
import lombok.Data; | ||
|
||
import java.io.Serializable; | ||
|
||
@Data | ||
public class MongodbParameters implements Serializable { | ||
|
||
private String uri; | ||
|
||
private String database; | ||
|
||
private String collection; | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
...src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/source/MongodbSource.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,114 @@ | ||
/* | ||
* 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 static org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig.COLLECTION; | ||
import static org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig.DATABASE; | ||
import static org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig.DEFAULT_FORMAT; | ||
import static org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig.FORMAT; | ||
import static org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig.SCHEMA; | ||
import static org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig.URI; | ||
|
||
import org.apache.seatunnel.api.common.PrepareFailException; | ||
import org.apache.seatunnel.api.common.SeaTunnelContext; | ||
import org.apache.seatunnel.api.serialization.DeserializationSchema; | ||
import org.apache.seatunnel.api.source.Boundedness; | ||
import org.apache.seatunnel.api.source.SeaTunnelSource; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelDataType; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRowType; | ||
import org.apache.seatunnel.common.config.CheckConfigUtil; | ||
import org.apache.seatunnel.common.config.CheckResult; | ||
import org.apache.seatunnel.common.constants.PluginType; | ||
import org.apache.seatunnel.connectors.seatunnel.common.schema.SeaTunnelSchema; | ||
import org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitReader; | ||
import org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitSource; | ||
import org.apache.seatunnel.connectors.seatunnel.common.source.SingleSplitReaderContext; | ||
import org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbParameters; | ||
import org.apache.seatunnel.format.json.JsonDeserializationSchema; | ||
|
||
import org.apache.seatunnel.shade.com.typesafe.config.Config; | ||
import org.apache.seatunnel.shade.com.typesafe.config.ConfigBeanFactory; | ||
|
||
import com.google.auto.service.AutoService; | ||
|
||
@AutoService(SeaTunnelSource.class) | ||
public class MongodbSource extends AbstractSingleSplitSource<SeaTunnelRow> { | ||
|
||
private SeaTunnelContext seaTunnelContext; | ||
|
||
private SeaTunnelRowType rowType; | ||
|
||
private MongodbParameters params; | ||
|
||
private DeserializationSchema<SeaTunnelRow> deserializationSchema; | ||
|
||
@Override | ||
public String getPluginName() { | ||
return "MongoDB"; | ||
} | ||
|
||
@Override | ||
public void prepare(Config config) throws PrepareFailException { | ||
CheckResult result = CheckConfigUtil.checkAllExists(config, URI, DATABASE, COLLECTION); | ||
if (!result.isSuccess()) { | ||
throw new PrepareFailException(getPluginName(), PluginType.SOURCE, result.getMsg()); | ||
} | ||
|
||
this.params = ConfigBeanFactory.create(config, MongodbParameters.class); | ||
|
||
if (config.hasPath(SCHEMA)) { | ||
Config schema = config.getConfig(SCHEMA); | ||
this.rowType = SeaTunnelSchema.buildWithConfig(schema).getSeaTunnelRowType(); | ||
} else { | ||
this.rowType = SeaTunnelSchema.buildSimpleTextSchema(); | ||
} | ||
|
||
// TODO: use format SPI | ||
// default use json format | ||
String format; | ||
if (config.hasPath(FORMAT)) { | ||
format = config.getString(FORMAT); | ||
this.deserializationSchema = null; | ||
} else { | ||
format = DEFAULT_FORMAT; | ||
this.deserializationSchema = new JsonDeserializationSchema(false, false, rowType); | ||
} | ||
} | ||
|
||
@Override | ||
public void setSeaTunnelContext(SeaTunnelContext seaTunnelContext) { | ||
this.seaTunnelContext = seaTunnelContext; | ||
} | ||
|
||
@Override | ||
public Boundedness getBoundedness() { | ||
return Boundedness.BOUNDED; | ||
} | ||
|
||
@Override | ||
public SeaTunnelDataType<SeaTunnelRow> getProducedType() { | ||
return this.rowType; | ||
} | ||
|
||
@Override | ||
public AbstractSingleSplitReader<SeaTunnelRow> createReader(SingleSplitReaderContext context) throws Exception { | ||
return new MongodbSourceReader(context, this.params, this.deserializationSchema); | ||
} | ||
|
||
} |
Oops, something went wrong.