List of usage examples for com.mongodb DBCollection drop
public void drop()
From source file:models.OntoProcessor.java
License:Open Source License
protected void saveProcesses() throws JsonProcessingException { DBCollection processesColl = db.getCollection("processes"); processesColl.drop(); for (Process process : ontology.getProcesses()) { String serializedProcess = mapper.writeValueAsString(process); BasicDBObject dbObject = (BasicDBObject) JSON.parse(serializedProcess); dbObject.append("_id", process.getId()); processesColl.insert(dbObject);//from w w w . j a va 2 s.c om processesId.add(process.getId()); HashMap<String, String> node = new HashMap<>(); node.put("id", process.getId()); String label = ""; for (Keyword keyword : process.getKeywords()) { label += keyword.getLabel() + " - "; } label = label.substring(0, label.length() - 3); label += " [" + process.getUnit().getSymbol() + "]"; node.put("label", label); derivedNodes.add(node); } }
From source file:models.OntoProcessor.java
License:Open Source License
protected void saveCoefficients() throws JsonProcessingException { DBCollection coefficientsColl = db.getCollection("coefficients"); coefficientsColl.drop(); for (Coefficient coeff : ontology.getCoefficients()) { String serializedCoeff = mapper.writeValueAsString(coeff); BasicDBObject dbObject = (BasicDBObject) JSON.parse(serializedCoeff); dbObject.append("_id", coeff.getId()); coefficientsColl.insert(dbObject); }/* w ww . j a v a 2 s . com*/ }
From source file:models.OntoProcessor.java
License:Open Source License
protected void saveReferences() throws JsonProcessingException { DBCollection refColl = db.getCollection("references"); refColl.drop(); String serializedReferences = "{references:" + mapper.writeValueAsString(ontology.getReferences().values()) + "}"; BasicDBObject dbObject = (BasicDBObject) JSON.parse(serializedReferences); refColl.insert(dbObject);/*from ww w .ja va 2 s. c o m*/ }
From source file:models.OntoProcessor.java
License:Open Source License
protected void saveGraph() { DBCollection graphColl = db.getCollection("graph"); graphColl.drop(); for (SourceRelation sourceRelation : ontology.getSourceRelations().values()) { String sourceId = sourceRelation.getSource().getId(); String destinationId = sourceRelation.getDestination().getId(); HashMap<String, Object> link = new HashMap<>(); link.put("id", sourceRelation.getId()); link.put("source", processGroupsId.indexOf(sourceId)); link.put("target", processGroupsId.indexOf(destinationId)); if (sourceRelation.getType() != null) link.put("type", sourceRelation.getType().getId()); else//from www .j a v a 2s .c o m link.put("type", "#none"); links.add(link); } BasicDBObject dbObject = (BasicDBObject) JSON .parse("{nodes:" + toJson(nodes).toString() + ",links:" + toJson(links).toString() + "}"); graphColl.insert(dbObject); }
From source file:models.OntoProcessor.java
License:Open Source License
protected void saveDerivedGraph() { DBCollection derivedGraphColl = db.getCollection("derivedGraph"); derivedGraphColl.drop(); for (DerivedRelation relation : ontology.getDerivedRelations()) { String sourceId = relation.getSource().getId(); String destinationId = relation.getDestination().getId(); HashMap<String, Object> link = new HashMap<>(); link.put("source", processesId.indexOf(sourceId)); link.put("target", processesId.indexOf(destinationId)); if (relation.getType() != null) link.put("type", relation.getType().getId()); else//w w w . j a v a 2 s . c om link.put("type", "#none"); derivedLinks.add(link); } BasicDBObject dbObject = (BasicDBObject) JSON.parse( "{nodes:" + toJson(derivedNodes).toString() + ",links:" + toJson(derivedLinks).toString() + "}"); derivedGraphColl.insert(dbObject); }
From source file:models.OntoProcessor.java
License:Open Source License
protected void saveStats() throws JsonProcessingException { DBCollection statsColl = db.getCollection("ontologyStats"); statsColl.drop(); HashMap<String, Integer> stats = new HashMap<>(); stats.put("coefficientGroups", ontology.getCoefficientGroups().size()); stats.put("processGroups", ontology.getProcessGroups().size()); stats.put("coefficients", ontology.getCoefficients().size()); stats.put("processes", ontology.getProcesses().size()); Integer numberOfInputElementaryFlow = 0; Integer numberOfCalculatedElementaryFlow = 0; Integer numberOfImpact = 0;// w w w .j av a 2 s . c o m for (Process process : ontology.getProcesses()) { numberOfInputElementaryFlow += process.getInputFlows().size(); numberOfCalculatedElementaryFlow += process.getCalculatedFlows().size(); numberOfImpact += process.getImpacts().size(); } stats.put("inputFlows", numberOfInputElementaryFlow); stats.put("calculatedFlows", numberOfCalculatedElementaryFlow); stats.put("impacts", numberOfImpact); stats.put("sourceRelations", ontology.getSourceRelations().size()); stats.put("derivedRelations", ontology.getDerivedRelations().size()); stats.put("references", ontology.getReferences().size()); BasicDBObject dbObject = (BasicDBObject) JSON.parse(mapper.writeValueAsString(stats)); statsColl.insert(dbObject); }
From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java
License:Open Source License
@Override public void createSchema(Properties props) { // drop all collections com.mongodb.DB db = null;//w w w . j ava2 s. c o m try { // drop database and collections // db = mongo.getDB(database); db = mongo.getDB("admin"); db.requestStart(); // add shards /* * BasicDBObject addShard = new BasicDBObject("addshard", * "10.0.0.138:27011"); CommandResult shardAddResult = * db.command(addShard); shardAddResult.getException(); * System.out.println(shardAddResult.toString()); addShard = new * BasicDBObject("addshard", "10.0.0.138:27012"); shardAddResult = * db.command(addShard); * System.out.println(shardAddResult.toString()); //db.command( * " { addshard : \"10.0.0.138:27011\" , allowLocal : true } )"); */ db = mongo.getDB(database); DBCollection collection = db.getCollection("users"); collection.drop(); collection = db.getCollection("resources"); collection.drop(); collection = db.getCollection("manipulation"); collection.drop(); if (Boolean.parseBoolean( props.getProperty(MONGODB_SHARDING_PROPERTY, MONGODB_SHARDING_PROPERTY_DEFAULT)) == true) { // enable sharding on the database in the admin user db = mongo.getDB("admin"); BasicDBObject s = new BasicDBObject("enablesharding", props.getProperty(MONGODB_DB_PROPERTY, "benchmark")); CommandResult cr = db.command(s); // enable sharding on each collection cr = db.command(BasicDBObjectBuilder.start("shardCollection", "benchmark.users").push("key") .add("_id", 1).pop().get()); if (Boolean.parseBoolean(props.getProperty(MONGODB_MANIPULATION_ARRAY_PROPERTY, MONGODB_MANIPULATION_ARRAY_PROPERTY_DEFAULT)) == false) { cr = db.command(BasicDBObjectBuilder.start("shardCollection", "benchmark.resources").push("key") .add("walluserid", 1).pop().get()); cr = db.command(BasicDBObjectBuilder.start("shardCollection", "benchmark.manipulation") .push("key").add("rid", 1).pop().get()); } else { cr = db.command(BasicDBObjectBuilder.start("shardCollection", "benchmark.resources").push("key") .add("_id", 1).pop().get()); } //force move a chunk //BasicDBObject mov = new BasicDBObject("moveChunk","benchmark.users"); //BasicDBObject mo2 = new BasicDBObject("_id", 1); //this is the chunkid not the userid //mov.put("find", mo2); //mov.put("to", "shard0001"); //CommandResult result2 = mongo.getDB("admin").command(mov); //System.out.println(result2); //check if migration is happening // com.mongodb.DB db2 = mongo.getDB("config"); // DBCollection cl = db2.getCollection("locks"); // DBObject q = new BasicDBObject().append("_id", "balancer"); // DBObject qres = cl.findOne(q); // while( Integer.parseInt(qres.get("state").toString()) == 1 ) // System.out.println("still migrating"); } // create indexes on collection db = mongo.getDB(database); collection = db.getCollection("users"); collection.createIndex(new BasicDBObject("users.wallResources", 1)); // create // index // on collection = db.getCollection("resources"); collection.createIndex(new BasicDBObject("walluserid", 1)); // create // index // on // "i", // ascending if (Boolean.parseBoolean(props.getProperty(MONGODB_MANIPULATION_ARRAY_PROPERTY, MONGODB_MANIPULATION_ARRAY_PROPERTY_DEFAULT)) == false) { collection = db.getCollection("manipulation"); collection.createIndex(new BasicDBObject("mid", 1)); collection.createIndex(new BasicDBObject("rid", 1)); // create // index // on // "i", // ascending } else { collection.createIndex(new BasicDBObject("resources.Manipulations", 1)); // create index on // "i", ascending } } catch (Exception e) { System.out.println(e.toString()); return; } finally { if (db != null) { db.requestDone(); } } }
From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java
License:Open Source License
@Override public void reconstructSchema() { com.mongodb.DB db = null;/* w w w . jav a 2s . c o m*/ try { db = mongo.getDB(database); db.requestStart(); // getting the number of users DBCollection collection = db.getCollection("users"); int numUsers = (int) collection.getCount(); // for every user set their conf Friends and pend Friends to null for (int i = 0; i < numUsers; i++) { DBObject r = new BasicDBObject().append("_id", i); DBObject queryResult = collection.findOne(r); BasicDBObject updateCommand = new BasicDBObject(); updateCommand.put("$set", new BasicDBObject("ConfFriends", new ArrayList<Integer>())); WriteResult res = collection.update(r, updateCommand, false, false, writeConcern); updateCommand = new BasicDBObject(); updateCommand.put("$set", new BasicDBObject("PendFriends", new ArrayList<Integer>())); res = collection.update(r, updateCommand, false, false, writeConcern); } if (manipulationArray) { collection = db.getCollection("resources"); int numResources = (int) collection.getCount(); // for every user set their conf Friends and pend Friends to null for (int i = 0; i < numUsers; i++) { DBObject r = new BasicDBObject().append("_id", i); DBObject queryResult = collection.findOne(r); BasicDBObject updateCommand = new BasicDBObject(); updateCommand.put("$set", new BasicDBObject("Manipulations", new ArrayList<Integer>())); WriteResult res = collection.update(r, updateCommand, false, false, writeConcern); } } else { collection = db.getCollection("manipulation"); //TODO: test this if (Boolean.parseBoolean( props.getProperty(MONGODB_SHARDING_PROPERTY, MONGODB_SHARDING_PROPERTY_DEFAULT)) == true) collection.drop(); //this doesnt work with shards else { DBCursor DBCur = collection.find(); while (DBCur.hasNext()) collection.remove(DBCur.next()); } } db.requestDone(); } catch (Exception ex) { db.requestDone(); ex.printStackTrace(); } }
From source file:mongodb.performance.MongoDBPerformance.java
/** * @param args the command line arguments *//*from www . jav a 2s. co m*/ public static void main(String[] args) throws UnknownHostException, FileNotFoundException, IOException { if (args.length == 0) { System.out.println("Parmetro no informado!"); System.exit(-1); } System.out.println("Parmetro: " + args[0]); MongoClient mongoClient = new MongoClient(); //MongoClient mongoClient = new MongoClient( "54.172.218.64" , 27017 ); DB db = mongoClient.getDB("myDatabase"); DBCollection collection = db.getCollection("ads"); collection.drop(); BulkWriteOperation builder = collection.initializeUnorderedBulkOperation(); FileInputStream fileInputStream = new FileInputStream(".\\resources\\MongoDB" + args[0] + ".txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream)); // Insert // Time start long start = System.currentTimeMillis(); String line; while ((line = bufferedReader.readLine()) != null) { DBObject bson = (DBObject) JSON.parse(line); builder.insert(bson); } bufferedReader.close(); builder.execute(); //Time end long elapsed = System.currentTimeMillis() - start; System.out.println("[insert] Time elapsed: " + elapsed + " ms"); // Update // Time start start = System.currentTimeMillis(); collection.updateMulti(new BasicDBObject(), new BasicDBObject("$set", new BasicDBObject().append("ano", 2006))); // Time end elapsed = System.currentTimeMillis() - start; System.out.println("[update] Time elapsed: " + elapsed + " ms"); // Select // Time start start = System.currentTimeMillis(); BasicDBObject keys = new BasicDBObject(); keys.put("_id", 1); keys.put("modeloCarro.marca", 1); keys.put("modeloCarro.nome", 1); keys.put("uf", 1); keys.put("placa_carro", 1); keys.put("qtd_portas", 1); keys.put("cambio", 1); keys.put("combustivel", 1); keys.put("cor", 1); keys.put("km", 1); keys.put("valor", 1); keys.put("detalhe", 1); BasicDBObject sort = new BasicDBObject("_id", 1); DBCursor cursor = collection.find(new BasicDBObject(), keys).sort(sort); while (cursor.hasNext()) { cursor.next(); } // Time end elapsed = System.currentTimeMillis() - start; System.out.println("[select] Time elapsed: " + elapsed + " ms"); // Delete // Time start start = System.currentTimeMillis(); collection.remove(new BasicDBObject()); // Time end elapsed = System.currentTimeMillis() - start; System.out.println("[delete] Time elapsed: " + elapsed + " ms"); }
From source file:mypackage.ManupulateMongo.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ String respective = request.getParameter("respective"); String db = request.getParameter("dbname"); String coll = request.getParameter("collname"); String sb = respective;/*from w w w .jav a 2 s .c om*/ int index = sb.indexOf(";"); int length = respective.length(); String key = sb.substring(index + 1, length); HttpSession session = request.getSession(false); MongoClient mongoClient = new MongoClient("localhost", 27017); if (key.equals("db")) { String dbname = respective.substring(0, index); String coll_personal = session.getAttribute("uname") + "DB"; DB mongodb = mongoClient.getDB(dbname); DB mongodb_personal = mongoClient.getDB("mydb"); mongodb.dropDatabase(); DBCollection dBCollection = mongodb_personal.getCollection(coll_personal); BasicDBObject query = new BasicDBObject(); query.put("kapil", dbname); dBCollection.remove(query); out.print("true"); } if (key.equals("coll")) { String collname = respective.substring(0, index); DB mongodb = mongoClient.getDB(db); DBCollection dBCollection = mongodb.getCollection(collname); dBCollection.drop(); out.print("true"); } if (key.equals("doc")) { String docname = respective.substring(0, index); DB mongodb = mongoClient.getDB(db); DBCollection dBCollection = mongodb.getCollection(coll); BasicDBObject query = new BasicDBObject(); query.put("name", docname); dBCollection.remove(query); out.print("true"); } } }