Example usage for com.mongodb DBCollection getStats

List of usage examples for com.mongodb DBCollection getStats

Introduction

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

Prototype

public CommandResult getStats() 

Source Link

Document

The collStats command returns a variety of storage statistics for a given collection

Usage

From source file:com.zjy.mongo.splitter.MongoSplitterFactory.java

License:Apache License

public static MongoCollectionSplitter getSplitterByStats(final MongoClientURI uri, final Configuration config) {
    /* Looks at the collection in mongo.input.uri
     * and choose an implementation based on what's in there.  */

    MongoCollectionSplitter returnVal;//w w w .  j  ava 2s. c o m

    // If the split calculation is totally disabled, just make one
    // big split for the whole collection.
    if (!MongoConfigUtil.createInputSplits(config)) {
        returnVal = new SingleMongoSplitter(config);
    } else {
        MongoClientURI authURI = MongoConfigUtil.getAuthURI(config);
        CommandResult stats;
        DBCollection coll = null;
        try {
            if (authURI != null) {
                coll = MongoConfigUtil.getCollectionWithAuth(uri, authURI);
                stats = coll.getStats();
                LOG.info("Retrieved Collection stats:" + stats);
            } else {
                coll = MongoConfigUtil.getCollection(uri);
                stats = coll.getStats();
            }
        } finally {
            if (coll != null) {
                MongoConfigUtil.close(coll.getDB().getMongo());
            }
        }

        if (!stats.getBoolean("ok", false)) {
            throw new RuntimeException(
                    "Unable to calculate input splits from collection stats: " + stats.getString("errmsg"));
        }

        if (!stats.getBoolean("sharded", false)) {
            returnVal = new StandaloneMongoSplitter(config);
        } else {
            // Collection is sharded
            if (MongoConfigUtil.isShardChunkedSplittingEnabled(config)) {
                // Creates one split per chunk. 
                returnVal = new ShardChunkMongoSplitter(config);
            } else if (MongoConfigUtil.canReadSplitsFromShards(config)) {
                // Creates one split per shard, but ignores chunk bounds. 
                // Reads from shards directly (bypassing mongos).
                // Not usually recommended.
                returnVal = new ShardMongoSplitter(config);
            } else {
                //Not configured to use chunks or shards -
                //so treat this the same as if it was an unsharded collection
                returnVal = new StandaloneMongoSplitter(config);
            }
        }
    }
    return returnVal;
}

From source file:com.zjy.mongo.splitter.StandaloneMongoSplitter.java

License:Apache License

@Override
public List<InputSplit> calculateSplits() throws SplitFailedException {
    final DBObject splitKey = MongoConfigUtil.getInputSplitKey(getConfiguration());
    final DBObject splitKeyMax = MongoConfigUtil.getMaxSplitKey(getConfiguration());
    final DBObject splitKeyMin = MongoConfigUtil.getMinSplitKey(getConfiguration());
    final int splitSize = MongoConfigUtil.getSplitSize(getConfiguration());
    final MongoClientURI inputURI;
    DBCollection inputCollection = null;
    final ArrayList<InputSplit> returnVal;
    try {/*from www  .  jav  a  2 s. c  o  m*/
        inputURI = MongoConfigUtil.getInputURI(getConfiguration());
        MongoClientURI authURI = MongoConfigUtil.getAuthURI(getConfiguration());
        if (authURI != null) {
            inputCollection = MongoConfigUtil.getCollectionWithAuth(inputURI, authURI);
        } else {
            inputCollection = MongoConfigUtil.getCollection(inputURI);
        }

        returnVal = new ArrayList<InputSplit>();
        final String ns = inputCollection.getFullName();

        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Running splitVector on namespace: %s.%s; hosts: %s",
                    inputURI.getDatabase(), inputURI.getCollection(), inputURI.getHosts()));
        }
        final DBObject cmd = BasicDBObjectBuilder.start("splitVector", ns).add("keyPattern", splitKey)
                .add("min", splitKeyMin).add("max", splitKeyMax)
                // force:True is misbehaving it seems
                .add("force", false).add("maxChunkSize", splitSize).get();

        CommandResult data;
        boolean ok = true;
        try {
            data = inputCollection.getDB().getSisterDB(inputURI.getDatabase()).command(cmd,
                    ReadPreference.primary());
        } catch (final MongoException e) { // 2.0 servers throw exceptions rather than info in a CommandResult
            data = null;
            LOG.info(e.getMessage(), e);
            if (e.getMessage().contains("unrecognized command: splitVector")) {
                ok = false;
            } else {
                throw e;
            }
        }

        if (data != null) {
            if (data.containsField("$err")) {
                throw new SplitFailedException("Error calculating splits: " + data);
            } else if (!data.get("ok").equals(1.0)) {
                ok = false;
            }
        }

        if (!ok) {
            final CommandResult stats = inputCollection.getStats();
            if (stats.containsField("primary")) {
                final DBCursor shards = inputCollection.getDB().getSisterDB("config").getCollection("shards")
                        .find(new BasicDBObject("_id", stats.getString("primary")));
                try {
                    if (shards.hasNext()) {
                        final DBObject shard = shards.next();
                        final String host = ((String) shard.get("host")).replace(shard.get("_id") + "/", "");
                        final MongoClientURI shardHost;
                        if (authURI != null) {
                            shardHost = new MongoClientURIBuilder(authURI).host(host).build();
                        } else {
                            shardHost = new MongoClientURIBuilder(inputURI).host(host).build();
                        }
                        MongoClient shardClient = null;
                        try {
                            shardClient = new MongoClient(shardHost);
                            data = shardClient.getDB(shardHost.getDatabase()).command(cmd,
                                    ReadPreference.primary());
                        } catch (final Exception e) {
                            LOG.error(e.getMessage(), e);
                        } finally {
                            if (shardClient != null) {
                                shardClient.close();
                            }
                        }
                    }
                } finally {
                    shards.close();
                }
            }
            if (data != null && !data.get("ok").equals(1.0)) {
                throw new SplitFailedException("Unable to calculate input splits: " + data.get("errmsg"));
            }

        }

        // Comes in a format where "min" and "max" are implicit
        // and each entry is just a boundary key; not ranged
        final BasicDBList splitData = (BasicDBList) data.get("splitKeys");

        if (splitData.size() == 0) {
            LOG.warn(
                    "WARNING: No Input Splits were calculated by the split code. Proceeding with a *single* split. Data may be too"
                            + " small, try lowering 'mongo.input.split_size' if this is undesirable.");
        }

        BasicDBObject lastKey = null; // Lower boundary of the first min split

        // If splitKeyMin was given, use it as first boundary.
        if (!splitKeyMin.toMap().isEmpty()) {
            lastKey = new BasicDBObject(splitKeyMin.toMap());
        }
        for (final Object aSplitData : splitData) {
            final BasicDBObject currentKey = (BasicDBObject) aSplitData;
            returnVal.add(createSplitFromBounds(lastKey, currentKey));
            lastKey = currentKey;
        }

        BasicDBObject maxKey = null;
        // If splitKeyMax was given, use it as last boundary.
        if (!splitKeyMax.toMap().isEmpty()) {
            maxKey = new BasicDBObject(splitKeyMax.toMap());
        }
        // Last max split
        final MongoInputSplit lastSplit = createSplitFromBounds(lastKey, maxKey);
        returnVal.add(lastSplit);
    } finally {
        if (inputCollection != null) {
            MongoConfigUtil.close(inputCollection.getDB().getMongo());
        }
    }

    return returnVal;
}

