Package io.debezium.connector.mongodb
Class MongoUtil
java.lang.Object
io.debezium.connector.mongodb.MongoUtil
Utilities for working with MongoDB.
- Author:
- Randall Hauch
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringThe delimiter used between addresses.static final Patternprivate static final PatternRegular expression that gets the host and (optional) port.private static final PatternRegular expression that gets the IPv6 host and (optional) port, where the IPv6 address must be surrounded by square brackets. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <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.Document> event) Helper function to extract the session transaction-id from an Change Stream event.static StringgetOplogSessionTransactionId(org.bson.Document oplogEvent) Helper function to extract the session transaction-id from an oplog event.protected static com.mongodb.ServerAddressgetPrimaryAddress(com.mongodb.client.MongoClient client) static 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.ServerAddressparseAddress(String addressStr) Parse the server address string, of the formhost:portorhost.protected static List<com.mongodb.ServerAddress>parseAddresses(String addressStr) Parse the comma-separated list of server addresses.static StringreplicaSetUsedIn(String addresses) Find the name of the replica set precedes the host addresses.protected static StringtoString(com.mongodb.ServerAddress address) protected static String
-
Field Details
-
ADDRESS_DELIMITER
The delimiter used between addresses.- See Also:
-
ADDRESS_DELIMITER_PATTERN
-
ADDRESS_PATTERN
Regular expression that gets the host and (optional) port. The raw expression is([^:]+)(:(\d+))?. -
IPV6_ADDRESS_PATTERN
Regular expression that gets the IPv6 host and (optional) port, where the IPv6 address must be surrounded by square brackets. The raw expression is(\[[^]]+\])(:(\d+))?.
-
-
Constructor Details
-
MongoUtil
private MongoUtil()
-
-
Method Details
-
replicaSetUsedIn
Find the name of the replica set precedes the host addresses.- Parameters:
addresses- the string containing the host addresses, of the formreplicaSetName/...; may not be null- Returns:
- the replica set name, or
nullif no replica set name is in the string
-
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
-
parseAddress
Parse the server address string, of the formhost:portorhost.The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.
- Parameters:
addressStr- the string containing the host and port; may be null- Returns:
- the server address, or
nullif the string did not contain a host or host:port pair
-
getOplogSessionTransactionId
Helper function to extract the session transaction-id from an oplog event.- Parameters:
oplogEvent- the oplog event- Returns:
- the session transaction id from the oplog event
-
getChangeStreamSessionTransactionId
public static SourceInfo.SessionTransactionId getChangeStreamSessionTransactionId(com.mongodb.client.model.changestream.ChangeStreamDocument<org.bson.Document> 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
-
parseAddresses
Parse the comma-separated list of server addresses. The format of the supplied string is one of the following:replicaSetName/host:port replicaSetName/host:port,host2:port2 replicaSetName/host:port,host2:port2,host3:port3 host:port host:port,host2:port2 host:port,host2:port2,host3:port3
wherereplicaSetNameis the name of the replica set,hostcontains the resolvable hostname or IP address of the server, andportis the integral port number. If the port is not provided, thedefault portis used. If neither the host or port are provided (oraddressStringisnull), then an address will use thedefault hostanddefault port.The IP address can be either an IPv4 address, or an IPv6 address surrounded by square brackets.
This method does not use the replica set name.
- Parameters:
addressStr- the string containing a comma-separated list of host and port pairs, optionally preceded by a replica set name- Returns:
- the list of server addresses; never null, but possibly empty
-
toString
-
toString
-
getPrimaryAddress
protected static com.mongodb.ServerAddress getPrimaryAddress(com.mongodb.client.MongoClient client)
-