Skip to content
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

[Improve] mongodb connector v2 add source query capability #3697

Merged
merged 14 commits into from
Dec 26, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public class MongodbConfig implements Serializable {
.noDefaultValue()
.withDescription("MongoDB collection");

public static final Option<String> MATCHQUERY =
Options.key("matchQuery")
.stringType()
.noDefaultValue()
.withDescription("matchQuery MongoDB collection");

// Don't use now
public static final String FORMAT = "format";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public class MongodbParameters implements Serializable {

private String collection;

private String matchQuery;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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.MATCHQUERY;
import static org.apache.seatunnel.connectors.seatunnel.mongodb.config.MongodbConfig.URI;

import org.apache.seatunnel.api.configuration.util.OptionRule;
Expand All @@ -37,6 +38,6 @@ public String factoryIdentifier() {

@Override
public OptionRule optionRule() {
return OptionRule.builder().required(URI, DATABASE, COLLECTION, SeaTunnelSchema.SCHEMA).build();
return OptionRule.builder().required(URI, DATABASE, COLLECTION, MATCHQUERY, SeaTunnelSchema.SCHEMA).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Projections;
import lombok.extern.slf4j.Slf4j;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.io.IOException;
import java.util.Optional;

@Slf4j
public class MongodbSourceReader extends AbstractSingleSplitReader<SeaTunnelRow> {
Expand Down Expand Up @@ -86,7 +88,7 @@ public void close() throws IOException {
public void pollNext(Collector<SeaTunnelRow> output) throws Exception {
try (MongoCursor<Document> mongoCursor = client.getDatabase(params.getDatabase())
.getCollection(params.getCollection())
.find()
.find(Optional.ofNullable(params.getMatchQuery()).isPresent() ? BsonDocument.parse(params.getMatchQuery()) : new BsonDocument())
.projection(projectionFields)
.iterator()) {
while (mongoCursor.hasNext()) {
Expand Down