Search in sources :

Example 6 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project gravitee-access-management by gravitee-io.

the class MongodbProvider method afterPropertiesSet.

@Override
public void afterPropertiesSet() {
    mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"));
    mongoDBContainer.withEnv("MONGO_INITDB_DATABASE", databaseName);
    mongoDBContainer.start();
    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongoDBContainer.getHost(), mongoDBContainer.getFirstMappedPort()))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build()));
    MongoClientSettings settings = MongoClientSettings.builder().applyToClusterSettings(builder1 -> builder1.applySettings(clusterSettings)).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    mongoClient = MongoClients.create(settings);
    mongoDatabase = mongoClient.getDatabase(databaseName);
}
Also used : MongoDBContainer(org.testcontainers.containers.MongoDBContainer) ServerAddress(com.mongodb.ServerAddress) PojoCodecProvider(org.bson.codecs.pojo.PojoCodecProvider) DockerImageName(org.testcontainers.utility.DockerImageName) CodecRegistries.fromProviders(org.bson.codecs.configuration.CodecRegistries.fromProviders) ClusterSettings(com.mongodb.connection.ClusterSettings) MongoClients(com.mongodb.reactivestreams.client.MongoClients) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) InitializingBean(org.springframework.beans.factory.InitializingBean) MongoDBContainer(org.testcontainers.containers.MongoDBContainer) MongoClient(com.mongodb.reactivestreams.client.MongoClient) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) DisposableBean(org.springframework.beans.factory.DisposableBean) MongoClientSettings(com.mongodb.MongoClientSettings) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) CodecRegistries.fromRegistries(org.bson.codecs.configuration.CodecRegistries.fromRegistries) ClusterSettings(com.mongodb.connection.ClusterSettings) ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) CodecRegistry(org.bson.codecs.configuration.CodecRegistry)

Example 7 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project pulsar by apache.

the class MongoSink method open.

@Override
public void open(Map<String, Object> config, SinkContext sinkContext) throws Exception {
    log.info("Open MongoDB Sink");
    mongoConfig = MongoConfig.load(config);
    mongoConfig.validate(true, true);
    if (clientProvider != null) {
        mongoClient = clientProvider.get();
    } else {
        mongoClient = MongoClients.create(mongoConfig.getMongoUri());
    }
    final MongoDatabase db = mongoClient.getDatabase(mongoConfig.getDatabase());
    collection = db.getCollection(mongoConfig.getCollection());
    incomingList = Lists.newArrayList();
    flushExecutor = Executors.newScheduledThreadPool(1);
    flushExecutor.scheduleAtFixedRate(() -> flush(), mongoConfig.getBatchTimeMs(), mongoConfig.getBatchTimeMs(), TimeUnit.MILLISECONDS);
}
Also used : MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 8 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project pulsar by apache.

the class MongoSource method open.

