Example usage for com.mongodb Mongo getDB

List of usage examples for com.mongodb Mongo getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

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();// www .  j a  v 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.MongoStreamInputFormat.java

License:Apache License

/**
 * almost identical to MongoInputFormat/*ww  w  .j ava2s .  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.MongoStreamOutputFormat.java

License:Apache License

public void checkOutputSpecs(FileSystem ignored, JobConf conf) throws IOException {

    if (conf.getBoolean("mongo.output.skip_splitting", false))
        return;//from  ww  w  .j a v a2  s .com

    String database = conf.get("mongo.output.database", "");
    if (database.equals("")) {
        throw new IOException("must specify a value for mongo.output.database");
    }

    String collection = conf.get("mongo.output.collection", "");
    if (collection.equals("")) {
        throw new IOException("must supply a value for mongo.output.collection");
    }

    // connect to global db
    Mongo m = new Mongo("localhost");
    DB db = m.getDB(database);
    DB admindb = m.getDB("admin");
    DB configdb = m.getDB("config");

    // optionally drop the existing collection
    boolean drop = conf.getBoolean("mongo.output.drop", false);
    DBCollection coll = db.getCollection(collection);
    if (drop) {
        coll.drop();
    } else {
        if (coll.count() > 0) {
            // don't shard an existing collection - may already be sharded ...
            return;
        }
    }

    // get a list of shards
    ArrayList<String> shards = new ArrayList<String>();
    for (DBObject s : configdb.getCollection("shards").find()) {
        shards.add((String) s.get("_id"));
    }

    if (shards.size() < 2) {
        // don't let's be silly
        return;
    }

    // shard the new output collection
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.add("enableSharding", database);
    admindb.command(builder.get());

    builder = new BasicDBObjectBuilder();
    builder.add("shardCollection", database + "." + collection);

    // just shard on _id - but user gets to decide what the _id is
    builder.add("key", new BasicDBObject("_id", 1));
    admindb.command(builder.get());

    // pre-split to get parallel writes
    // this http://www.mongodb.org/display/DOCS/Splitting+Chunks says 
    // balancer moving chunks should take 5 minutes ... too long
    // wonder if moveChunk command is faster
    // well we could do it anyway - the jobs that can benefit from it will

    // check for user-submitted splitPoints
    String[] splits;
    String splitString = conf.get("mongo.output.split_points", "");

    // generate our own split points if necessary
    if (splitString.equals("")) {
        long max = (long) Math.pow(93.0, 5.0);

        long step = max / shards.size();
        splits = new String[shards.size() - 1];

        // assume human readable keys
        for (int i = 0; i < shards.size() - 1; i++) {
            splits[i] = splitPointForLong(step * (i + 1));
        }
    } else {
        splits = splitString.split(",");
    }

    HashMap<String, Object> splitCmd = new HashMap<String, Object>();
    splitCmd.put("split", database + "." + collection);
    splitCmd.put("middle", "");

    HashMap<String, Object> moveCmd = new HashMap<String, Object>();
    moveCmd.put("moveChunk", database + "." + collection);
    moveCmd.put("find", "");
    moveCmd.put("to", "");

    // do the splitting and migrating
    // we assign chunks to shards in a round-robin manner
    int i = 0;
    for (String split : splits) {

        splitCmd.remove("middle");
        splitCmd.put("middle", new BasicDBObject("_id", split));

        // create new chunk
        admindb.command(new BasicDBObject(splitCmd));

        // move to shard
        moveCmd.remove("find");
        moveCmd.put("find", new BasicDBObject("_id", split));
        moveCmd.put("to", shards.get(i));

        admindb.command(new BasicDBObject(moveCmd));

        i = (i + 1) % shards.size();
    }
}

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

License:Apache License

public void installed() throws Exception {

    final Mongo m = new Mongo();

    for (final String s : m.getDatabaseNames()) {
        System.out.println(s);//  w  w w . j a va  2  s.  c o  m
    }

    /*
     * Mongo m = new Mongo( "localhost" ); Mongo m = new Mongo( "localhost"
     * , 27017 );
     */
    m.dropDatabase("mydb");

    System.out.println("\n...");
    for (final String s : m.getDatabaseNames()) {
        System.out.println(s);
    }

    final DB db = m.getDB("mydb");
    /*
     * DBCollection coll = db.getCollection("testCollection1"); coll =
     * db.getCollection("testCollection2");
     */

    final DBCollection coll = db.getCollection("testCollection1");

    final BasicDBObject doc = new BasicDBObject();

    doc.put("name", "MongoDB");
    doc.put("type", "database");
    doc.put("count", 1);

    final BasicDBObject info = new BasicDBObject();

    info.put("x", 203);
    info.put("y", 102);

    doc.put("info", info);

    coll.insert(doc);

    final Set<String> colls = db.getCollectionNames();

    for (final String s : colls) {
        System.out.println(s);
    }

}

