List of usage examples for com.mongodb Mongo getDB
@Deprecated public DB getDB(final String dbName)
From source file:mongoextraction.MongoExtraction.java
public static void main(String[] args) { try {// w w w . j a v a2s .c om Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("twitterdata2"); DBCollection collection = db.getCollection("tweets2"); // convert JSON to DBObject directly //DBObject dbObject = (DBObject) JSON //.parse("{'name':'harika', 'age':24}"); //collection.insert(dbObject); Integer count = 0; DBCursor cursorDoc = collection.find(); while (cursorDoc.hasNext()) { count++; //System.out.println(cursorDoc.next()); DBObject dbobj = cursorDoc.next(); //System.out.println(dbobj); String jsonString = dbobj.toString(); //String loctmp = jsonString. } System.out.println(count); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:mytubermiserver.mongo.GridFileSystem.java
public void sendVideo(String fileName, InputStream videoStream) throws UnknownHostException, MongoException, IOException { Mongo mongo = new Mongo(hostAddress, portAddress); DB db = mongo.getDB("MyTube"); // create a "video" namespace GridFS gfsPhoto = new GridFS(db, "video"); //get the file from stream GridFSInputFile gfsFile = gfsPhoto.createFile(videoStream); // set a new filename for identify purpose gfsFile.setFilename(fileName);/*from w w w .j ava2 s.c o m*/ // save the image file into mongoDB gfsFile.save(); videoStream.close(); }
From source file:mytubermiserver.mongo.GridFileSystem.java
public InputStream receiveVideo(String fileName) throws UnknownHostException, MongoException, IOException { Mongo mongo = new Mongo(hostAddress, portAddress); DB db = mongo.getDB("MyTube"); // create a "video" namespace GridFS gfsPhoto = new GridFS(db, "video"); // get image file by it's filename GridFSDBFile videoForOutput = gfsPhoto.findOne(fileName); return videoForOutput.getInputStream(); }
From source file:mytubermiserver.mongo.GridFileSystem.java
public void RemoveVideo(String fileName) throws UnknownHostException, MongoException, IOException { Mongo mongo = new Mongo(hostAddress, portAddress); DB db = mongo.getDB("MyTube"); // create a "video" namespace GridFS gfsVideo = new GridFS(db, "video"); // remove the image file from mongoDB gfsVideo.remove(fileName);//from w ww . jav a 2 s . c om }
From source file:net.bunselmeyer.mongo.maven.plugin.MigrateMojo.java
License:Apache License
private void runMigrations(List<MigrationDetails> migrations) { if (migrations.isEmpty()) { return;/*from w ww . ja v a 2 s. c o m*/ } MigrationDetails migrationDetails = migrations.get(0); Mongo mongo; try { mongo = new Mongo(migrationDetails.host, Integer.parseInt(port)); } catch (UnknownHostException e) { getLog().error("Failed to connect to " + migrationDetails.host + ":" + port); return; } catch (NumberFormatException e) { getLog().error("Invalid port: " + port); return; } mongo.setWriteConcern(WriteConcern.SAFE); DB db = mongo.getDB(migrationDetails.db); JacksonDBCollection<MigrationVersionDetails, ObjectId> migrationVersionCollection = createMigrationVersionCollection( db); getLog().info("Running migrations. Host: " + migrationDetails.host + ". DB: " + migrationDetails.db); sortMigrationDetails(migrations); Class<? extends Migration> lastMigration = null; try { for (MigrationDetails details : migrations) { lastMigration = details.migration; Migration m = details.migration.newInstance(); MigrationVersionDetails versionDetails = new MigrationVersionDetails(); versionDetails.setMigrationName(details.migration.getName()); versionDetails.setVersion(details.version); if (migrationVersionCollection.getCount(versionDetails) == 0) { m.up(db); db.getLastError().throwOnError(); getLog().info( " " + details.migration.getName() + ", v" + details.version + " migration complete"); versionDetails.setRun(DateTime.now(DateTimeZone.UTC)); migrationVersionCollection.insert(versionDetails); } else { getLog().info( " " + details.migration.getName() + ", v" + details.version + " was already run"); } } } catch (Exception e) { String name = lastMigration != null ? lastMigration.getName() : ""; getLog().info(" FAIL! " + name + " migration error", e); } finally { mongo.close(); } }
From source file:net.cit.tetrad.resource.ManagementResource.java
License:Open Source License
/** * mongos? ? ?? //from w w w. j a v a 2 s.c o m * @param request * @param response * @throws Exception */ @RequestMapping("/mongosResearch.do") public void mongosResearch(HttpServletRequest request, HttpServletResponse response, CommonDto commDto) throws Exception { String authUser = Utility.isNull(commDto.getAuthUser()); String authPasswd = Utility.isNull(commDto.getAuthPasswd()); String tempIp = Utility.isNull(commDto.getIp()); boolean loopbackCheck = commDto.isLoopbackCheck(); boolean authCheck = false; Mongo mongo = null; List<Map<String, Object>> resultLst = new ArrayList<Map<String, Object>>(); boolean auth = false; DB db; try { PersonJson result = new PersonJson(); if (authUser != "" && authPasswd != "") authCheck = true; List<Object> readMongoShards = new ArrayList<Object>(); List<Object> readMongos = new ArrayList<Object>(); Object readConfigDb = null; mongo = new Mongo(commDto.getIp(), Integer.parseInt(commDto.getPort())); if (authCheck) { db = mongo.getDB("admin"); auth = db.authenticate(authUser, authPasswd.toCharArray()); } readMongos = mongoStatusToMonitor.readMongoShards(mongo, "mongos", "config"); readMongoShards = mongoStatusToMonitor.readMongoShards(mongo, "shards", "config"); readConfigDb = mongoStatusToMonitor.readMongoStatus(mongo, new Device(), "getCmdLineOpts") .get("parsed_configdb"); mongo.close(); //mongos int mongoIdNum = 0; for (Object mongosObj : readMongos) { mongoIdNum++; Map<String, Object> statusMap = (Map<String, Object>) mongosObj; String[] splitName = ((String) statusMap.get("_id")).split(":"); boolean isExistCheck = false; if (isExistDeviceIpPort(splitName[0], splitName[1]) != null) isExistCheck = true; statusMap.put("isExistCheck", isExistCheck); statusMap.put("ipStr", splitName[0]); statusMap.put("portStr", splitName[1]); statusMap.put("groupNameStr", "mongos_grp"); statusMap.put("uidStr", "mongos" + "_" + mongoIdNum + "_" + splitName[1]); statusMap.put("typeStr", "mongos"); resultLst.add(statusMap); } // mongod for (Object shardObj : readMongoShards) { Map<String, Object> shardMap = (Map<String, Object>) shardObj; String[] host = ((String) shardMap.get("host")).split("/"); String[] hostLst = host[1].split(","); String[] ipPortSplit = hostLst[0].split(":"); if (ipPortSplit[0].equals("127.0.0.1") || ipPortSplit[0].equals("localhost")) { ipPortSplit[0] = tempIp; } mongo = new Mongo(ipPortSplit[0], Integer.parseInt(ipPortSplit[1])); if (authCheck) { db = mongo.getDB("admin"); auth = db.authenticate(authUser, authPasswd.toCharArray()); } Map<String, Object> readMongoStatus = mongoStatusToMonitor.readMongoStatus(mongo, new Device(), "replSetGetStatus"); mongoIdNum = 0; for (Object status : (List<Object>) readMongoStatus.get("members")) { mongoIdNum++; Map<String, Object> statusMap = (Map<String, Object>) status; String[] splitName = ((String) statusMap.get("name")).split(":"); if (loopbackCheck == true && (splitName[0].equals("127.0.0.1") || splitName[0].equals("localhost"))) { splitName[0] = tempIp; } boolean isExistCheck = false; if (isExistDeviceIpPort(splitName[0], splitName[1]) != null) isExistCheck = true; statusMap.put("isExistCheck", isExistCheck); statusMap.put("ipStr", splitName[0]); statusMap.put("portStr", splitName[1]); statusMap.put("groupNameStr", readMongoStatus.get("set")); statusMap.put("uidStr", readMongoStatus.get("set") + "_" + mongoIdNum + "_" + statusMap.get("stateStr")); statusMap.put("typeStr", "mongod"); resultLst.add(statusMap); } mongo.close(); } //config mongoIdNum = 0; for (String configStr : ((String) readConfigDb).split(",")) { mongoIdNum++; Map<String, Object> statusMap = new HashMap<String, Object>(); String[] splitName = configStr.split(":"); if (loopbackCheck == true && (splitName[0].equals("127.0.0.1") || splitName[0].equals("localhost"))) { splitName[0] = tempIp; } boolean isExistCheck = false; if (isExistDeviceIpPort(splitName[0], splitName[1]) != null) isExistCheck = true; statusMap.put("isExistCheck", isExistCheck); statusMap.put("ipStr", splitName[0]); statusMap.put("portStr", splitName[1]); statusMap.put("groupNameStr", "config_grp"); statusMap.put("uidStr", "config" + "_" + mongoIdNum + "_" + splitName[1]); statusMap.put("typeStr", "config"); resultLst.add(statusMap); } result.setAaData(resultLst); JSONObject jsonObject = JSONObject.fromObject(result); Writer writer = setResponse(response).getWriter(); writer.write(jsonObject.toString()); writer.flush(); } catch (Exception e) { e.printStackTrace(); mongo.close(); } log.debug("end - mongosResearch()"); }
From source file:net.cit.tetrad.rrd.dao.MongoStatusToMonitorImpl.java
License:Open Source License
public Map<String, Object> readMongoStatus(Mongo mongo, Device device, String command, String databaseName) throws MongoException, Exception { Map<String, Object> deviceStatus = new HashMap<String, Object>(); try {//w w w. j a va2s . c om DB db = mongo.getDB(databaseName); CommandResult commandResult = db.command(command); deviceStatus = JasonUtil.getEntryValue(commandResult, "", deviceStatus); } catch (DataAccessResourceFailureException e) { e.printStackTrace(); throw new MongoException(e.getMessage()); } catch (MongoException e) { // alarm ? e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } finally { } return deviceStatus; }
From source file:net.cit.tetrad.rrd.dao.MongoStatusToMonitorImpl.java
License:Open Source License
public List<Object> readMongoShards(Mongo mongo, String collection, String databaseName) throws MongoException, Exception { List<Object> dboLst = new ArrayList<Object>(); try {//from www . j a v a2 s .c om DB db = mongo.getDB(databaseName); DBCollection dbcollection = db.getCollectionFromString(collection); DBCursor cr = dbcollection.find(); Iterator<DBObject> it = cr.iterator(); DBObject dbo; while (it.hasNext()) { dbo = it.next(); Map<String, Object> deviceStatus = new HashMap<String, Object>(); deviceStatus = JasonUtil.getEntryValue(dbo, "", deviceStatus); dboLst.add(deviceStatus); } } catch (DataAccessResourceFailureException e) { e.printStackTrace(); throw new MongoException(e.getMessage()); } catch (MongoException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } finally { } return dboLst; }
From source file:net.cit.tetrad.rrd.dao.MongoStatusToMonitorImpl.java
License:Open Source License
public List<Object> readMongoChunksGrp(Mongo mongo, String collName, List<Object> dboLst) throws MongoException, Exception { try {//from w w w. j a v a 2 s.c o m DB db = mongo.getDB("config"); DBCollection dbcollection = db.getCollectionFromString("chunks"); DBObject dbo = dbcollection.group(new BasicDBObject("shard", true), new BasicDBObject("ns", collName), new BasicDBObject("nChunks", 0), "function (doc, out) {out.nChunks++;}"); Iterator<String> keys = dbo.keySet().iterator(); BasicDBObject getDbo; while (keys.hasNext()) { String key = keys.next(); getDbo = (BasicDBObject) dbo.get(key); getDbo.put("collName", collName); dboLst.add(getDbo); } } catch (DataAccessResourceFailureException e) { e.printStackTrace(); throw new MongoException(e.getMessage()); } catch (MongoException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } finally { } return dboLst; }
From source file:net.cit.tetrad.rrd.dao.MongoStatusToMonitorImpl.java
License:Open Source License
public Set<String> readMongoCollectionName(Mongo mongo, String dbName) throws MongoException, Exception { Set<String> collNameLst = null; try {/* ww w . j av a 2 s. co m*/ DB db = mongo.getDB(dbName); collNameLst = db.getCollectionNames(); } catch (DataAccessResourceFailureException e) { e.printStackTrace(); throw new MongoException(e.getMessage()); } catch (MongoException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } finally { } return collNameLst; }