Package io.debezium.connector.mongodb
Class MongoUtils
java.lang.Object
io.debezium.connector.mongodb.MongoUtils
Utilities for working with MongoDB.
- Author:
- Randall Hauch
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic com.mongodb.connection.ClusterDescriptionclusterDescription(com.mongodb.client.MongoClient client) Retrieves cluster description, forcing a connection if not yet availablestatic <T> booleanDetermine if the suppliedMongoIterablecontains an element that is equal to the supplied value.static <T> booleanDetermine if the suppliedMongoIterablecontains at least one element that satisfies the given predicate.static <T> voidPerform the given operation on each of the values in the iterable container.static voidforEachCollectionNameInDatabase(com.mongodb.client.MongoClient client, String databaseName, Consumer<String> operation) Perform the given operation on each of the collection names in the named database.static voidforEachDatabaseName(com.mongodb.client.MongoClient client, Consumer<String> operation) Perform the given operation on each of the database names.getChangeStreamSessionTransactionId(com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.BsonDocument> event) Helper function to extract the session transaction-id from an Change Stream event.static org.bson.BsonTimestampstatic voidonCollection(com.mongodb.client.MongoClient client, String dbName, String collectionName, Consumer<com.mongodb.client.MongoCollection<org.bson.Document>> collectionOperation) Perform the given operation on the named collection in the named database, if the database and collection both exist.static voidonCollectionDocuments(com.mongodb.client.MongoClient client, String dbName, String collectionName, BlockingConsumer<org.bson.Document> documentOperation) Perform the given operation on all of the documents inside the named collection in the named database, if the database and collection both exist.static voidonDatabase(com.mongodb.client.MongoClient client, String dbName, Consumer<com.mongodb.client.MongoDatabase> dbOperation) Perform the given operation on the database with the given name, only if that database exists.static com.mongodb.client.ChangeStreamIterable<org.bson.BsonDocument>openChangeStream(com.mongodb.client.MongoClient client, MongoDbTaskContext taskContext) Opens change stream based onMongoDbConnectorConfig.getCaptureScope()replicaSetName(com.mongodb.connection.ClusterDescription clusterDescription)
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
-
Constructor Details
-
MongoUtils
private MongoUtils()
-
-
Method Details
-
forEachDatabaseName
public static void forEachDatabaseName(com.mongodb.client.MongoClient client, Consumer<String> operation) Perform the given operation on each of the database names.- Parameters:
client- the MongoDB client; may not be nulloperation- the operation to perform; may not be null
-
forEachCollectionNameInDatabase
public static void forEachCollectionNameInDatabase(com.mongodb.client.MongoClient client, String databaseName, Consumer<String> operation) Perform the given operation on each of the collection names in the named database.- Parameters:
client- the MongoDB client; may not be nulldatabaseName- the name of the database; may not be nulloperation- the operation to perform; may not be null
-
forEach
Perform the given operation on each of the values in the iterable container.- Parameters:
iterable- the iterable collection obtained from a MongoDB client; may not be nulloperation- the operation to perform; may not be null
-
onDatabase
public static void onDatabase(com.mongodb.client.MongoClient client, String dbName, Consumer<com.mongodb.client.MongoDatabase> dbOperation) Perform the given operation on the database with the given name, only if that database exists.- Parameters:
client- the MongoDB client; may not be nulldbName- the name of the database; may not be nulldbOperation- the operation to perform; may not be null
-
onCollection
public static void onCollection(com.mongodb.client.MongoClient client, String dbName, String collectionName, Consumer<com.mongodb.client.MongoCollection<org.bson.Document>> collectionOperation) Perform the given operation on the named collection in the named database, if the database and collection both exist.- Parameters:
client- the MongoDB client; may not be nulldbName- the name of the database; may not be nullcollectionName- the name of the collection; may not be nullcollectionOperation- the operation to perform; may not be null
-
onCollectionDocuments
public static void onCollectionDocuments(com.mongodb.client.MongoClient client, String dbName, String collectionName, BlockingConsumer<org.bson.Document> documentOperation) Perform the given operation on all of the documents inside the named collection in the named database, if the database and collection both exist. The operation is called once for each document, so if the collection exists but is empty then the function will not be called.- Parameters:
client- the MongoDB client; may not be nulldbName- the name of the database; may not be nullcollectionName- the name of the collection; may not be nulldocumentOperation- the operation to perform; may not be null
-
contains
Determine if the suppliedMongoIterablecontains an element that is equal to the supplied value.- Parameters:
iterable- the iterable; may not be nullmatch- the value to find in the iterable; may be null- Returns:
trueif a matching value was found, orfalseotherwise
-
contains
public static <T> boolean contains(com.mongodb.client.MongoIterable<T> iterable, Predicate<T> matcher) Determine if the suppliedMongoIterablecontains at least one element that satisfies the given predicate.- Parameters:
iterable- the iterable; may not be nullmatcher- the predicate function called on each value in the iterable until a match is found; may not be null- Returns:
trueif a matching value was found, orfalseotherwise
-
getChangeStreamSessionTransactionId
public static SourceInfo.SessionTransactionId getChangeStreamSessionTransactionId(com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.BsonDocument> event) Helper function to extract the session transaction-id from an Change Stream event.- Parameters:
event- the Change Stream event- Returns:
- the session transaction id from the event
-
clusterDescription
public static com.mongodb.connection.ClusterDescription clusterDescription(com.mongodb.client.MongoClient client) Retrieves cluster description, forcing a connection if not yet available- Parameters:
client- cluster connection client- Returns:
- cluster description
-
replicaSetName
-
openChangeStream
public static com.mongodb.client.ChangeStreamIterable<org.bson.BsonDocument> openChangeStream(com.mongodb.client.MongoClient client, MongoDbTaskContext taskContext) Opens change stream based onMongoDbConnectorConfig.getCaptureScope()- Parameters:
client- mongodb clienttaskContext- task context- Returns:
- change stream iterable
-
hello
-