From source file:org.apache.sling.mongodb.impl.MongoDBResourceProviderFactory.java

License:Apache License

@Activate
protected void activate(final Map<String, Object> props) throws Exception {
    final String[] roots = PropertiesUtil.toStringArray(props.get(ResourceProvider.ROOTS));
    if (roots == null || roots.length == 0) {
        throw new Exception("Roots configuration is missing.");
    }/*from  w  w w .ja v  a2 s .c o m*/
    if (roots.length > 1) {
        throw new Exception("Only a single root should be configured.");
    }
    if (roots[0] == null || roots[0].trim().length() == 0) {
        throw new Exception("Roots configuration is missing.");
    }
    final String host = PropertiesUtil.toString(props.get(PROP_HOST), DEFAULT_HOST);
    final int port = PropertiesUtil.toInteger(props.get(PROP_PORT), DEFAULT_PORT);
    final String db = PropertiesUtil.toString(props.get(PROP_DB), DEFAULT_DB);
    logger.info("Starting MongoDB resource provider with host={}, port={}, db={}",
            new Object[] { host, port, db });
    final DBAddress address = new DBAddress(host, port, db);
    final MongoOptions options = new MongoOptions();

    options.connectionsPerHost = PropertiesUtil.toInteger(props.get(PROP_NUM_CONNECTIONS),
            DEFAULT_NUMCONNECTIONS);
    options.threadsAllowedToBlockForConnectionMultiplier = PropertiesUtil
            .toInteger(props.get(PROP_THREAD_MULTIPLIER), DEFAULT_THREAD_MULTIPLIER);
    final Mongo m = new Mongo(address, options);

    final DB database = m.getDB(db);
    logger.info("Connected to database {}", database);

    this.context = new MongoDBContext(database, roots[0],
            PropertiesUtil.toStringArray(props.get(PROP_FILTER_COLLECTIONS)), this.eventAdmin);
}

From source file:org.apache.whirr.service.mongodb.MongoDBReplSetMemberClusterActionHandler.java

License:Apache License

