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.bluedragon.mongo.MongoDSN.java

License:Open Source License

public synchronized void open() throws Exception {

    if (mongouri == null) {

        if (clientMongoMap.containsKey(ip + port)) {
            mdb = clientMongoMap.get(ip + port).open().getDatabase(db);
        } else {//from  w  w w  .  j a v  a  2s  .  c om
            mongoclient = newClient(server + ":" + port, user, pass, db);
            MongoClientWrapper mcw = new MongoClientWrapper(mongoclient);
            clientMongoMap.put(ip + port, mcw);
            mdb = mcw.open().getDatabase(db);
        }

    } else {
        MongoClientURI clientURI = new MongoClientURI(mongouri);
        mongoclient = new MongoClient(clientURI);
        mdb = mongoclient.getDatabase(clientURI.getDatabase());
    }

    lastUsed = System.currentTimeMillis();
}

From source file:com.bluedragon.profiler.ProfilerExtension.java

License:Open Source License

public static void setMongo(String mongourl, String database, String collection) throws Exception {
    if (thisInst.mongourl != null && !thisInst.mongourl.equals(mongourl))
        return;//from  w  w w  . ja  va2 s .  c o  m

    thisInst.mongourl = mongourl;
    MongoClientURI m = new MongoClientURI(thisInst.mongourl);

    thisInst.mongo = new MongoClient(m);
    thisInst.coll = thisInst.mongo.getDB(database).getCollection(collection);

    Thread t = new Thread(thisInst);
    t.setName("ProfilerExtension:MongoThread");
    t.setPriority(Thread.MIN_PRIORITY);
    t.start();
}

From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

@Override
public Partition[] getPartitions(S config) {
    MongoClient mongoClient = null;//  w  w w  .j  a  v a2 s  .  c om

    try {

        mongoDeepJobConfig = initConfig(config, mongoDeepJobConfig);

        DBCollection collection;
        ServerAddress address = new ServerAddress(mongoDeepJobConfig.getHost());

        List<ServerAddress> addressList = new ArrayList<>();
        addressList.add(address);
        mongoClient = new MongoClient(addressList);

        //mongoClient.setReadPreference(ReadPreference.nearest());
        DB db = mongoClient.getDB(mongoDeepJobConfig.getDatabase());
        collection = db.getCollection(mongoDeepJobConfig.getCollection());
        return isShardedCollection(collection) ? calculateShardChunks(collection) : calculateSplits(collection);
    } catch (UnknownHostException e) {

        throw new GenericException(e);
    } finally {
        if (mongoClient != null) {
            mongoClient.close();
        }

    }
}

From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Gets split data collection shard enviroment.
 *
 * @param shards         the shards/*from www  .  j  a v  a  2s .  c om*/
 * @param dbName         the db name
 * @param collectionName the collection name
 * @return the split data collection shard enviroment
 */
private Pair<BasicDBList, List<ServerAddress>> getSplitDataCollectionShardEnviroment(
        Map<String, String[]> shards, String dbName, String collectionName) {
    MongoClient mongoClient = null;
    try {
        Set<String> keys = shards.keySet();

        for (String key : keys) {

            List<ServerAddress> addressList = getServerAddressList(Arrays.asList(shards.get(key)));

            mongoClient = new MongoClient(addressList);

            BasicDBList dbList = getSplitData(mongoClient.getDB(dbName).getCollection(collectionName));

            if (dbList != null) {
                return Pair.create(dbList, addressList);
            }
        }
    } catch (UnknownHostException e) {
        throw new GenericException(e);
    } finally {
        if (mongoClient != null) {
            mongoClient.close();
        }

    }

    return null;

}

From source file:com.bosscs.spark.mongodb.writer.MongoWriter.java

License:Apache License

/**
 * Instantiates a new Mongo writer.// w w w.jav  a 2 s  .  co m
 * 
 * @param serverAddresses
 *            the server addresses
 * @param databaseName
 *            the database name
 * @param collectionName
 *            the collection name
 */
public MongoWriter(List<ServerAddress> serverAddresses, String databaseName, String collectionName,
        WriteConcern writeConcern) {
    mongoClient = new MongoClient(serverAddresses);
    dbCollection = mongoClient.getDB(databaseName).getCollection(collectionName);
    this.writeConcern = writeConcern;
}

From source file:com.caci.dummyserver.MongoRepository.java

public MongoRepository(String serverName) throws Exception {
    if (mongo == null) {
        mongo = new MongoClient(serverName);
    }
}

From source file:com.callidusrobotics.droptables.configuration.MongoFactory.java

License:Open Source License

private MongoClient buildClient(Environment env, String username, String password) throws UnknownHostException {
    final MongoClient mongoClient;
    if (StringUtils.isBlank(username)) {
        mongoClient = new MongoClient(new ServerAddress(host, port));
    } else {/*  w  w w . java  2s .c  o  m*/
        char[] passwordChars = password == null ? new char[0] : password.toCharArray();
        mongoClient = new MongoClient(new ServerAddress(host, port),
                Arrays.asList(MongoCredential.createCredential(username, dbName, passwordChars)));
    }

    env.lifecycle().manage(new Managed() {
        @Override
        public void start() throws Exception {
        }

        @Override
        public void stop() throws Exception {
            mongoClient.close();
        }
    });

    return mongoClient;
}

From source file:com.cloudbees.demo.beesshop.util.MongodbDBFactoryBean.java

License:Apache License

@Override
public void afterPropertiesSet() throws Exception {
    MongoClientURI mongoDbUri = new MongoClientURI(uri);
    logger.info("host=" + mongoDbUri.getHosts() + ",username=" + mongoDbUri.getUsername() + ",database="
            + mongoDbUri.getDatabase() + ",collection=" + mongoDbUri.getCollection());

    mongoClient = new MongoClient(mongoDbUri);
    db = mongoClient.getDB(mongoDbUri.getDatabase());
}

From source file:com.cognifide.aet.vs.mongodb.MongoDBClient.java

License:Apache License

private void setupMongoDBConnection() throws UnknownHostException {
    mongoClient = new MongoClient(new MongoClientURI(mongoUri));
    testConnection();//ww  w  . ja va  2s.  com
    LOGGER.info("Mongo client connected at: " + mongoUri);
}

From source file:com.connection.Connection.java

private void establishConnection() {
    if (mongoClient == null) {
        MongoClientURI uri = new MongoClientURI("mongodb://352634:cs-120@ds023418.mlab.com:23418/example");
        try {// w  w w  .  ja  v  a2  s. c om
            mongoClient = new MongoClient(uri);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dbname = uri.getDatabase();
        //  mongoClient = new MongoClient("localhost", 27017);
        mongoDB = mongoClient.getDB(dbname);

    }
}