Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:org.apache.jackrabbit.oak.upgrade.cli.node.MongoFactory.java

License:Apache License

@Override
public NodeStore create(BlobStore blobStore, Closer closer) throws UnknownHostException {
    String db;//ww w. ja  v a  2 s .  com
    if (uri.getDatabase() == null) {
        db = "aem-author"; // assume an author instance
    } else {
        db = uri.getDatabase();
    }
    DocumentMK.Builder builder = getBuilder(cacheSize);
    MongoClient client = new MongoClient(uri);
    closer.register(asCloseable(client));
    builder.setMongoDB(client.getDB(db));
    if (blobStore != null) {
        builder.setBlobStore(blobStore);
    }
    DocumentNodeStore documentNodeStore = builder.getNodeStore();
    closer.register(asCloseable(documentNodeStore));
    return documentNodeStore;
}

From source file:org.apache.nutch.crawl.GeneratorJob2.java

License:Apache License

public int generateBatchId(Configuration conf, String batchId) {
    MongoClient mongoClient = null;
    try {//from  ww  w  .j  a  v  a  2s  .c o  m
        mongoClient = new MongoClient(goraMongoAddress);
        DB db = mongoClient.getDB(goraMongoDb);
        String cId = conf.get(Nutch.CRAWL_ID_KEY);
        String collPrefix = "";
        if (org.apache.commons.lang3.StringUtils.isNoneEmpty(cId)) {
            collPrefix = cId + "_";
        }
        String crawlColl = collPrefix + "webpage";
        DBCollection collOps = db.getCollection(crawlColl);
        //update({"count":{$gt:20}},{$set:{"name":"c4"}},false,true)  
        BasicDBObject q = new BasicDBObject("batchId", null);

        DBObject set = new BasicDBObject("batchId", batchId);
        set.put("markers._gnmrk_", batchId);
        BasicDBObject o = new BasicDBObject("$set", set);
        WriteResult wr = collOps.update(q, o, false, true);
        long curTime = System.currentTimeMillis();
        //taotoxht add
        q = new BasicDBObject();
        q.append("fetchTime", new BasicDBObject().append(QueryOperators.GT, curTime));
        o = new BasicDBObject();
        o.append("$set", new BasicDBObject().append("fetchTime", curTime));
        collOps.update(q, o, false, true);

        return wr.getN();
    } catch (Exception e) {
        e.printStackTrace();
        return 0;

    } finally {
        if (mongoClient != null) {
            mongoClient.close();
        }
    }

}

From source file:org.apache.rya.export.mongo.parent.MongoParentMetadataRepository.java

License:Apache License

/**
 * Creates a new {@link MongoParentMetadataRepository}
 * @param client - The client connection to mongo.
 * @param dbName - The database to connect to, usually the RyaInstanceName
 *//*from w w  w. j  av  a2 s.  co m*/
public MongoParentMetadataRepository(final MongoClient client, final String dbName) {
    checkNotNull(client);
    checkNotNull(dbName);
    collection = client.getDB(dbName).getCollection(COLLECTION_NAME);
    adapter = new ParentMetadataRepositoryAdapter();
}

From source file:org.apache.rya.mongodb.instance.MongoRyaInstanceDetailsRepository.java

License:Apache License

/**
 * Constructs an instance of {@link MongoRyaInstanceDetailsRepository}.
 *
 * @param client - Connects to the instance of Mongo that hosts the Rya instance. (not null)
 * @param instanceName - The name of the Rya instance this repository represents. (not null)
 *//*from   www .  j a v a2  s  .  c  o m*/
public MongoRyaInstanceDetailsRepository(final MongoClient client, final String instanceName) {
    checkNotNull(client);
    this.instanceName = requireNonNull(instanceName);
    db = client.getDB(this.instanceName);
}

From source file:org.araqne.logdb.mongo.query.MongoFindCommand.java

License:Apache License

@Override
public void run() {
    MongoProfile profile = options.getProfile();

    MongoClient mongo = null;
    DBCursor cursor = null;/*  w ww.  ja  va2 s . c  o  m*/
    try {
        mongo = new MongoClient(profile.getAddress(), profile.getCredentials());

        DB db = mongo.getDB(options.getDatabase());
        DBCollection col = db.getCollection(options.getCollection());
        cursor = col.find();

        while (cursor.hasNext()) {
            DBObject doc = cursor.next();

            Map<String, Object> m = convert(doc);
            pushPipe(new Row(m));
        }
    } catch (Throwable t) {
        slog.error("araqne logdb mongo: cannot run mongo.find", t);
    } finally {
        if (cursor != null)
            cursor.close();

        if (mongo != null)
            mongo.close();
    }
}

From source file:org.aw20.mongoworkbench.command.AggregateMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);
    BasicDBObject cmdMap = parseMongoCommandString(db, cmd);

    if (!cmdMap.containsField("aggregateArg"))
        throw new Exception("no aggregate document");

    // Execute the command
    Object result = db.eval(cmd, (Object[]) null);

    if (result == null)
        throw new Exception("null returned");
    if (!(result instanceof BasicDBObject))
        throw new Exception("not correct type returned: " + result.getClass().getName());

    BasicDBObject dbo = (BasicDBObject) result;
    if (dbo.containsField("result")) {
        dbListResult = (BasicDBList) dbo.get("result");
        setMessage("# rows=" + dbListResult.size());
    } else {//from   ww  w . j a v  a  2s. c o m
        setMessage("# rows=0");
    }
}

From source file:org.aw20.mongoworkbench.command.CollectionCountMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);
    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);

    DBCollection collection = db.getCollection(sColl);

    setMessage("count=" + collection.count());
}

From source file:org.aw20.mongoworkbench.command.CollectionRemoveAllMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);
    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);

    DBCollection collection = db.getCollection(sColl);
    collection.remove(new BasicDBObject());

    setMessage("count=" + collection.count());
}

From source file:org.aw20.mongoworkbench.command.CollectionStatsMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);
    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);

    Set<String> collNames = db.getCollectionNames();
    Iterator<String> it = collNames.iterator();
    while (it.hasNext()) {
        String col = it.next();/*from   ww  w  .  ja  v a  2 s  .com*/

        statsListMap.add(transform(db.getCollection(col).getStats().toMap()));
    }

    setMessage("#" + statsListMap.size() + " collections ");
}

From source file:org.aw20.mongoworkbench.command.CreateCollectionMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    DB db = mdb.getDB(sDb);
    db.createCollection(sColl, new BasicDBObject());

    super.execute();
}