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.crosstreelabs.cognitio.service.mongo.provider.MongoClientProvider.java

License:Apache License

@Override
public MongoClient get() {
    List<ServerAddress> seeds = getSeeds();
    if (seeds.size() == 1) {
        return new MongoClient(seeds.get(0));
    }//from  w w w.  j av  a  2  s .  co  m
    return new MongoClient(seeds);
}

From source file:com.datatorrent.contrib.frauddetect.operator.MongoDBOutputOperator.java

License:Open Source License

@Override
public void setup(Context.OperatorContext context) {
    super.setup(context);
    try {/*from   ww  w .j a  v a2  s  . c  o m*/
        mongoClient = new MongoClient(hostName);
        db = mongoClient.getDB(dataBase);
        if (userName != null && passWord != null) {
            if (!db.authenticate(userName, passWord.toCharArray())) {
                throw new IllegalArgumentException(
                        "MongoDB authentication failed. Illegal username and password for MongoDB!!");
            }
        }
        dbCollection = db.getCollection(collection);
    } catch (UnknownHostException ex) {
        logger.debug(ex.toString());
    }
}

From source file:com.datatorrent.contrib.mongodb.MongoDBConnectable.java

License:Open Source License

@Override
public void connect() {
    try {/*www.  j ava2s.  co m*/
        mongoClient = new MongoClient(hostName);
        db = mongoClient.getDB(dataBase);
        if (userName != null && passWord != null) {
            db.authenticate(userName, passWord.toCharArray());
        }
    } catch (UnknownHostException ex) {
        throw new RuntimeException("creating mongodb client", ex);
    }
}

From source file:com.datatorrent.contrib.mongodb.MongoDBInputOperator.java

License:Open Source License

@Override
public void beginWindow(long windowId) {
    try {//from  w ww . ja  va  2  s  .  c o  m
        mongoClient = new MongoClient(hostName);
        db = mongoClient.getDB(dataBase);
        if (userName != null && passWord != null) {
            db.authenticate(userName, passWord.toCharArray());
        }
    } catch (UnknownHostException ex) {
        logger.debug(ex.toString());
    }

}

From source file:com.datatorrent.contrib.mongodb.MongoDBOperatorBase.java

License:Open Source License

@Override
public void setupDbConnection() {
    try {// w  w  w.j a  va  2s.  c o  m
        mongoClient = new MongoClient(hostName);
        db = mongoClient.getDB(dataBase);
        if (userName != null && passWord != null) {
            db.authenticate(userName, passWord.toCharArray());
        }
    } catch (UnknownHostException ex) {
        throw new RuntimeException("creating mongodb client", ex);
    }
}

From source file:com.datatorrent.contrib.mongodb.MongoDBOutputOperator.java

License:Open Source License

/**
 * At setup time, init last completed windowId from maxWindowTable
 *
 * @param context//from  w w w . j  a v  a  2  s.  c o m
 */
@Override
public void setup(OperatorContext context) {
    operatorId = context.getId();
    try {
        mongoClient = new MongoClient(hostName);
        db = mongoClient.getDB(dataBase);
        if (userName != null && passWord != null) {
            db.authenticate(userName, passWord.toCharArray());
        }
        initLastWindowInfo();
        for (String table : tableList) {
            tableToDocumentList.put(table, new ArrayList<DBObject>());
            tableToDocument.put(table, new BasicDBObject());
        }
    } catch (UnknownHostException ex) {
        logger.debug(ex.toString());
    }
}

From source file:com.davidsalter.cappedcollection.JavaDriverCappedCollection.java

License:Apache License

/**
 * @param args the command line arguments
 *//*www .  jav  a 2s. c  om*/
public static void main(String[] args) throws UnknownHostException {
    MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost"));
    DB db = mongoClient.getDB("test");

    DBCollection collection;
    if (!db.collectionExists("cappedLogsJavaDriver")) {
        BasicDBObject options = new BasicDBObject("capped", true);
        options.append("size", 4096);
        options.append("max", 5);
        collection = db.createCollection("cappedLogsJavaDriver", options);
    } else {
        collection = db.getCollection("cappedLogsJavaDriver");
    }

    for (int i = 0; i < 8; i++) {
        BasicDBObject logEntry = new BasicDBObject("logId", i);
        collection.insert(logEntry);
    }
}

From source file:com.davidsalter.cappedcollection.MorphiaCappedCollection.java

License:Apache License

/**
 * @param args the command line arguments
 *//*from  ww w.j a  va  2 s  .  c o m*/
public static void main(String[] args) throws UnknownHostException {
    MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost"));
    DB db = mongoClient.getDB("test");

    Morphia morphia = new Morphia();
    morphia.map(LogEntry.class);

    Datastore datastore = morphia.createDatastore(mongoClient, "test");
    datastore.ensureCaps();

    for (int i = 0; i < 8; i++) {
        LogEntry logEntry = new LogEntry(i);
        datastore.save(logEntry);
    }
}

From source file:com.dawsonsystems.session.MongoSessionManager.java

License:Apache License

@SuppressWarnings("deprecation")
private void initDbConnection() throws LifecycleException {
    try {//w ww.  j  a  v  a  2  s.  c o  m
        String[] hosts = getHost().split(",");

        List<ServerAddress> addrs = new ArrayList<ServerAddress>();

        for (String host : hosts) {
            addrs.add(new ServerAddress(host, getPort()));
        }
        mongo = new MongoClient(addrs);
        db = mongo.getDB(getDatabase());
        if (slaveOk) {
            db.slaveOk();
        }
        getCollection().ensureIndex(new BasicDBObject("lastmodified", 1));
        log.info("Connected to Mongo " + host + "/" + database + " for session storage, slaveOk=" + slaveOk
                + ", " + (getMaxInactiveInterval() * 1000) + " session live time");
    } catch (IOException e) {
        e.printStackTrace();
        throw new LifecycleException("Error Connecting to Mongo", e);
    }
}

From source file:com.deftlabs.lock.mongo.impl.SvcImpl.java

License:Apache License

/**
 * Initialize the service./*from   ww w. j  a v  a2s  .  c om*/
 */
@Override
public void startup() {
    if (!_running.compareAndSet(false, true))
        throw new IllegalStateException("startup called but already running");

    _lock.lock();
    try {
        if (_options.getMongoClient() == null) {
            _mongo = new MongoClient(new MongoClientURI(_options.getMongoUri()));
        } else {
            _mongo = _options.getMongoClient();
        }

        // Init the db/collection.
        LockDao.setup(_mongo, _options);
        if (_options.getEnableHistory())
            LockHistoryDao.setup(_mongo, _options);

        // Init the monitor threads.
        _lockHeartbeat = new Monitor.LockHeartbeat(_mongo, _options, _locks);
        _lockHeartbeat.start();

        _lockTimeout = new Monitor.LockTimeout(_mongo, _options);
        _lockTimeout.start();

        _lockUnlocked = new Monitor.LockUnlocked(_mongo, _options, _locks);
        _lockUnlocked.start();

    } catch (final Throwable t) {
        throw new DistributedLockException(t);
    } finally {
        _lock.unlock();
    }
}