Example usage for com.mongodb.async.client MongoDatabase getCollection

List of usage examples for com.mongodb.async.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.async.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:com.example.App.java

License:Apache License

public static void main(String[] args) throws InterruptedException {
    final MongoClientSettings settings = MongoClientSettings.builder()
            .addCommandListener(new CommandListener() {
                @Override//from ww  w. ja  v a2s. c om
                public void commandStarted(CommandStartedEvent event) {
                    logger.info("command: db = {}, command = {}", event.getDatabaseName(),
                            event.getCommandName());
                }

                @Override
                public void commandSucceeded(CommandSucceededEvent event) {
                    logger.info("command succeed: request = {}, command = {}", event.getRequestId(),
                            event.getCommandName());
                }

                @Override
                public void commandFailed(CommandFailedEvent event) {
                    logger.info("command failed: request = {}, command = {}", event.getRequestId(),
                            event.getCommandName());
                    logger.error("detail", event.getThrowable());
                }
            }).applicationName("sample-app")
            .applyToConnectionPoolSettings(builder -> builder.maxSize(1).minSize(1)).build();

    final MongoClient client = MongoClients.create(settings);
    final MongoDatabase database = client.getDatabase("sample");
    final MongoCollection<Document> collection = database.getCollection("test");

    final Document firstDocument = new Document("id", UUID.randomUUID()).append("name", "test user")
            .append("created", LocalDateTime.now());

    logger.info("document to be saved: {}", firstDocument);

    final Mono<Document> firstMono = Mono.create(sink -> collection.insertOne(firstDocument, (result, t) -> {
        if (t == null) {
            logger.info("inserted: {}", firstDocument);
            sink.success(firstDocument);
        } else {
            logger.error("error", t);
            sink.error(t);
        }
    }));

    final Mono<List<Document>> secondMono = create100Users(collection, firstMono);

    final Mono<Long> thirdMono = secondMono.then(Mono.create(sink -> collection.countDocuments((count, t) -> {
        if (t == null) {
            logger.info("collection has {} items.", count);
            sink.success(count);
        } else {
            logger.error("error", t);
            sink.error(t);
        }
    })));

    final Mono<List<Document>> fourthMono = create100Users(collection, thirdMono);
    final Mono<List<Document>> fifthMono = create100Users(collection, fourthMono);
    final Mono<List<Document>> sixthMono = create100Users(collection, fifthMono);

    final Mono<Document> seventhMono = sixthMono.then(Mono.create(sink -> collection.find().first((doc, t) -> {
        if (t == null) {
            logger.info("found document: {}", doc);
            sink.success(doc);
        } else {
            logger.error("error", t);
            sink.error(t);
        }
    })));

    final CountDownLatch latch = new CountDownLatch(1);
    seventhMono.doOnTerminate(() -> {
        latch.countDown();
        client.close();
    }).subscribe(doc -> logger.info("first document: {}", doc));
    latch.await();
}

From source file:com.supermy.im.mongo.MongoRepository.java

License:Apache License

