List of usage examples for com.mongodb CommandResult toString
@SuppressWarnings("deprecation") public String toString()
Returns a JSON serialization of this object
The output will look like: {"a":1, "b":["x","y","z"]} }
From source file:com.appdynamics.monitors.mongo.MongoDBMonitor.java
License:Apache License
private void fetchAndPrintCollectionStats() { for (String databaseName : mongoClient.listDatabaseNames()) { DB db = mongoClient.getDB(databaseName); Set<String> collectionNames = db.getCollectionNames(); if (collectionNames != null && collectionNames.size() > 0) { for (String collectionName : collectionNames) { DBCollection collection = db.getCollection(collectionName); CommandResult collectionStatsResult = collection.getStats(); if (collectionStatsResult != null && collectionStatsResult.ok()) { DBObject collectionStats = (DBObject) JSON.parse(collectionStatsResult.toString()); printCollectionStats(db.getName(), collectionName, collectionStats); } else { String errorMessage = "Retrieving stats for collection " + collectionName + " of " + db.getName() + " failed"; if (collectionStatsResult != null) { errorMessage = errorMessage .concat(" with error message " + collectionStatsResult.getErrorMessage()); }/*from w w w . j av a2s. co m*/ logger.error(errorMessage); } } } } }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * coll stats/*from w w w. j a va 2 s. c om*/ * * @param userDB * @param colName * @return * @throws Exception */ public static String getCollStats(UserDBDAO userDB, String colName) throws Exception { DBCollection collection = findCollection(userDB, colName); CommandResult cr = collection.getStats(); if (cr.ok()) { return cr.toString(); } else { throw cr.getException(); } }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * collection validate/*from w ww. j av a 2s . com*/ * * @param userDB * @param collName * @param isFull * @return * @throws Exception */ public static BasicDBObject collValidate(UserDBDAO userDB, String collName, boolean isFull) throws Exception { DBObject queryObj = new BasicDBObject("validate", collName); queryObj.put("full", isFull); CommandResult cr = findDB(userDB).command(queryObj); if (!cr.ok()) throw cr.getException(); if (logger.isDebugEnabled()) logger.debug("[compact] complements" + cr.toString()); return (BasicDBObject) cr; }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * collection compact// ww w .j a v a 2 s . c om * * @param userDB * @param colName * @param force * @param paddingFactor * @param paddingBytes * @return * @throws Exception */ public static String collCompact(UserDBDAO userDB, String colName, boolean isForct, int paddingFactor, int paddingBytes) throws Exception { DB mongoDB = findDB(userDB); DBObject queryObj = new BasicDBObject("compact", colName); if (paddingFactor > 0) queryObj.put("paddingFactor", paddingFactor); if (paddingBytes > 0) queryObj.put("paddingBytes", paddingBytes); CommandResult cr = mongoDB.command(queryObj); if (!cr.ok()) throw cr.getException(); if (logger.isDebugEnabled()) logger.debug("[compact] complements" + cr.toString()); return cr.toString(); }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * Server status, return to String/*from w w w .ja v a 2s . c o m*/ * * @param userDB * @throws Exception */ public static String serverStatus(UserDBDAO userDB) throws Exception { DB mongoDB = findDB(userDB); DBObject queryObj = new BasicDBObject("serverStatus", 1); CommandResult cr = mongoDB.command(queryObj); if (cr.ok()) { return cr.toString(); } else { throw cr.getException(); } }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * top// ww w. ja v a2 s. c o m * * @param userDB * @param top * @return * @throws Exception */ public static String top(UserDBDAO userDB, int top) throws Exception { UserDBDAO adminDB = new UserDBDAO(); adminDB.setHost(userDB.getHost()); adminDB.setPort(userDB.getPort()); adminDB.setUsers(userDB.getUsers()); adminDB.setPasswd(userDB.getPasswd()); adminDB.setUrl(userDB.getHost() + ":" + userDB.getPort() + "/admin"); adminDB.setDb("admin"); DB mongoDB = findDB(adminDB); DBObject queryObj = new BasicDBObject("top", top); CommandResult cr = mongoDB.command(queryObj); if (cr.ok()) { return cr.toString(); } else { logger.error("top command" + userDB, cr.getException()); throw new Exception(cr.getErrorMessage());//cr.getException(); } }
From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java
License:Open Source License
/** * db stats//from w ww. j a va2 s.c om * * @param userDB * @throws Exception */ public static String dbStats(UserDBDAO userDB) throws Exception { DB mongoDB = findDB(userDB); CommandResult cr = mongoDB.getStats(); if (cr.ok()) { return cr.toString(); } else { throw cr.getException(); } }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestDBStats.java
License:Open Source License
/** * @param args/*from w w w . j a v a2 s . c o m*/ */ public static void main(String[] args) throws Exception { ConAndAuthentication testMongoCls = new ConAndAuthentication(); Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port); DB db = mongo.getDB("test"); CommandResult cr = db.getStats(); System.out.println("[ok]" + cr.ok()); if (!cr.ok()) System.out.println("[Exception ]" + cr.getException().toString()); System.out.println("[toString]" + JSONUtil.getPretty(cr.toString())); System.out.println("[size]" + cr.size()); mongo.close(); }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestListCommand.java
License:Open Source License
/** * @param args//from w w w .ja v a 2 s. c o m */ public static void main(String[] args) throws Exception { ConAndAuthentication testMongoCls = new ConAndAuthentication(); Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port); DB db = mongo.getDB("test"); DBObject queryObj = new BasicDBObject("listCommands", 1); CommandResult cr = db.command(queryObj); System.out.println("[ok]" + cr.ok()); if (!cr.ok()) System.out.println("[Exception ]" + cr.getException().toString()); System.out.println("[toString]" + JSONUtil.getPretty(cr.toString())); System.out.println("[size]" + cr.size()); mongo.close(); }
From source file:com.hangum.tadpole.mongodb.core.test.MongoTestProfilling.java
License:Open Source License
/** * @param args/* w ww .jav a2 s . c om*/ */ public static void main(String[] args) throws Exception { ConAndAuthentication testMongoCls = new ConAndAuthentication(); Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port); DB db = mongo.getDB("test"); // ?? System.out.println("####[profilling ]######################################################"); CommandResult cr = db.command(new BasicDBObject("profile", 0)); System.out.println("[ok]" + cr.ok()); if (!cr.ok()) System.out.println("[Exception ]" + cr.getException().toString()); System.out.println("[toString]" + JSONUtil.getPretty(cr.toString())); System.out.println("[size]" + cr.size()); System.out.println("####[profilling ]######################################################"); // ? System.out.println( "####[profilling collections ]######################################################"); if (db.collectionExists("system.profile")) { DBCollection profileColl = db.getCollection("system.profile"); profileColl.drop(); } System.out.println( "####[profilling collections ]######################################################"); // ? // system.profile collection ? System.out.println( "####[profilling collections ? ]######################################################"); DBObject dbObject = (DBObject) JSON.parse("{capped:true, size:8000000}"); DBCollection dbColl = db.createCollection("system.profile", dbObject); BasicDBObject newProfileColl = (BasicDBObject) dbColl.getStats().copy(); System.out.println( "####[profilling collections ? ]######################################################"); // system.profile collection ? System.out.println("####[profilling ]######################################################"); cr = db.command(new BasicDBObject("profile", 2)); System.out.println("[ok]" + cr.ok()); if (!cr.ok()) System.out.println("[Exception ]" + cr.getException().toString()); System.out.println("[toString]" + JSONUtil.getPretty(cr.toString())); System.out.println("[size]" + cr.size()); System.out.println("####[profilling ]######################################################"); // //####################################################################################################### // //####################################################################################################### // //####################################################################################################### System.out.println("####[start profilling result]######################################################"); DBCollection myColl = db.getCollection("system.profile"); BasicDBObject query = new BasicDBObject(); query.put("millis", new BasicDBObject("$gt", 4)); DBCursor myCursor = myColl.find(); while (myCursor.hasNext()) { DBObject dbObj = myCursor.next(); System.out.println(dbObj.get("ts") + " - " + dbObj.get("ns") + " - " + dbObj.get("nscanned") + "/" + dbObj.get("nreturned") + " millis :" + dbObj.get("millis")); } System.out.println("####[end profilling result]######################################################"); mongo.close(); }