Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI, @Nullable final MongoDriverInformation mongoDriverInformation) 

Source Link

Usage

From source file:org.anyframe.logmanager.bundle.core.Activator.java

License:Apache License

/**
 * @param context//from   w w  w .ja va  2  s  . c  om
 * @throws Exception
 */
private void initMongo(BundleContext context) throws Exception {

    if (context == null) {
        throw new LogManagerBundleException("Context is null.");
    }

    String mongoSvr = context.getProperty("mongo.host");
    int mongoPort = Integer.parseInt(context.getProperty("mongo.port"));
    logger.info("MongoDB connect to - " + mongoSvr + ":" + mongoPort);

    MongoOptions options = new MongoOptions();
    options.connectionsPerHost = Integer.parseInt(context.getProperty("mongo.connectionsPerHost"));
    options.autoConnectRetry = Boolean.parseBoolean(context.getProperty("mongo.autoConnectRetry"));
    options.connectTimeout = Integer.parseInt(context.getProperty("mongo.connectTimeout"));
    options.threadsAllowedToBlockForConnectionMultiplier = Integer
            .parseInt(context.getProperty("mongo.threadsAllowedToBlockForConnectionMultiplier"));
    options.maxWaitTime = Integer.parseInt(context.getProperty("mongo.maxWaitTime"));
    options.connectTimeout = Integer.parseInt(context.getProperty("mongo.connectTimeout"));
    options.socketKeepAlive = Boolean.parseBoolean(context.getProperty("mongo.socketKeepAlive"));
    options.socketTimeout = Integer.parseInt(context.getProperty("mongo.socketTimeout"));

    ServerAddress addr = new ServerAddress(mongoSvr, mongoPort);

    mongo = new Mongo(addr, options);

    if (mongo != null) {
        logger.info("MongoDB connected - " + addr.getHost() + ":" + addr.getPort());
    }
}

From source file:org.apache.camel.processor.idempotent.mongodb.MongoDbIdempotentRepository.java

License:Apache License

