Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

In this page you can find the example usage for com.mongodb MongoClient MongoClient.

Prototype

public MongoClient(final MongoClientURI uri) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:com.ai.tris.server.db.mongodb.TrisMongoClient.java

License:Apache License

/**
 * Create mongo database access client. If Client uri initializes failed or just is empty,
 * give up the creation./*from  w  w w .  ja v a2 s  .  c  om*/
 *
 * @return mongo database client.
 */
private static MongoClient createMongoClient() {
    if (StringUtils.isEmpty(clientUri)) {
        throw new RuntimeException("Give up trying to create a client using empty uri.");
    }
    return new MongoClient(new MongoClientURI(clientUri));
}

From source file:com.Aleksandar.Zoric.MongoMain.java

public String mapReduceFunction() {
    MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
    DB db = mongoClient.getDB("amarokforumdb");
    DBCollection collection = db.getCollection("comments");
    long count = db.getCollection("comments").count();

    System.out.println("Current amount of documents: " + count);

    String map = "function() { " + "var category; " + "var numOfDocuments = " + count + ";"
            + "for(i = 0; i < numOfDocuments; i++){ " + "if (numOfDocuments <= 100) {"
            + "category = 'New Comments'; }" + "else if(numOfDocuments > 100){"
            + "category = 'Old Comments'; }}" + "emit(category,1);};";

    String reduce = "function(key, values) { " + "var sum = 0; " + "values.forEach(function(doc) { "
            + "sum += 1; " + "}); " + "return {comments: sum};} ";

    MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce, null,
            MapReduceCommand.OutputType.INLINE, null);

    MapReduceOutput out = collection.mapReduce(cmd);

    System.out.println("Mapreduce results");

    String result = null;/*from w ww .  ja v  a2 s  . c  o m*/

    for (DBObject o : out.results()) {
        result += o;

    }
    return result;
}

From source file:com.AlertMailerWebPage.servlet.MCRMData.java

public Document getMCRMObject(String database, String collection, String serverAddress, String email) {
    MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress, 27017));
    MongoDatabase db = mongoClient.getDatabase(database);

    FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email))
            .projection(new Document("email", 0).append("_id", 0));
    iterable.forEach(new Block<Document>() {
        @Override//from ww w .jav a2 s .c  o  m
        public void apply(final Document document) {
            MCRMData.document = document;
        }
    });
    return document;
}

From source file:com.AlertMailerWebPage.servlet.MCRMData.java

public void getSL_VM(String database, String collection, String serverAddress, String email) {
    MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress, 27017));
    MongoDatabase db = mongoClient.getDatabase(database);

    FindIterable<Document> iterable = db.getCollection(collection).find(new Document("email", email))
            .sort(new Document("_id", -1)).limit(1);
    iterable.forEach(new Block<Document>() {
        @Override/*  www.ja v a2 s .c  om*/
        public void apply(final Document document) {
            SL = (String) document.get("subject");
            VM = (String) document.get("vmfilename");
        }
    });

}

From source file:com.apifest.oauth20.MongoUtil.java

License:Apache License

public static MongoClient getMongoClient() {
    if (mongoClient == null) {
        try {/*w  w  w . ja  v a  2  s  .c om*/
            MongoClientOptions.Builder options = new MongoClientOptions.Builder().connectionsPerHost(100)
                    .connectTimeout(2).threadsAllowedToBlockForConnectionMultiplier(1);
            final MongoClientURI mongoClientURI = new MongoClientURI(OAuthServer.getDbURI(), options);
            mongoClient = new MongoClient(mongoClientURI);

            if (mongoClientURI.getDatabase() != null) {
                database = mongoClientURI.getDatabase();
            }
        } catch (UnknownHostException e) {
            log.error("Cannot connect to DB", e);
        }
    }
    return mongoClient;
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoUtil.java

License:Apache License

public static MongoClient getMongoClient(String uri) {
    if (mongoClient == null) {
        try {//from   w ww . j  a v a2 s.  co m
            MongoClientOptions.Builder options = new MongoClientOptions.Builder().connectionsPerHost(100)
                    .connectTimeout(2000).threadsAllowedToBlockForConnectionMultiplier(1);
            final MongoClientURI mongoClientURI = new MongoClientURI(uri, options);
            mongoClient = new MongoClient(mongoClientURI);

            if (mongoClientURI.getDatabase() != null) {
                database = mongoClientURI.getDatabase();
            }
        } catch (UnknownHostException e) {
            log.error("Cannot connect to DB", e);
        }
    }
    return mongoClient;
}

From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java

License:Apache License

private MongoClient buildMongoClient(Configuration config, List<MongoCredential> credentials,
        MongoClientOptions options) {//  ww  w .  j a  v  a  2  s .c  o  m
    List<ServerAddress> seeds = Lists.newArrayList();
    for (Server server : config.getServers()) {
        seeds.add(new ServerAddress(server.getHost(), server.getPort()));
    }
    if (options == null && credentials.size() == 0) {
        mongoClient = new MongoClient(seeds);
    } else if (options == null && credentials.size() > 0) {
        mongoClient = new MongoClient(seeds, credentials);
    } else if (options != null && credentials.size() == 0) {
        mongoClient = new MongoClient(seeds, options);
    } else {
        mongoClient = new MongoClient(seeds, credentials, options);
    }
    return mongoClient;
}

From source file:com.baifendian.swordfish.common.job.struct.datasource.MongoDatasource.java

License:Apache License

@Override
public void isConnectable() throws Exception {
    MongoClient mongoClient = new MongoClient(new MongoClientURI(this.address));
    try {//from   w w w .  ja v  a2s. c  om
        MongoClientOptions options = MongoClientOptions.builder().connectTimeout(10).socketKeepAlive(false)
                .build();

        MongoDatabase db = mongoClient.getDatabase(this.database);
        for (Document doc : db.listCollections()) {
            logger.debug("{}", doc);
        }
    } finally {
        mongoClient.close();
    }
}

From source file:com.bancvue.mongomigrate.MigrationHistoryRepository.java

License:Apache License

public MigrationHistoryRepository(String host, String databaseName) throws UnknownHostException {
    this.databaseName = databaseName;
    MongoClient mongoClient = new MongoClient(host);
    DB db = mongoClient.getDB(databaseName);
    DBCollection dbCollection = db.getCollection("MigrationHistory");
    collection = JacksonDBCollection.wrap(dbCollection, Migration.class, String.class);
}

From source file:com.bilko.controller.BlogController.java

License:Apache License

private BlogController(final String uri) throws IOException {
    final MongoDatabase db = new MongoClient(new MongoClientURI(uri)).getDatabase("blog");

    config = createFreemarkerConfiguration();
    userDao = new UserDao(db);
    sessionDao = new SessionDao(db);
    blogPostDao = new BlogPostDao(db);

    port(8082);/*w w w  . j av a2s .  co m*/
    initRoutes();
}