Search in sources :

Example 11 with MongoDatabase

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

the class PolicyPersistenceOperationsActor 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 configuration settings.
 * @return a Props object.
 */
public static Props props(final ActorRef pubSubMediator, final MongoDbConfig mongoDbConfig, final Config config, final PersistenceOperationsConfig persistenceOperationsConfig) {
    return Props.create(PolicyPersistenceOperationsActor.class, () -> {
        final MongoEventSourceSettings eventSourceSettings = MongoEventSourceSettings.fromConfig(config, PolicyPersistenceActor.PERSISTENCE_ID_PREFIX, true, PolicyPersistenceActor.JOURNAL_PLUGIN_ID, PolicyPersistenceActor.SNAPSHOT_PLUGIN_ID);
        final MongoClientWrapper mongoClient = MongoClientWrapper.newInstance(mongoDbConfig);
        final MongoDatabase db = mongoClient.getDefaultDatabase();
        final NamespacePersistenceOperations namespaceOps = MongoNamespacePersistenceOperations.of(db, eventSourceSettings);
        final EntityPersistenceOperations entitiesOps = MongoEntitiesPersistenceOperations.of(db, eventSourceSettings);
        return new PolicyPersistenceOperationsActor(pubSubMediator, namespaceOps, entitiesOps, mongoClient, persistenceOperationsConfig);
    });
}
Also used : MongoNamespacePersistenceOperations(org.eclipse.ditto.internal.utils.persistence.mongo.ops.eventsource.MongoNamespacePersistenceOperations) NamespacePersistenceOperations(org.eclipse.ditto.internal.utils.persistence.operations.NamespacePersistenceOperations) EntityPersistenceOperations(org.eclipse.ditto.internal.utils.persistence.operations.EntityPersistenceOperations) 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)

Example 12 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project helidon by oracle.

the class MongoDbClientTest method testUnwrapClientClass.

@Test
void testUnwrapClientClass() {
    MongoDbClient dbClient = new MongoDbClient(new MongoDbClientProviderBuilder(), CLIENT, DB);
    Single<MongoClient> future = dbClient.unwrap(MongoClient.class);
    MongoClient connection = future.await();
    assertThat(connection, notNullValue());
    Single<MongoDatabase> dbFuture = dbClient.unwrap(MongoDatabase.class);
    MongoDatabase db = dbFuture.await();
    assertThat(db, notNullValue());
}
Also used : MongoClient(com.mongodb.reactivestreams.client.MongoClient) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Example 13 with MongoDatabase

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

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 14 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project mongo-java-driver by mongodb.

the class ConnectionsSurvivePrimaryStepDownProseTest method setUp.

@Before
public void setUp() {
    assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(4, 0));
    connectionPoolListener = new TestConnectionPoolListener();
    MongoClientSettings settings = MongoClientSettings.builder(getMongoClientSettings()).retryWrites(false).applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(connectionPoolListener)).build();
    collectionHelper = new CollectionHelper<>(new DocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
    client = MongoClients.create(settings);
    MongoDatabase database = client.getDatabase(getDefaultDatabaseName());
    collection = client.getDatabase(getDefaultDatabaseName()).getCollection(COLLECTION_NAME);
    Mono.from(collection.withWriteConcern(WriteConcern.MAJORITY).drop()).block(TIMEOUT_DURATION);
    Mono.from(database.withWriteConcern(WriteConcern.MAJORITY).createCollection(COLLECTION_NAME)).block(TIMEOUT_DURATION);
}
Also used : Document(org.bson.Document) ClusterFixture.isDiscoverableReplicaSet(com.mongodb.ClusterFixture.isDiscoverableReplicaSet) Assume.assumeFalse(org.junit.Assume.assumeFalse) Fixture.getMongoClientSettings(com.mongodb.reactivestreams.client.Fixture.getMongoClientSettings) MongoClients(com.mongodb.reactivestreams.client.MongoClients) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) MongoClient(com.mongodb.reactivestreams.client.MongoClient) TIMEOUT_DURATION(com.mongodb.ClusterFixture.TIMEOUT_DURATION) Collections.singletonList(java.util.Collections.singletonList) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) DocumentCodec(org.bson.codecs.DocumentCodec) Assert.fail(org.junit.Assert.fail) MongoNotPrimaryException(com.mongodb.MongoNotPrimaryException) Before(org.junit.Before) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoNamespace(com.mongodb.MongoNamespace) MongoException(com.mongodb.MongoException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) ClusterFixture.getDefaultDatabaseName(com.mongodb.ClusterFixture.getDefaultDatabaseName) List(java.util.List) ClusterFixture.serverVersionAtLeast(com.mongodb.ClusterFixture.serverVersionAtLeast) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) WriteConcern(com.mongodb.WriteConcern) Assert.assertEquals(org.junit.Assert.assertEquals) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) DocumentCodec(org.bson.codecs.DocumentCodec) Fixture.getMongoClientSettings(com.mongodb.reactivestreams.client.Fixture.getMongoClientSettings) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) Before(org.junit.Before)

Example 15 with MongoDatabase

use of com.mongodb.reactivestreams.client.MongoDatabase in project mongo-java-driver by mongodb.

the class GridFSTour method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 * @throws FileNotFoundException if the sample file cannot be found
 * @throws IOException if there was an exception closing an input stream
 */