@Override
protected void afterConfigure(ClusterActionEvent event) {
    ClusterSpec clusterSpec = event.getClusterSpec();
    Cluster cluster = event.getCluster();

    LOG.info("Configuring replica set members.");

    //Get all the instances that are marked as replica set members
    Set<String> replSetRoles = Sets.newHashSet(ROLE, MongoDBArbiterClusterActionHandler.ROLE);
    Set<Cluster.Instance> replSetInstances = cluster.getInstancesMatching(anyRoleIn(replSetRoles));
    //Just grab the first of these instances, use it to send the rs.initiate()
    Cluster.Instance setLeader = replSetInstances.iterator().next();

    try {/* www  .j  a  v  a  2s .  c om*/
        Configuration config = getConfiguration(clusterSpec);
        this.arbiterPort = config.getInt(MongoDBArbiterClusterActionHandler.CFG_KEY_PORT,
                MongoDBArbiterClusterActionHandler.PORT);
    } catch (IOException e) {
        this.arbiterPort = MongoDBArbiterClusterActionHandler.PORT;
    }

    Mongo mongo;
    DB db;

    try {
        // throws IOExc, UnknownHostExc:
        LOG.info(
                "Connecting to " + setLeader.getPublicAddress().getHostAddress() + " to initiate replica set.");
        mongo = new Mongo(setLeader.getPublicAddress().getHostAddress(), PORT);
        db = mongo.getDB("admin");
        if (this.authPassword != null && this.authUsername != null) {
            db.authenticate(this.authUsername, this.authPassword.toCharArray());
        }
    } catch (Exception e) {
        LOG.error("Unable to get public host address of replica set leader, " + e.getMessage());
        return;
    }

    try {
        BasicDBObject configObject = this.generateReplicaSetConfig(replSetInstances); // throws IOexc
        LOG.info("config object:" + configObject.toString());
        BasicDBObject commandInfo = new BasicDBObject("replSetInitiate", configObject);
        LOG.info("Sending rs.initiate() command");
        CommandResult initiateResult = db.command(commandInfo);
        LOG.info("Command Result: " + initiateResult.toString());
    } catch (IOException e) {
        LOG.error("Unable to get private host addresses of replica set members, " + e.getMessage());
    } finally {
        //TODO any cleanup?
    }

}

From source file:org.axonframework.common.mongo.AuthenticatingMongoTemplate.java

License:Apache License

/**
 * Initializes the MongoTemplate to connect using the given <code>mongo</code> instance and the database with given
 * <code>databaseName</code>. The given <code>userName</code> and <code>password</code>, when not
 * <code>null</code>, are used to authenticate against the database.
 *
 * @param mongo        The Mongo instance configured to connect to the Mongo Server
 * @param databaseName The name of the database containing the data
 * @param userName     The username to authenticate with. Use <code>null</code> to skip authentication
 * @param password     The password to authenticate with. Use <code>null</code> to skip authentication
 *///from   w ww.  jav  a 2  s.c o m
protected AuthenticatingMongoTemplate(Mongo mongo, String databaseName, String authenticationDatabaseName,
        String userName, char[] password) { // NOSONAR
    this.database = mongo.getDB(databaseName);
    this.authenticationDatabase = databaseName.equals(authenticationDatabaseName) ? database
            : mongo.getDB(authenticationDatabaseName);
    this.userName = userName;
    this.password = password;
}

From source file:org.bigmouth.nvwa.log4mongo.MongoDbAppender.java

License:Apache License

protected DB getDatabase(Mongo mongo, String databaseName) {
    return mongo.getDB(databaseName);
}

From source file:org.broad.igv.plugin.mongocollab.MongoCollabPlugin.java

License:Open Source License

static DBCollection getCollection(Locator locator) {
    Mongo mongo = getMongo(locator.host, locator.port);
    DB mongoDB = mongo.getDB(locator.dbName);
    return mongoDB.getCollection(locator.collectionName);
}

From source file:org.broad.igv.plugin.mongocollab.MongoCollabPlugin.java

License:Open Source License

/**
 * Check whether the database/collection specified by the given {@code locator}
 * exists.// ww w.  j ava  2 s.  c  o m
 * @param locator
 * @return An integer consisting of 2 flags:
 * DB_EXISTS
 * COLLECTION_EXISTS
 *
 * with each set if the DB/COLLECTION exists. !DB_EXISTS && COLLECTION_EXISTS should never happen
 */
public static int checkDestinationExists(Locator locator) {
    Mongo mongo = getMongo(locator.host, locator.port);
    List<String> dbNames = mongo.getDatabaseNames();
    boolean dbExists = dbNames.indexOf(locator.dbName) >= 0;

    if (!dbExists) {
        return 0;
    }

    int result = DB_EXISTS;
    DB db = mongo.getDB(locator.dbName);
    Set<String> collections = db.getCollectionNames();
    result |= collections.contains(locator.collectionName) ? COLLECTION_EXISTS : 0;

    return result;
}