@Bean(name = "mongoCollection")
public MongoCollection mongoCollection() {
    System.out.println("*******************" + mycoll);

    final MongoDatabase mydb = mongoDatabase();

    /**/*from   w  ww . j a  v  a  2s  .com*/
     * ??,?;?;
     * 1???,?,????;
     */
    if (!collectionExists(mycoll)) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);//??

        mydb.createCollection(mycoll, new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                // logger.debug("?");

                MongoCollection coll = mydb.getCollection(mycoll);

                coll.createIndex(new Document("position", "2d"), new SingleResultCallback<Void>() {
                    @Override
                    public void onResult(final Void result, final Throwable t) {
                        // logger.debug("??");
                    }
                });

                //?
                Document expire = Document.parse("{time:1},{expireAfterSeconds:10*60}");
                coll.createIndex(expire, new SingleResultCallback<Void>() {
                    @Override
                    public void onResult(final Void result, final Throwable t) {
                        // logger.debug("??");
                    }
                });

                countDownLatch.countDown();
            }
        });
        try {
            countDownLatch.await(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    MongoCollection coll = mydb.getCollection(mycoll);
    // TODO: 16/5/10  ;?
    //> db.location.ensureIndex( {position: "2d"} )

    return coll;
}

From source file:com.supermy.im.mongo.MongoRepository.java

License:Apache License

@Bean(name = "mongoCollectionTask")
public MongoCollection mongoCollectionTask() {
    System.out.println("*******************" + mytasks);

    MongoDatabase mydb = mongoDatabase();
    MongoCollection coll = mydb.getCollection(mytasks);
    // TODO: 16/5/10  ;?
    //> db.location.ensureIndex( {position: "2d"} )

    return coll;/*  ww  w . j  av  a2 s . c o m*/
}

From source file:com.waves_rsp.ikb4stream.consumer.database.DatabaseReader.java

License:Open Source License

/**
 * The constructor of {@link DatabaseReader}
 * This class is a singleton//from   w  ww. j  ava2 s .  c om
 *
 * @see DatabaseReader#checkConfiguration()
 * @see DatabaseReader#PROPERTIES_MANAGER
 * @see DatabaseReader#mongoCollection
 * @see DatabaseReader#limit
 */
private DatabaseReader() {
    try {
        checkConfiguration();
        final MongoClient mongoClient = MongoClients.create(PROPERTIES_MANAGER.getProperty("database.host"));
        final MongoDatabase mongoDatabase = mongoClient
                .getDatabase(PROPERTIES_MANAGER.getProperty("database.datasource"));
        this.mongoCollection = mongoDatabase
                .getCollection(PROPERTIES_MANAGER.getProperty("database.collection"));
    } catch (IllegalArgumentException e) {
        LOGGER.error(e.getMessage());
        throw new IllegalStateException(e);
    }
    int tmp = 50000;
    try {
        tmp = Integer.parseInt(PROPERTIES_MANAGER.getProperty("database.limit"));
    } catch (IllegalArgumentException e) {
        LOGGER.warn("Use default database.limit");
    }
    this.limit = tmp;
    LOGGER.info("DatabaseReader has been instantiate");
}

From source file:com.waves_rsp.ikb4stream.producer.DatabaseWriter.java

License:Open Source License

/**
 * DataWriter constructor/*w  ww  .j  a  v a2 s. c  om*/
 *
 * @throws IllegalStateException if database configuration is not set
 */
private DatabaseWriter() {
    try {
        final MongoClient mongoClient = MongoClients.create(PROPERTIES_MANAGER.getProperty("database.host"));
        final MongoDatabase mongoDatabase = mongoClient
                .getDatabase(PROPERTIES_MANAGER.getProperty("database.datasource"));
        this.mongoCollection = mongoDatabase
                .getCollection(PROPERTIES_MANAGER.getProperty("database.collection"));
    } catch (IllegalArgumentException e) {
        LOGGER.error(e.getMessage());
        throw new IllegalStateException(e.getMessage());
    }
    LOGGER.info("DatabaseWriter has been instantiate");
}

From source file:net.modelbased.proasense.storage.writer.EventWriterMongoAsync.java

License:Apache License

public void run() {
    // Connect to MongoDB database
    MongoClient mongoClient = MongoClients.create(mongoURL);
    MongoDatabase database = mongoClient.getDatabase(EventProperties.STORAGE_DATABASE_NAME);

    // Create hash map of collections
    Map<String, MongoCollection<Document>> collectionMap = new HashMap<String, MongoCollection<Document>>();

    int cnt = 0;//from   w  w  w  .  j a v a 2s  . c  om
    long timer0 = System.currentTimeMillis();
    long timer1 = timer0;
    long timer2 = timer0;
    boolean skipLog = false;

    Map<String, List<Document>> documentMap = new HashMap<String, List<Document>>();
    try {
        if (isLogfile)
            logfileWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("EventWriterMongoAsync_benchmark_" + this.threadNumber + ".txt"),
                    "ISO-8859-1"));

        while (true) {
            long timeoutExpired = System.currentTimeMillis() + this.maxWait;
            EventDocument eventDocument = queue.take();

            String collectionId = eventDocument.getCollectionId();

            if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) {
                cnt++;
                skipLog = false;
            } else
                skipLog = true;

            // Add data for bulk write
            if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) {
                Document document = eventDocument.getDocument();

                if (!collectionMap.containsKey(collectionId)) {
                    collectionMap.put(collectionId, database.getCollection(collectionId));
                    List<Document> documentList = new ArrayList<Document>();
                    documentMap.put(collectionId, documentList);
                }

                documentMap.get(collectionId).add(document);
            }
            // Write data if heartbeat timeout is received
            else {
                for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) {
                    String key = entry.getKey();
                    if (!documentMap.get(key).isEmpty()) {
                        collectionMap.get(key).insertMany(documentMap.get(key),
                                new SingleResultCallback<Void>() {
                                    //                                @Override
                                    public void onResult(final Void result, final Throwable t) {
                                    }
                                });
                        documentMap.get(key).clear();
                    }
                }
            }

            // Write data if bulk size or max wait (ms) is reached
            if ((cnt % this.bulkSize == 0) || (System.currentTimeMillis() >= timeoutExpired)) {
                for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) {
                    String key = entry.getKey();
                    if (!documentMap.get(key).isEmpty()) {
                        collectionMap.get(key).insertMany(documentMap.get(key),
                                new SingleResultCallback<Void>() {
                                    //                                @Override
                                    public void onResult(final Void result, final Throwable t) {
                                    }
                                });
                        documentMap.get(key).clear();
                    }
                }
            }

            // Benchmark output
            if ((!skipLog) && (cnt % this.logSize == 0)) {
                timer2 = System.currentTimeMillis();
                long difference = timer2 - timer1;

                if (difference != 0) {
                    long average = (this.logSize * 1000) / (timer2 - timer1);

                    System.out.println("Benchmark: ");
                    System.out.println("  Records written  : " + cnt);
                    System.out.println("  Average records/s: " + average);
                    timer1 = timer2;

                    if (isLogfile) {
                        logfileWriter.write(cnt + "," + average + System.getProperty("line.separator"));
                        logfileWriter.flush();
                    }

                    if (cnt == this.loadTestMaxMessages) {
                        long loadTestStart = timer0 / 1000;
                        long loadTestEnd = timer2 / 1000;
                        long loadTestTime = loadTestEnd - loadTestStart;
                        long loadTestAverage = this.loadTestMaxMessages / loadTestTime;

                        System.out.println("*****************************");
                        System.out.println("Load test results: ");
                        System.out.println("  Records written  : " + cnt);
                        System.out.println("  Average records/s: " + loadTestAverage);

                        if (isLogfile) {
                            logfileWriter.write(
                                    "*****************************" + System.getProperty("line.separator"));
                            logfileWriter.write("Load test results: " + System.getProperty("line.separator"));
                            logfileWriter.write(
                                    "  Records written  : " + cnt + System.getProperty("line.separator"));
                            logfileWriter.write("  Average records/s: " + loadTestAverage
                                    + System.getProperty("line.separator"));
                            logfileWriter.flush();
                        }
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getClass().getName() + ": " + e.getMessage());
    } finally {
        if (isLogfile)
            try {
                logfileWriter.close();
            } catch (IOException e) {
                System.out.println(e.getClass().getName() + ": " + e.getMessage());
            }
    }

    mongoClient.close();

}

From source file:opensnap.repository.MongoRepository.java

License:Apache License

public MongoRepository(MongoDatabase db, ObjectMapper mapper, String collectionName, Class<T> clazz) {
    this.mapper = mapper;
    this.clazz = clazz;
    collection = db.getCollection(collectionName);
}

From source file:org.apache.pulsar.io.mongodb.MongoSink.java

License:Apache License

@Override
public void open(Map<String, Object> config, SinkContext sinkContext) throws Exception {
    log.info("Open MongoDB Sink");

    mongoConfig = MongoConfig.load(config);
    mongoConfig.validate();//from w ww  .j  av  a 2  s.c om

    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);
}