public static void main(final String[] args) throws FileNotFoundException, IOException {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = MongoClients.create();
    } else {
        mongoClient = MongoClients.create(args[0]);
    }
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    ObservableSubscriber<Void> dropSubscriber = new OperationSubscriber<>();
    database.drop().subscribe(dropSubscriber);
    dropSubscriber.await();
    GridFSBucket gridFSBucket = GridFSBuckets.create(database);
    /*
         * UploadFromPublisher Example
         */
    // Get the input publisher
    Publisher<ByteBuffer> publisherToUploadFrom = toPublisher(ByteBuffer.wrap("MongoDB Tutorial..".getBytes(StandardCharsets.UTF_8)));
    // Create some custom options
    GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024).metadata(new Document("type", "presentation"));
    ObservableSubscriber<ObjectId> uploadSubscriber = new OperationSubscriber<>();
    gridFSBucket.uploadFromPublisher("mongodb-tutorial", publisherToUploadFrom, options).subscribe(uploadSubscriber);
    ObjectId fileId = uploadSubscriber.get().get(0);
    /*
         * Find documents
         */
    System.out.println("File names:");
    ConsumerSubscriber<GridFSFile> filesSubscriber = new ConsumerSubscriber<>(gridFSFile -> System.out.println(" - " + gridFSFile.getFilename()));
    gridFSBucket.find().subscribe(filesSubscriber);
    filesSubscriber.await();
    /*
         * Find documents with a filter
         */
    filesSubscriber = new ConsumerSubscriber<>(gridFSFile -> System.out.println("Found: " + gridFSFile.getFilename()));
    gridFSBucket.find(eq("metadata.contentType", "image/png")).subscribe(filesSubscriber);
    filesSubscriber.await();
    /*
         * DownloadToPublisher
         */
    ObservableSubscriber<ByteBuffer> downloadSubscriber = new OperationSubscriber<>();
    gridFSBucket.downloadToPublisher(fileId).subscribe(downloadSubscriber);
    Integer size = downloadSubscriber.get().stream().map(Buffer::limit).reduce(0, Integer::sum);
    System.out.println("downloaded file sized: " + size);
    /*
         * DownloadToStreamByName
         */
    GridFSDownloadOptions downloadOptions = new GridFSDownloadOptions().revision(0);
    downloadSubscriber = new OperationSubscriber<>();
    gridFSBucket.downloadToPublisher("mongodb-tutorial", downloadOptions).subscribe(downloadSubscriber);
    size = downloadSubscriber.get().stream().map(Buffer::limit).reduce(0, Integer::sum);
    System.out.println("downloaded file sized: " + size);
    /*
         * Rename
         */
    OperationSubscriber<Void> successSubscriber = new OperationSubscriber<>();
    gridFSBucket.rename(fileId, "mongodbTutorial").subscribe(successSubscriber);
    successSubscriber.await();
    System.out.println("Renamed file");
    /*
         * Delete
         */
    successSubscriber = new OperationSubscriber<>();
    gridFSBucket.delete(fileId).subscribe(successSubscriber);
    successSubscriber.await();
    System.out.println("Deleted file");
    // Final cleanup
    successSubscriber = new OperationSubscriber<>();
    database.drop().subscribe(successSubscriber);
    successSubscriber.await();
    System.out.println("Finished");
}
Also used : Document(org.bson.Document) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Publisher(org.reactivestreams.Publisher) ConsumerSubscriber(reactivestreams.helpers.SubscriberHelpers.ConsumerSubscriber) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) MongoClients(com.mongodb.reactivestreams.client.MongoClients) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) MongoClient(com.mongodb.reactivestreams.client.MongoClient) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions) GridFSBuckets(com.mongodb.reactivestreams.client.gridfs.GridFSBuckets) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions) GridFSBucket(com.mongodb.reactivestreams.client.gridfs.GridFSBucket) ObservableSubscriber(reactivestreams.helpers.SubscriberHelpers.ObservableSubscriber) Buffer(java.nio.Buffer) PublisherHelpers.toPublisher(reactivestreams.helpers.PublisherHelpers.toPublisher) ObjectId(org.bson.types.ObjectId) Filters.eq(com.mongodb.client.model.Filters.eq) ByteBuffer(java.nio.ByteBuffer) Buffer(java.nio.Buffer) ObjectId(org.bson.types.ObjectId) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Document(org.bson.Document) ByteBuffer(java.nio.ByteBuffer) GridFSBucket(com.mongodb.reactivestreams.client.gridfs.GridFSBucket) MongoClient(com.mongodb.reactivestreams.client.MongoClient) ConsumerSubscriber(reactivestreams.helpers.SubscriberHelpers.ConsumerSubscriber) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions)

Aggregations

MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)38 Document (org.bson.Document)15 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 Map (java.util.Map)6 HashMap (java.util.HashMap)5 BsonDocument (org.bson.BsonDocument)5 MongoClientSettings (com.mongodb.MongoClientSettings)4 MongoException (com.mongodb.MongoException)4 InsertOneResult (com.mongodb.client.result.InsertOneResult)4 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)4 Publisher (org.reactivestreams.Publisher)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 WriteConcern (com.mongodb.WriteConcern)3 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)3 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)3 MongoClients (com.mongodb.reactivestreams.client.MongoClients)3