Class MongoSinks


  • public final class MongoSinks
    extends java.lang.Object
    Contains factory methods for MongoDB sinks.
    Since:
    5.3
    • Method Detail

      • builder

        public static <T> MongoSinkBuilder<T> builder​(@Nonnull
                                                      java.lang.Class<T> itemClass,
                                                      @Nonnull
                                                      SupplierEx<com.mongodb.client.MongoClient> clientSupplier)
        Returns a builder object that offers a step-by-step fluent API to build a custom MongoDB Sink for the Pipeline API.

        The sink inserts or replaces the items it receives to specified collection using MongoCollection.bulkWrite(java.util.List<? extends com.mongodb.client.model.WriteModel<? extends TDocument>>).

        All operations are done within transaction if processing guarantee of the job is ProcessingGuarantee.EXACTLY_ONCE.

        All writes are done using default MongoDB codecs with POJO class codec added.

        Example usage:

        
         Sink<Document> mongoSink =
                 MongoSinks.builder(
                             Document.class,
                             () -> MongoClients.create("mongodb://127.0.0.1:27017")
                         )
                         .into("myDatabase", "myCollection")
                         .identifyDocumentBy("_id", doc -> doc.get("_id"))
                         .build()
                 );
        
         Pipeline p = Pipeline.create();
         (...)
         someStage.writeTo(mongoSink);
         
        Type Parameters:
        T - type of the items the sink accepts
        Parameters:
        clientSupplier - MongoDB client supplier
        itemClass - type of document that will be saved
        Since:
        5.3
      • builder

        public static <T> MongoSinkBuilder<T> builder​(@Nonnull
                                                      java.lang.Class<T> itemClass,
                                                      @Nonnull
                                                      DataConnectionRef dataConnectionRef)
        Returns a builder object that offers a step-by-step fluent API to build a custom MongoDB Sink for the Pipeline API.

        The sink inserts or replaces the items it receives to specified collection using MongoCollection.bulkWrite(java.util.List<? extends com.mongodb.client.model.WriteModel<? extends TDocument>>).

        All operations are done within transaction if processing guarantee of the job is ProcessingGuarantee.EXACTLY_ONCE.

        All writes are done using default MongoDB codecs with POJO class codec added.

        Example usage:

        
         Sink<Document> mongoSink =
                 MongoSinks.builder(
                             Document.class,
                             dataConnectionRef("someMongoDB")
                         )
                         .into("myDatabase", "myCollection")
                         .identifyDocumentBy("_id", doc -> doc.get("_id"))
                         .build()
                 );
        
         Pipeline p = Pipeline.create();
         (...)
         someStage.writeTo(mongoSink);
         

        Connector will use provided data connection reference to obtain an instance of MongoClient. Depending on the configuration this client may be shared between processors or not.

        Type Parameters:
        T - type of the items the sink accepts
        Parameters:
        dataConnectionRef - reference to mongo data connection
        itemClass - type of document that will be saved
        Since:
        5.3
      • mongodb

        public static Sink<org.bson.Document> mongodb​(@Nonnull
                                                      java.lang.String connectionString,
                                                      @Nonnull
                                                      java.lang.String database,
                                                      @Nonnull
                                                      java.lang.String collection)
        Convenience for builder(java.lang.Class<T>, com.hazelcast.function.SupplierEx<com.mongodb.client.MongoClient>). Example usage:
        
         Sink<Document> mongoSink =
                 MongoSinks.mongodb(
                         "mongodb://127.0.0.1:27017",
                         "myDatabase",
                         "myCollection"
                 );
        
         Pipeline p = Pipeline.create();
         (...)
         someStage.writeTo(mongoSink);
         
        Parameters:
        connectionString - connection string to MongoDB instance
        database - database to which the documents will be put into
        collection - collection to which the documents will be put into
        Since:
        5.3
      • mongodb

        public static Sink<org.bson.Document> mongodb​(@Nonnull
                                                      DataConnectionRef dataConnectionRef,
                                                      @Nonnull
                                                      java.lang.String database,
                                                      @Nonnull
                                                      java.lang.String collection)
        Convenience for builder(java.lang.Class<T>, com.hazelcast.function.SupplierEx<com.mongodb.client.MongoClient>). Example usage:
        
         Sink<Document> mongoSink =
                 MongoSinks.mongodb(
                         dataConnectionRef("someMongoDB"),
                         "myDatabase",
                         "myCollection"
                 );
        
         Pipeline p = Pipeline.create();
         (...)
         someStage.writeTo(mongoSink);
         
        Parameters:
        dataConnectionRef - reference to some mongo data connection
        database - database to which the documents will be put into
        collection - collection to which the documents will be put into
        Since:
        5.3