public void init() {
    try {/*from w  w w  .  j ava  2  s.c o m*/
        Mongo mongo = new Mongo(host, port);
        db = mongo.getDB(dbName);
        coll = db.getCollection(collectionName);

        // create a unique index on event id key
        coll.ensureIndex(new BasicDBObject(EVENTID, 1), "repo_index", true);

        log.debug("unique index constraint --> " + coll.getIndexInfo());

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.cassandra.db.engine.MongoConfigure.java

License:Apache License

public DB connect(String dbName, String host, int port, String user, String pass) {
    try {/*  ww  w  . j a va  2s. co m*/
        Mongo mongo = new Mongo(host, port);
        DB db = mongo.getDB(dbName);
        if (user != null && pass != null)
            if (!db.authenticate(user, pass.toCharArray()))
                throw new Exception("authentication error!!");
        return db;
    } catch (Exception e) {
        System.err.println("can't connect MongoDB [host: " + host + " port:" + port + " user:" + user + "]");
        System.exit(1);
    }
    return null;
}

From source file:org.apache.hadoop.contrib.mongoreduce.MongoInputFormat.java

License:Apache License

@Override
public List<InputSplit> getSplits(JobContext context) throws IOException, InterruptedException {

    Configuration conf = context.getConfiguration();

    ArrayList<InputSplit> splits = new ArrayList<InputSplit>();

    // in single testing mode we just hit the local db
    boolean singleTestingMode = conf.getBoolean("mongo.single.testing", false);
    if (singleTestingMode) {
        String[] hosts = { "localhost:27017" };
        splits.add(new MongoInputSplit(hosts));

        return splits;
    }/*from w w w  . j a  va 2s  .c o  m*/

    boolean primaryOk = conf.getBoolean("mongo.input.primary_ok", false);

    // connect to global mongo through a mongos process
    Mongo m = new Mongo("localhost", 27017);

    // get a list of all shards and their hosts
    // TODO: add notification if config db not found / db is not sharded
    DB configdb = m.getDB("config");
    DBCollection shards = configdb.getCollection("shards");

    // we need to query/read/process each shard once
    for (DBObject shard : shards.find()) {

        System.out.println("adding shard" + shard.toString());

        String[] hosts = hostsForShard((String) shard.get("host"), primaryOk);
        for (String host : hosts)
            System.out.print(host + " ");
        System.out.println();

        InputSplit split = new MongoInputSplit(hosts);
        splits.add(split);
    }

    return splits;
}

From source file:org.apache.hadoop.contrib.mongoreduce.MongoRecordReader.java

License:Apache License

private void connect(String location, Configuration conf) throws IOException {

    String[] parts = location.split(":");

    // default port for sharded server
    int port = 27018;
    if (parts.length > 1)
        port = Integer.parseInt(parts[1]);

    Mongo mongo = new Mongo(parts[0], port);

    // figure out if we can read from this server

    // allow reading from secondaries
    mongo.slaveOk();//w  w  w .j av a  2 s . co m

    String database = conf.get("mongo.input.database");
    String collection = conf.get("mongo.input.collection");
    String query = conf.get("mongo.input.query", "");
    String select = conf.get("mongo.input.select", "");

    if (!query.equals("")) {
        DBObject q = (DBObject) JSON.parse(query);

        if (!select.equals("")) {
            DBObject s = (DBObject) JSON.parse(select);
            cursor = mongo.getDB(database).getCollection(collection).find(q, s);
        } else {
            cursor = mongo.getDB(database).getCollection(collection).find(q);
        }
    } else {
        if (!select.equals("")) {
            DBObject s = (DBObject) JSON.parse(select);
            cursor = mongo.getDB(database).getCollection(collection).find(new BasicDBObject(), s);
        } else {
            cursor = mongo.getDB(database).getCollection(collection).find();
        }
    }

    cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);

    // thanks mongo, for this handy method
    totalResults = cursor.count();
    resultsRead = 0.0f;

}

From source file:org.apache.hadoop.contrib.mongoreduce.MongoRecordWriter.java

License:Apache License

public MongoRecordWriter(String database, String collection) throws IOException {

    // connect to local mongos process
    try {/*from  w  w w  .  ja  v a2s  .com*/
        mongo = new Mongo("localhost", 27017);
        coll = mongo.getDB(database).getCollection(collection);

    } catch (UnknownHostException e) {

        e.printStackTrace();
        throw new IOException(e.getMessage());
    } catch (MongoException e) {

        e.printStackTrace();
        throw new IOException(e.getMessage());
    }

}

From source file:org.apache.hadoop.contrib.mongoreduce.MongoStreamInputFormat.java

License:Apache License

/**
 * almost identical to MongoInputFormat//from   w w w  .j ava2 s  .c  o m
 * 
 * just uses old API and returns Streaming Splits instead
 */
//@Override
public InputSplit[] getSplits(JobConf conf, int numsplits) throws IOException {

    ArrayList<InputSplit> splits = new ArrayList<InputSplit>();
    InputSplit[] ret;

    // in single testing mode we just hit the local db
    boolean singleTestingMode = conf.getBoolean("mongo.single.testing", false);
    if (singleTestingMode) {
        String[] hosts = { "localhost:27017" };
        splits.add(new MongoStreamInputSplit(hosts));

        ret = new InputSplit[1];
        return splits.toArray(ret);
    }

    boolean primaryOk = conf.getBoolean("mongo.input.primary_ok", false);

    // connect to global mongo through a mongos process
    Mongo m = new Mongo("localhost", 27017);

    // get a list of all shards and their hosts
    DB configdb = m.getDB("config");
    DBCollection shards = configdb.getCollection("shards");

    // we need to query/read/process each shard once
    for (DBObject shard : shards.find()) {

        System.out.println("adding shard" + shard.toString());

        String[] hosts = MongoInputFormat.hostsForShard((String) shard.get("host"), primaryOk);

        for (String h : hosts)
            System.out.println("host:" + h);

        InputSplit split = new MongoStreamInputSplit(hosts);
        splits.add(split);
    }

    ret = new InputSplit[splits.size()];
    return splits.toArray(ret);
}

From source file:org.apache.hadoop.contrib.mongoreduce.MongoStreamRecordWriter.java

License:Apache License

public MongoStreamRecordWriter(String database, String collection) throws IOException {
    // connect to local mongos process
    try {//  ww  w  .  ja  v  a 2  s  .  c o m
        mongo = new Mongo("localhost", 27017);
        coll = mongo.getDB(database).getCollection(collection);

    } catch (UnknownHostException e) {

        e.printStackTrace();
        throw new IOException(e.getMessage());
    } catch (MongoException e) {

        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}

From source file:org.apache.isis.objectstore.nosql.db.mongo.MongoDb.java

License:Apache License

@Override
public void open() {
    try {//from   w  ww  . j  a va  2s.  c  om
        if (mongo == null) {
            mongo = new Mongo(host, port);
            db = mongo.getDB(dbName);
            db.setWriteConcern(com.mongodb.WriteConcern.SAFE);
            LOG.info("opened database (" + dbName + "): " + mongo);
        } else {
            LOG.info(" using opened database " + db);
        }
    } catch (final UnknownHostException e) {
        throw new NoSqlStoreException(e);
    } catch (final MongoException e) {
        throw new NoSqlStoreException(e);
    }
}

From source file:org.apache.isis.runtimes.dflt.objectstores.nosql.db.mongo.MongoDb.java

License:Apache License

@Override
public void open() {
    try {// w  w w.  jav  a  2  s . c o m
        mongo = new Mongo(host, port);
        db = mongo.getDB(dbName);
        db.setWriteConcern(WriteConcern.STRICT);

        LOG.info("opened database (" + dbName + "): " + db);
    } catch (final UnknownHostException e) {
        throw new NoSqlStoreException(e);
    } catch (final MongoException e) {
        throw new NoSqlStoreException(e);
    }
}