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);
});
}
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());
}
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");
}
});
}
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);
}
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");
}
Aggregations