public void LoadSeedData(String strColl, String metadatafile) throws Exception { try { InputStream is = this.getClass().getResourceAsStream(metadatafile); String xml; xml = IOUtils.toString(is); JSONObject rwSeed = XML.toJSONObject(xml); if (!rwSeed.isNull("ReferralWireSeedData")) { JSONObject json = rwSeed.getJSONObject("ReferralWireSeedData"); DBCollection dbcoll = store.getColl(strColl); Object coll = (Object) json.get(strColl); if (coll instanceof JSONArray) { JSONArray defs = (JSONArray) coll; for (int i = 0; i < defs.length(); i++) { Object obj = (JSONObject) defs.get(i); dbcoll.insert((DBObject) com.mongodb.util.JSON.parse(obj.toString())); } } else { dbcoll.insert((DBObject) com.mongodb.util.JSON.parse(coll.toString())); } } } catch (IOException e) { // TODO Auto-generated catch block log.debug("API Error: ", e); } }
private void createTagDocuments() { DBCollection coll = mongoTemplate.getCollection(INPUT_COLLECTION); coll.insert(createDocument("Doc1", "spring", "mongodb", "nosql")); coll.insert(createDocument("Doc2", "spring", "mongodb")); coll.insert(createDocument("Doc3", "spring")); }
@Test public void testCountWithQueryCommand() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("n", 1)); collection.insert(new BasicDBObject("n", 2)); collection.insert(new BasicDBObject("n", 2)); assertEquals(2, collection.count(new BasicDBObject("n", 2))); }
@Test public void testCountOnCursor() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("n", 1)); collection.insert(new BasicDBObject("n", 2)); collection.insert(new BasicDBObject("n", 2)); assertEquals(3, collection.find(QueryBuilder.start("n").exists(true).get()).count()); }
public void indexSortedFile(String filename) { try { System.out.println("Indexing " + filename); BufferedReader bfr = new BufferedReader(new FileReader(filename)); String line = ""; ArrayList<DBObject> anchorArr = new ArrayList<DBObject>(); int count = 0; while ((line = bfr.readLine()) != null) { if (line.contains("|")) { String[] linesplit = line.split("\\|"); if (linesplit.length == 5) { anchorArr.add( indexDoc( linesplit[0], Integer.parseInt(linesplit[2]), Integer.parseInt(linesplit[3]), Integer.parseInt(linesplit[4]))); count++; if (count % 10000 == 0) { table.insert(anchorArr); anchorArr.clear(); // System.out.println(count); } } else if (linesplit.length > 5) { anchorArr.add( indexDoc( linesplit[0], Integer.parseInt(linesplit[2]), Integer.parseInt(linesplit[3]), Integer.parseInt(linesplit[4]))); for (int i = 5; i < linesplit.length; i = i + 2) { if (linesplit[i].matches("\\d+") && linesplit[i + 1].matches("\\d+")) { // Numeric string validation test anchorArr.add( indexDoc( linesplit[0], 0, Integer.parseInt(linesplit[i]), Integer.parseInt(linesplit[i + 1]))); count++; if (count % 10000 == 0) { table.insert(anchorArr); anchorArr.clear(); // System.out.println(count); } } } // End for } } } if (count % 10000 != 0) { table.insert(anchorArr); anchorArr.clear(); System.out.println(count); } bfr.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } // end indexSortedFile()
public void run() throws UnknownHostException { final List<Integer> models = new ArrayList<Integer>(); final List<String> owners = new ArrayList<String>(); final MongoClient client = new MongoClient(); DB db = client.getDB("mongo_hadoop"); DBCollection devices = db.getCollection("devices"); DBCollection logs = db.getCollection("logs"); if ("true".equals(System.getenv("SENSOR_DROP"))) { LOG.info("Dropping sensor data"); devices.drop(); logs.drop(); devices.createIndex(new BasicDBObject("devices", 1)); } db.getCollection("logs_aggregate").createIndex(new BasicDBObject("devices", 1)); if (logs.count() == 0) { for (int i = 0; i < 10; i++) { owners.add(getRandomString(10)); } for (int i = 0; i < 10; i++) { models.add(getRandomInt(10, 20)); } List<ObjectId> deviceIds = new ArrayList<ObjectId>(); for (int i = 0; i < NUM_DEVICES; i++) { DBObject device = new BasicDBObject("_id", new ObjectId()) .append("name", getRandomString(5) + getRandomInt(3, 5)) .append("type", choose(TYPES)) .append("owner", choose(owners)) .append("model", choose(models)) .append("created_at", randomDate(new Date(2000, 1, 1, 16, 49, 29), new Date())); deviceIds.add((ObjectId) device.get("_id")); devices.insert(device); } for (int i = 0; i < NUM_LOGS; i++) { if (i % 50000 == 0) { LOG.info(format("Creating %d sensor log data entries: %d%n", NUM_LOGS, i)); } BasicDBList location = new BasicDBList(); location.add(getRandomInRange(-180, 180, 3)); location.add(getRandomInRange(-90, 90, 3)); DBObject log = new BasicDBObject("_id", new ObjectId()) .append("d_id", choose(deviceIds)) .append("v", getRandomInt(0, 10000)) .append("timestamp", randomDate(new Date(2013, 1, 1, 16, 49, 29), new Date())) .append("loc", location); logs.insert(log); } } }
@Test public void testDistinctQuery() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("n", 1).append("_id", 1)); collection.insert(new BasicDBObject("n", 2).append("_id", 2)); collection.insert(new BasicDBObject("n", 3).append("_id", 3)); collection.insert(new BasicDBObject("n", 1).append("_id", 4)); collection.insert(new BasicDBObject("n", 1).append("_id", 5)); assertEquals(Arrays.asList(1, 2, 3), collection.distinct("n")); }
@Test public void testFindWithQuery() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("name", "jon")); collection.insert(new BasicDBObject("name", "leo")); collection.insert(new BasicDBObject("name", "neil")); collection.insert(new BasicDBObject("name", "neil")); DBCursor cursor = collection.find(new BasicDBObject("name", "neil")); assertEquals("should have two neils", 2, cursor.toArray().size()); }
protected <T> Key<T> insert(DBCollection dbColl, T entity, WriteConcern wc) { LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>(); DBObject dbObj = entityToDBObj(entity, involvedObjects); WriteResult wr; if (wc == null) wr = dbColl.insert(dbObj); else wr = dbColl.insert(dbObj, wc); throwOnError(wc, wr); return postSaveGetKey(entity, dbObj, dbColl, involvedObjects); }
@Test public void testRemove() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("_id", 1)); collection.insert(new BasicDBObject("_id", 2)); collection.insert(new BasicDBObject("_id", 3)); collection.insert(new BasicDBObject("_id", 4)); collection.remove(new BasicDBObject("_id", 2)); assertEquals(null, collection.findOne(new BasicDBObject("_id", 2))); }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String createTableName = request.getParameter("createTableName"); String createTableX = request.getParameter("createTableX"); try { boolean createTable = true; for (String name : Util.getMongoDb().getCollectionNames()) { if (name.equals(createTableName)) { createTable = false; } } if (createTable) { System.out.println("creating table"); DBObject options = BasicDBObjectBuilder.start().get(); DBCollection table = Util.getMongoDb().createCollection(createTableName, options); BasicDBObject document = new BasicDBObject(); if (createTableX.isEmpty()) { createTableX = "Date"; } document.put("_id", createTableX); document.put("value", "Value"); table.insert(document); boolean createTableCollections = true; for (String name : Util.getMongoDb().getCollectionNames()) { if (name.equals("TableCollections")) { createTableCollections = false; } } if (createTableCollections) { DBObject options2 = BasicDBObjectBuilder.start().get(); DBCollection tableResourceCollection = Util.getMongoDb().createCollection("TableCollections", options2); BasicDBObject documentResourceCollection = new BasicDBObject(); documentResourceCollection.put("_id", createTableName); documentResourceCollection.put("value", createTableX); tableResourceCollection.insert(documentResourceCollection); } else { DBCollection tableResourceCollection = Util.getMongoDb().getCollection("TableCollections"); BasicDBObject documentResourceCollection = new BasicDBObject(); documentResourceCollection.put("_id", createTableName); documentResourceCollection.put("value", createTableX); tableResourceCollection.insert(documentResourceCollection); } } else { request.setAttribute("message", "Table Already Exists"); } } catch (Exception e) { request.setAttribute("message", e.getMessage()); } request.setAttribute("tableNames", Util.getCollections()); request.getRequestDispatcher("createTable.jsp").forward(request, response); }
@Test public void testFindWithSkipLimit() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("_id", 1)); collection.insert(new BasicDBObject("_id", 2)); collection.insert(new BasicDBObject("_id", 3)); collection.insert(new BasicDBObject("_id", 4)); DBCursor cursor = collection.find().limit(2).skip(2); assertEquals( Arrays.asList(new BasicDBObject("_id", 3), new BasicDBObject("_id", 4)), cursor.toArray()); }
public void addToDB(Element e) { if (e instanceof Article) { Article art = (Article) e; document = new BasicDBObject(); if (art.getKey() != null) document.put("key", art.getKey()); if (art.getMdate() != null) document.put("mdate", art.getMdate()); if (art.getRating() != null) document.put("rating", art.getRating()); if (art.getReviewid() != null) document.put("reviewid", art.getReviewid()); article.insert(addElement(art, document)); } if (e instanceof Book) { Book bo = (Book) e; document = new BasicDBObject(); if (bo.getKey() != null) document.put("key", bo.getKey()); if (bo.getMdate() != null) document.put("mdate", bo.getMdate()); book.insert(addElement(bo, document)); } if (e instanceof Incollection) { Incollection inco = (Incollection) e; document = new BasicDBObject(); if (inco.getKey() != null) document.put("key", inco.getKey()); if (inco.getMdate() != null) document.put("mdate", inco.getMdate()); incollection.insert(addElement(inco, document)); } if (e instanceof Inproceedings) { Inproceedings inpro = (Inproceedings) e; document = new BasicDBObject(); if (inpro.getKey() != null) document.put("key", inpro.getKey()); if (inpro.getMdate() != null) document.put("mdate", inpro.getMdate()); inproceeding.insert(addElement(inpro, document)); } if (e instanceof Mastersthesis) { Mastersthesis mas = (Mastersthesis) e; document = new BasicDBObject(); if (mas.getKey() != null) document.put("key", mas.getKey()); if (mas.getMdate() != null) document.put("mdate", mas.getMdate()); mastersthesis.insert(addElement(mas, document)); } if (e instanceof Phdthesis) { Phdthesis phd = (Phdthesis) e; document = new BasicDBObject(); if (phd.getKey() != null) document.put("key", phd.getKey()); if (phd.getMdate() != null) document.put("mdate", phd.getMdate()); phdthesis.insert(addElement(phd, document)); } if (e instanceof Proceedings) { Proceedings pro = (Proceedings) e; document = new BasicDBObject(); if (pro.getKey() != null) document.put("key", pro.getKey()); if (pro.getMdate() != null) document.put("mdate", pro.getMdate()); proceedings.insert(addElement(pro, document)); } if (e instanceof Www) { Www w = (Www) e; document = new BasicDBObject(); if (w.getKey() != null) document.put("key", w.getKey()); if (w.getMdate() != null) document.put("mdate", w.getMdate()); www.insert(addElement(w, document)); } }
@Test public void testBasicUpdate() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("_id", 1)); collection.insert(new BasicDBObject("_id", 2).append("b", 5)); collection.insert(new BasicDBObject("_id", 3)); collection.insert(new BasicDBObject("_id", 4)); collection.update(new BasicDBObject("_id", 2), new BasicDBObject("a", 5)); assertEquals( new BasicDBObject("_id", 2).append("a", 5), collection.findOne(new BasicDBObject("_id", 2))); }
@Test public void testSortByEmeddedKey() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("_id", 1).append("a", new BasicDBObject("b", 1))); collection.insert(new BasicDBObject("_id", 2).append("a", new BasicDBObject("b", 2))); collection.insert(new BasicDBObject("_id", 3).append("a", new BasicDBObject("b", 3))); List<DBObject> results = collection.find().sort(new BasicDBObject("a.b", -1)).toArray(); assertEquals( Arrays.asList( new BasicDBObject("_id", 3).append("a", new BasicDBObject("b", 3)), new BasicDBObject("_id", 2).append("a", new BasicDBObject("b", 2)), new BasicDBObject("_id", 1).append("a", new BasicDBObject("b", 1))), results); }
@Test public void testIdInQueryResultsInIndexOrder() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("_id", 4)); collection.insert(new BasicDBObject("_id", 3)); collection.insert(new BasicDBObject("_id", 1)); collection.insert(new BasicDBObject("_id", 2)); DBCursor cursor = collection.find(new BasicDBObject("_id", new BasicDBObject("$in", Arrays.asList(3, 2, 1)))); assertEquals( Arrays.asList( new BasicDBObject("_id", 1), new BasicDBObject("_id", 2), new BasicDBObject("_id", 3)), cursor.toArray()); }
@Test public void testFullUpdateWithSameId() throws Exception { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("_id", 1)); collection.insert(new BasicDBObject("_id", 2).append("b", 5)); collection.insert(new BasicDBObject("_id", 3)); collection.insert(new BasicDBObject("_id", 4)); collection.update( new BasicDBObject("_id", 2).append("b", 5), new BasicDBObject("_id", 2).append("a", 5)); assertEquals( new BasicDBObject("_id", 2).append("a", 5), collection.findOne(new BasicDBObject("_id", 2))); }
public void createNewTodo(String body) { Todo todo = new Gson().fromJson(body, Todo.class); collection.insert( new BasicDBObject("title", todo.getTitle()) .append("done", todo.isDone()) .append("createdOn", new Date())); }
@Override public String IngresarJson(String nombreDB, String json, int clienteId) throws UnknownHostException { // TODO Auto-generated method stub MongoClient mongoClient = new MongoClient("localhost", 27017); DB base = mongoClient.getDB(nombreDB); // Ya existe if (ExisteCliente(nombreDB, clienteId)) { // System.out.println("********************\n"); // System.out.println("Ya existe cliente, error no se igresa el Json"); // System.out.println("********************\n"); return "Ya existe cliente, error no se igresa el Json"; } // no existe el cliente DBCollection collection = base.getCollection("Json"); BasicDBObject document = new BasicDBObject(); document.put("id", clienteId); DBObject object = (DBObject) JSON.parse(json); document.put("json", object); // document.put("json",json); collection.insert(document); return "Ok"; }
public void addEntry(GroupResource groupRes) { BasicDBObject info = new BasicDBObject(); info.putAll(groupRes.getMap()); collection.insert(info); }
public void insertar(UsuarioOD Usuario) { DBCollection coleccionUsuario = conectarMongo(); if (coleccionUsuario != null) { BasicDBObject usuario = new BasicDBObject(); usuario.put("id_u", crearId()); usuario.put("nombre", Usuario.getNombre()); usuario.put("nombres", Usuario.getNombres()); usuario.put("apellido", Usuario.getApellido()); usuario.put("apellidos", Usuario.getApellidos()); usuario.put("email", Usuario.getEmail()); usuario.put("fecha", Usuario.getFecha()); usuario.put("nick", Usuario.getNick()); usuario.put("pais", Usuario.getPais()); usuario.put("biografia", Usuario.getBiografia()); usuario.put("sexo", Usuario.getSexo()); usuario.put("clave", Usuario.getClave()); coleccionUsuario.insert(usuario); } else { System.out.println("coleccion no existente"); } }
@Before public void init() throws IOException { action = new MethodMsgAction(); MapPubSub pubSub = new MapPubSub(); DB db = initMongoDB(); collection = db.getCollection("customer"); collection.drop(); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("_id", "testid"); basicDBObject.put("testField", "pippo"); collection.insert(basicDBObject); action.addInvoker( new MongoDBMethodInvoker( db, pubSub, new IdGenerator() { @Override public String generateCollectionID(String randomSeed) { return "fixed"; } })); mockSession = new MockSession(); session = new DDPSession(mockSession); pubSub.sub( new Sub() { { setName("customer"); } }, session); }
public static void main(String args[]) throws IOException { DBCollection dbCollection = new MongoResource().getCollection(); ProductReader productReader = new ProductReader(); BasicDBObject product; System.out.println("Inserting products ..."); List<DBObject> products = new ArrayList<DBObject>(); while ((product = productReader.read()) != null) { products.add(product); } dbCollection.insert(products); System.out.println("Finished."); MetaDataReader metaDataReader = new MetaDataReader(); BasicDBObject metadata; System.out.println("Creating index on itemNumber"); dbCollection.ensureIndex("itemNumber"); System.out.println("Finished creating index on itemNumber"); System.out.println("Updating products ..."); while ((metadata = metaDataReader.read()) != null) { BasicDBObject existingProduct = new BasicDBObject("itemNumber", metadata.get("itemNumber")); dbCollection.update(existingProduct, metadata, true, false); } System.out.println("Finished."); }
private void batchInsertion( String argDatabaseName, Entity argEntity, DBCollection argDbCollection) { List<DBObject> dbObjectsList = new ArrayList<DBObject>(); for (int i = 0; i < MongoActivityConstants.BATCH_SIZE; i++) { DBObject dbObject = getDbObject(argDatabaseName, argEntity); dbObjectsList.add(dbObject); if (MongoActivityConstants.BATCH_REQUESTS_SIZE == dbObjectsList.size()) { argDbCollection.insert(dbObjectsList, WriteConcern.FSYNC_SAFE); dbObjectsList = new ArrayList<DBObject>(); } } // harvest the last dbobjects... if (dbObjectsList.size() > 0) { argDbCollection.insert(dbObjectsList, WriteConcern.FSYNC_SAFE); } }
private void insertDocuments( String argDatabaseName, Entity argEntity, DBCollection argDbCollection) { for (int i = 0; i < MongoActivityConstants.BATCH_SIZE; i++) { DBObject dbObject = getDbObject(argDatabaseName, argEntity); argDbCollection.insert(dbObject, WriteConcern.FSYNC_SAFE); } }
public JsonArray addPerfResults(String perfResults) { DBCollection coll = db.getCollection("performance"); BasicDBObject newPerfResults = (BasicDBObject) JSON.parse(perfResults); newPerfResults.append("_id", newPerfResults.getString("name")); BasicDBObject query = new BasicDBObject(); query.append("name", newPerfResults.getString("name")); BasicDBObject removeId = new BasicDBObject("_id", 0); DBCursor cursor = coll.find(query, removeId); JsonObjectBuilder objBuild = Json.createObjectBuilder(); JsonArrayBuilder arrBuild = Json.createArrayBuilder(); JsonObject json = objBuild.build(); if (cursor.count() > 0) { LOG.info("Performance Results Found: "); BasicDBObject found = (BasicDBObject) cursor.next(); json = Json.createReader(new StringReader(found.toString())).readObject(); arrBuild.add(json); } else { LOG.info("New Performance Results Created: " + coll.insert(newPerfResults)); json = Json.createReader(new StringReader(newPerfResults.toString())).readObject(); arrBuild.add(json); } LOG.info( "----------------------------------- ARR BUILD -------------------------------------------------\n" + arrBuild.build().toString()); return arrBuild.build(); }
@Test public void testDropDatabaseAlsoDropsCollectionData() throws Exception { DBCollection collection = newCollection(); collection.insert(new BasicDBObject()); collection.getDB().dropDatabase(); assertEquals("Collection should have no data", 0, collection.count()); }
@Test public void testInsertDuplicateIgnored() { DBCollection collection = newCollection(); collection.insert(new BasicDBObject("_id", 1)); collection.insert(new BasicDBObject("_id", 1)); assertEquals(1, collection.count()); }
public JsonArray updatePerfResults(String perfResults) { BasicDBObject doc = (BasicDBObject) JSON.parse(perfResults); doc.append("_id", doc.get("name")); DBCollection coll = db.getCollection("performance"); BasicDBObject query = new BasicDBObject(); query.put("_id", doc.get("name")); DBCursor cursor = coll.find(query); JsonObjectBuilder objBuild = Json.createObjectBuilder(); JsonArrayBuilder arrBuild = Json.createArrayBuilder(); JsonObject json = objBuild.build(); if (cursor.count() > 0) { LOG.info("Performance Results Found and Updated: " + coll.update(query, doc, true, false)); json = Json.createReader(new StringReader(perfResults)).readObject(); return arrBuild.add(json).build(); } else { LOG.info("Performance Results Created: " + coll.insert(doc)); json = Json.createReader(new StringReader(perfResults)).readObject(); return arrBuild.add(json).build(); } }
/* * (non-Javadoc) * * @see org.ow2.play.service.registry.api.Registry#put(java.lang.String, * java.lang.String) */ @Override @WebMethod public void put(String name, String url) throws RegistryException { if (logger.isLoggable(Level.FINE)) { logger.fine(String.format("Put url %s for name %s", url, name)); } checkInitialized(); if (name == null || url == null) { throw new RegistryException("Can not put null values name = %s, url = %s", name, url); } // update the entry if it already exists DBObject filter = new BasicDBObject(); filter.put(NAME_KEY, name); DBObject filtered = collection.findOne(filter); if (filtered != null) { filtered.put(URL_KEY, url); collection.save(filtered); } else { DBObject o = new BasicDBObject(); o.put(NAME_KEY, name); o.put(URL_KEY, url); collection.insert(o); } }