@Override
public void open(Map<String, Object> config, SourceContext sourceContext) throws Exception {
    log.info("Open MongoDB Source");
    mongoConfig = MongoConfig.load(config);
    mongoConfig.validate(false, false);
    if (clientProvider != null) {
        mongoClient = clientProvider.get();
    } else {
        mongoClient = MongoClients.create(mongoConfig.getMongoUri());
    }
    if (StringUtils.isEmpty(mongoConfig.getDatabase())) {
        // Watch all databases
        log.info("Watch all");
        stream = mongoClient.watch();
    } else {
        final MongoDatabase db = mongoClient.getDatabase(mongoConfig.getDatabase());
        if (StringUtils.isEmpty(mongoConfig.getCollection())) {
            // Watch all collections in a database
            log.info("Watch db: {}", db.getName());
            stream = db.watch();
        } else {
            // Watch a collection
            final MongoCollection<Document> collection = db.getCollection(mongoConfig.getCollection());
            log.info("Watch collection: {} {}", db.getName(), mongoConfig.getCollection());
            stream = collection.watch();
        }
    }
    stream.batchSize(mongoConfig.getBatchSize()).fullDocument(FullDocument.UPDATE_LOOKUP);
    stream.subscribe(new Subscriber<ChangeStreamDocument<Document>>() {

        private ObjectMapper mapper = new ObjectMapper();

        private Subscription subscription;

        @Override
        public void onSubscribe(Subscription subscription) {
            this.subscription = subscription;
            this.subscription.request(Integer.MAX_VALUE);
        }

        @Override
        public void onNext(ChangeStreamDocument<Document> doc) {
            try {
                log.info("New change doc: {}", doc);
                // Build a record with the essential information
                final Map<String, Object> recordValue = new HashMap<>();
                recordValue.put("fullDocument", doc.getFullDocument());
                recordValue.put("ns", doc.getNamespace());
                recordValue.put("operation", doc.getOperationType());
                consume(new DocRecord(Optional.of(doc.getDocumentKey().toJson()), mapper.writeValueAsString(recordValue).getBytes(StandardCharsets.UTF_8)));
            } catch (JsonProcessingException e) {
                log.error("Processing doc from mongo", e);
            }
        }

        @Override
        public void onError(Throwable error) {
            log.error("Subscriber error", error);
        }

        @Override
        public void onComplete() {
            log.info("Subscriber complete");
        }
    });
}
Also used : ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) FullDocument(com.mongodb.client.model.changestream.FullDocument) Subscription(org.reactivestreams.Subscription) HashMap(java.util.HashMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 9 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project MongoSK by Romitou.

the class ExprMongoDatabase method get.

@Override
protected MongoSKDatabase[] get(@Nonnull final Event e) {
    String databaseName = exprDatabaseName.getSingle(e);
    MongoSKServer mongoSKServer = exprMongoSKServer.getSingle(e);
    if (databaseName == null || mongoSKServer == null)
        return new MongoSKDatabase[0];
    MongoDatabase mongoDatabase = mongoSKServer.getMongoClient().getDatabase(databaseName);
    if (mongoDatabase == null)
        return new MongoSKDatabase[0];
    return new MongoSKDatabase[] { new MongoSKDatabase(mongoDatabase) };
}
Also used : MongoSKServer(fr.romitou.mongosk.elements.MongoSKServer) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) MongoSKDatabase(fr.romitou.mongosk.elements.MongoSKDatabase)

Example 10 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project ditto by eclipse.

the class ThingPersistenceOperationsActor method props.

/**
 * Create Props of this actor.
 *
 * @param pubSubMediator Akka pub-sub mediator.
 * @param mongoDbConfig the MongoDB configuration settings.
 * @param config Configuration with info about event journal, snapshot store and database.
 * @param persistenceOperationsConfig the persistence operations config.
 * @return a Props object.
 */
public static Props props(final ActorRef pubSubMediator, final MongoDbConfig mongoDbConfig, final Config config, final PersistenceOperationsConfig persistenceOperationsConfig) {
    return Props.create(ThingPersistenceOperationsActor.class, () -> {
        final MongoEventSourceSettings eventSourceSettings = MongoEventSourceSettings.fromConfig(config, ThingPersistenceActor.PERSISTENCE_ID_PREFIX, true, ThingPersistenceActor.JOURNAL_PLUGIN_ID, ThingPersistenceActor.SNAPSHOT_PLUGIN_ID);
        final MongoClientWrapper mongoClient = MongoClientWrapper.newInstance(mongoDbConfig);
        final MongoDatabase db = mongoClient.getDefaultDatabase();
        final NamespacePersistenceOperations namespaceOps = MongoNamespacePersistenceOperations.of(db, eventSourceSettings);
        return new ThingPersistenceOperationsActor(pubSubMediator, namespaceOps, mongoClient, persistenceOperationsConfig);
    });
}
Also used : MongoNamespacePersistenceOperations(org.eclipse.ditto.internal.utils.persistence.mongo.ops.eventsource.MongoNamespacePersistenceOperations) NamespacePersistenceOperations(org.eclipse.ditto.internal.utils.persistence.operations.NamespacePersistenceOperations) MongoEventSourceSettings(org.eclipse.ditto.internal.utils.persistence.mongo.ops.eventsource.MongoEventSourceSettings) MongoClientWrapper(org.eclipse.ditto.internal.utils.persistence.mongo.MongoClientWrapper) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Aggregations

MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)32 Document (org.bson.Document)11 MongoClient (com.mongodb.reactivestreams.client.MongoClient)9 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)8 DeleteResult (com.mongodb.client.result.DeleteResult)6 List (java.util.List)6 BsonDocument (org.bson.BsonDocument)5 MongoClientSettings (com.mongodb.MongoClientSettings)4 MongoException (com.mongodb.MongoException)4 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)4 MongoClients (com.mongodb.reactivestreams.client.MongoClients)4 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)4 Publisher (org.reactivestreams.Publisher)4 WriteConcern (com.mongodb.WriteConcern)3 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)3 ReactiveMongoDatabaseFactory (io.micronaut.data.mongodb.database.ReactiveMongoDatabaseFactory)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 Map (java.util.Map)3 Objects (java.util.Objects)3