From source file:de.otto.mongodb.profiler.collection.DefaultCollectionProfiler.java

License:Apache License

protected void newSample(DefaultCollectionProfile profile) {

    final DBCollection collection = database.getCollection(profile.getCollectionName());
    final CommandResult result = collection.getStats();
    if (result.ok()) {
        profile.add(result);//from   w  ww  .  ja  va2 s.  co  m
    } else {
        if ("ns not found".equals(result.getErrorMessage())) {
            profile.reset();
        }
    }
}

From source file:fr.cnes.sitools.datasource.mongodb.dbexplorer.MongoDBExplorerResource.java

License:Open Source License

/**
 * Get a {@link Collection} from the MongoDB database and its name
 * /*from   www. j ava2  s .  com*/
 * @param database
 *          the database
 * @param colName
 *          the name of the collection
 * @return a {@link Collection} from the MongoDB database and its name
 */
private Collection getACollection(DB database, String colName) {
    List<String> statusDetails = new ArrayList<String>();
    DBCollection collectionMongo = database.getCollection(colName);
    Collection collection = new Collection();
    collection.setName(colName);
    collection.setUrl(getBaseRef());
    traceObjects(collectionMongo.getStats(), statusDetails);
    collection.setStatusDetails(statusDetails);
    return collection;
}

From source file:net.kamradtfamily.mongorest.CollectionsServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doGet()");

    String db_name = req.getParameter("dbname");
    String op = req.getParameter("op");
    if (db_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];/*  www . j  a  v a  2s  .  c  o  m*/
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (op == null)
        op = "list";

    DB db = mongo.getDB(db_name);

    if ("list".equals(op)) {
        Set<String> cols = db.getCollectionNames();
        out_json(req, cols);
        return;
    }

    // requires colname
    String col_name = req.getParameter("colname");
    if (col_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }
    DBCollection col = db.getCollection(col_name);

    if ("count".equals(op)) {
        out_str(req, "{\"count\":" + col.count() + "}", "application/json");
    } else if ("stats".equals(op)) {
        BasicBSONObject o = col.getStats();
        out_str(req, o.toString(), "application/json");
    } else
        res.sendError(SC_BAD_REQUEST);

}

From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoDB.java

License:Open Source License

/**
 * Returns statistics for the specified collection.
 *//*from w  ww. ja va 2  s.c o m*/
public DBObject getStats(DBCollection collection) throws StorageException {
    try {
        return collection.getStats();
    } catch (MongoException e) {
        throw new StorageException(e);
    }
}

From source file:org.vertx.mods.MongoPersistor.java

License:Apache License

private void getCollectionStats(Message<JsonObject> message) {
    String collection = getMandatoryString("collection", message);

    if (collection == null) {
        return;// w  w w  . j a va  2s .c o  m
    }

    DBCollection coll = db.getCollection(collection);
    CommandResult stats = coll.getStats();

    JsonObject reply = new JsonObject();
    reply.putObject("stats", new JsonObject(stats.toString()));
    sendOK(message, reply);

}