List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents)
From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java
License:Open Source License
public static void Inrdocumentinsert(int id, String FQN, String name, String container, String disjoint, String complete) {/*from ww w.ja v a2s .c om*/ try { MongoClient mongoClient = new MongoClient(mdlocalhost, mdport); DB db = mongoClient.getDB(tmtextdb); boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray()); DBCollection collection = db.getCollection("inheritancerelationship"); DBCursor cursor = collection.find(); BasicDBObject document = new BasicDBObject(); document.put("MySQLid", id); document.put("FQN", FQN); document.put("name", name); document.put("container", container); document.put("disjoint", disjoint); document.put("complete", complete); collection.insert(document); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java
License:Open Source License
public static void Inpdocumentinsert(int id, String FQN, String name, String container, String participant, int inheritanceRelationship_id) { try {/*from ww w .j ava 2 s. co m*/ MongoClient mongoClient = new MongoClient(mdlocalhost, mdport); DB db = mongoClient.getDB(tmtextdb); boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray()); DBCollection collection = db.getCollection("inheritanceparticipation"); DBCursor cursor = collection.find(); BasicDBObject document = new BasicDBObject(); document.put("MySQLid", id); document.put("FQN", FQN); document.put("name", name); document.put("container", container); document.put("participant", participant); document.put("inheritanceRelationship_id", inheritanceRelationship_id); collection.insert(document); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java
License:Open Source License
public static void Generaldocumentinsert(String id, String FQN, String name, String container, int potincy, String directtype, String label, String kind) { try {/*from w w w . jav a 2s .co m*/ MongoClient mongoClient = new MongoClient(mdlocalhost, mdport); DB db = mongoClient.getDB(tmtextdb); boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray()); DBCollection collection = db.getCollection("generalconnection"); DBCursor cursor = collection.find(); BasicDBObject document = new BasicDBObject(); document.put("MySQLid", id); document.put("FQN", FQN); document.put("name", name); document.put("potincy", potincy); document.put("container", container); document.put("directtype", directtype); document.put("label", label); document.put("kind", kind); collection.insert(document); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:de.unimannheim.infor.swt.uim.actions.MogoDBCreate.java
License:Open Source License
public static void Participationdocumentinsert(String id, String name, String FQN, String container, int participant_id, String lower, String upper, String row_name, String whole, String generalConnection) { try {//from w w w.j a va 2s. c o m MongoClient mongoClient = new MongoClient(mdlocalhost, mdport); DB db = mongoClient.getDB(tmtextdb); boolean auth = db.authenticate(tmtextuser, tmtextpassword.toCharArray()); DBCollection collection = db.getCollection("participation"); DBCursor cursor = collection.find(); BasicDBObject document = new BasicDBObject(); document.put("MySQLid", id); document.put("FQN", FQN); document.put("name", name); document.put("container", container); document.put("participant_id", participant_id); document.put("lower", lower); document.put("upper", upper); document.put("row_name", row_name); document.put("whole", whole); document.put("generalConnection", generalConnection); collection.insert(document); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
From source file:dinningphilosopher.DinningPhilosopher.java
private void eat(Lock leftFork, Lock rightFork, String name) throws Exception { leftFork.lock();/*from w w w . j a va 2s .com*/ rightFork.lock(); try { MongoClient mongoClient = new MongoClient("localhost"); DB db = mongoClient.getDB("mydb"); DBCollection coll = db.getCollection("mycol"); System.out.println(name + " eating..."); BasicDBObject doc1 = new BasicDBObject(name, " eating..."); coll.insert(doc1); Thread.sleep(1000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { System.out.println(name + " done eating and now thinking..."); MongoClient mongoClient = new MongoClient("localhost"); DB db = mongoClient.getDB("mydb"); DBCollection coll = db.getCollection("mycol"); BasicDBObject doc2 = new BasicDBObject(name, " done eating and now thinking..."); coll.insert(doc2); leftFork.unlock(); rightFork.unlock(); } }
From source file:dinningphilosopher.DinningPhilosopher.java
public void think(String name) throws Exception { try {/*from w w w. j a va 2 s .c om*/ MongoClient mongoClient = new MongoClient("localhost"); DB db = mongoClient.getDB("mydb"); DBCollection coll = db.getCollection("mycol"); System.out.println(name + " thinking..."); BasicDBObject doc = new BasicDBObject(name, " thinking..."); coll.insert(doc); Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:dsll.pinterest.crawler.Reduce.java
private static Text getPinContent(String url, DBCollection pinsCollection) throws JSONException { Document html = null;//from w w w . j av a 2 s. c o m JSONObject pin = new JSONObject(); try { html = Jsoup.connect(url).get(); } catch (Exception e) { return new Text("HTTP connection failed..."); } // Gather major pins data Element doc = html.select("body").first(); // Pin ID String id = (url.split("pin/")[1].split("/")[0]); pin.append("ID", id); // Pin image String imageURL = ""; Element tmp = doc.select("div[class=pinImageSourceWrapper]").first(); try { tmp = tmp.select("div[class=imageContainer]").select("img").first(); imageURL = tmp.attr("src"); } catch (Exception e) { } // try{ // ByteArrayOutputStream pimg=new ByteArrayOutputStream(), cimg = new ByteArrayOutputStream(); // for(int i=0; i<3; i++){ // BufferedImage img=dummyImage; // try{ // img = ImageIO.read(new URL(imageURL)); // // }catch(Exception e){} // ImageIO.write(img, "jpg", cimg); // if(pimg.size()<cimg.size()){ // pimg = cimg; // } // } // // save to hdfs // Configuration conf = new Configuration(); // FileSystem fs = FileSystem.get(conf); // Path outFile = new Path("/home/hadoop/"+id+".png"); // FSDataOutputStream out = fs.create(outFile); // out.write(pimg.toByteArray()); // // }catch(Exception e){ // e.printStackTrace(); // } pin.append("image", imageURL); //Pin name tmp = doc.select("h2[itemprop=name]").first(); String name = ""; if (tmp != null) { name = tmp.text().trim(); } pin.append("name", name); // Pin source Element sourceCont = doc.select("div[class=sourceFlagWrapper]").first(); JSONObject source = new JSONObject(); if (sourceCont != null) { String title = sourceCont.text().trim(); String src = sourceCont.select("a").first().attr("href"); source.append("title", title); source.append("src", src); } pin.append("source", source); //pin credit JSONObject pinCredit = new JSONObject(); Element credit = doc.select("div[class=pinCredits]").first(); String creditName = "", creditTitle = "", creditSource = ""; try { creditName = credit.select("div[class=creditName]").text().trim(); } catch (Exception e) { } try { creditTitle = credit.select("div[class=creditTitle]").text().trim(); } catch (Exception e) { } try { creditSource = credit.select("a").attr("href"); } catch (Exception e) { } pinCredit.append("name", creditName); pinCredit.append("title", creditTitle); pinCredit.append("src", creditSource); pin.append("credit", pinCredit); //comments JSONArray comments = new JSONArray(); Elements commentsConts = doc.select("div[class=commenterNameCommentText]"); for (Element commentCont : commentsConts) { JSONObject comment = new JSONObject(); Element creatorEle = commentCont.select("div[class=commenterWrapper] a").first(); String creatorName = creatorEle.text().trim(); String creatorSrc = creatorEle.attr("href"); String content = "", raw = ""; Element commentContent = commentCont.select(".commentDescriptionContent").first(); try { content = commentContent.text().trim(); raw = commentContent.html(); comment.append("creator", creatorName); comment.append("creator_url", creatorSrc); comment.append("content", content); comment.append("content_raw", raw); comments.put(comment); } catch (Exception e) { } } pin.append("comments", comments); //pin board link and related pins Element bottomDoc = doc.select("div[class=Module CloseupSidebar]").first(); //pin board JSONArray board = new JSONArray(); if (bottomDoc != null) { Element boardEle = bottomDoc.select("div[class=boardHeader]").first(); JSONObject b = new JSONObject(); String boardName = ""; try { boardName = boardEle.select("h3[class=title]").text().trim(); } catch (Exception ee) { } String boardSrc = ""; try { boardSrc = "https://www.pinterest.com" + boardEle.select("a").attr("href").trim(); } catch (Exception ee) { } b.append("name", boardName); b.append("src", boardSrc); board.put(b); } pin.append("board", board); //CAUTION: what if a pin shows up in different boards? //related pins bottomDoc = doc .select("div[class=closeupBottom] div[class=Module CloseupBottom] div[class=relatedPinsWrapper]") .first(); JSONArray relatedPins = new JSONArray(); if (bottomDoc != null) { Elements relatedPinsConts = bottomDoc.select("div[class=pinWrapper]"); for (Element relatedPinsCont : relatedPinsConts) { JSONObject relatedPin = new JSONObject(); try { relatedPin.append("src", "https://www.pinterest.com" + relatedPinsCont.select("div[class=pinHolder] > a").attr("href")); } catch (Exception e) { } relatedPins.put(relatedPin); } } pin.append("related_pins", relatedPins); // Optional: push data to database BasicDBObject dbObject = (BasicDBObject) JSON.parse(pin.toString()); pinsCollection.insert(dbObject); return new Text(pin.toString()); }
From source file:dsll.pinterest.crawler.Reduce.java
private static Text getBoardContent(String url, DBCollection boardsCollection) throws JSONException { // NOTE: only board information is crawled. the pins are left to the expanding process Document html = null;//w w w .ja v a 2 s. c o m JSONObject board = new JSONObject(); try { html = Jsoup.connect(url).get(); } catch (Exception e) { return new Text("HTTP connection failed..."); } // board major information String[] tmp = url.split("/"); String boardID = tmp[4]; String boardOwnrID = tmp[3]; String boardName = html.select("h1[class=boardName]").text().trim(); String boardDesp = html.select("p[class=description]").text().trim(); String boardOwnr = html.select("h4[classs=fullname]").text().trim(); // Contained Pins Elements pinsCont = html.select("div[class=pinWrapper]"); JSONArray pins = new JSONArray(); for (Element pinCont : pinsCont) { JSONObject pin = new JSONObject(); pin.append("src", pinCont.select("div[class=pinHolder]>a").first().attr("href")); pins.put(pin); } board.append("ID", boardID); board.append("owner_id", boardOwnrID); board.append("src", url); board.append("name", boardName); board.append("description", boardDesp); board.append("owner", boardOwnr); board.append("pins", pins); // Optional: push data to database BasicDBObject dbObject = (BasicDBObject) JSON.parse(board.toString()); boardsCollection.insert(dbObject); return new Text(board.toString()); }
From source file:edu.csulaerp.db.ReferenceMongo.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes no args/*from w w w. j a v a 2 s . c o m*/ * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017 */ public static void main(final String[] args) throws UnknownHostException { // connect to the local database server MongoClient mongoClient = new MongoClient(); /* // Authenticate - optional MongoCredential credential = MongoCredential.createMongoCRCredential(userName, database, password); MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential)); */ // get handle to "mydb" DB db = mongoClient.getDB("mydb"); // get a list of the collections in this database and print them out Set<String> collectionNames = db.getCollectionNames(); for (final String s : collectionNames) { System.out.println(s); } // get a collection object to work with DBCollection coll = db.getCollection("testCollection"); // drop all the data in it coll.drop(); // make a document and insert it BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1) .append("info", new BasicDBObject("x", 203).append("y", 102)); coll.insert(doc); // get it (since it's the only one in there since we dropped the rest earlier on) DBObject myDoc = coll.findOne(); System.out.println(myDoc); // now, lets add lots of little documents to the collection so we can explore queries and cursors for (int i = 0; i < 100; i++) { coll.insert(new BasicDBObject().append("i", i)); } System.out .println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount()); // lets get all the documents in the collection and print them out DBCursor cursor = coll.find(); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // now use a query to get 1 document out BasicDBObject query = new BasicDBObject("i", 71); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // $ Operators are represented as strings query = new BasicDBObject("j", new BasicDBObject("$ne", 3)).append("k", new BasicDBObject("$gt", 10)); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // now use a range query to get a larger subset // find all where i > 50 query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // range query with multiple constraints query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30)); cursor = coll.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // Count all documents in a collection but take a maximum second to do so coll.find().maxTime(1, SECONDS).count(); // Bulk operations BulkWriteOperation builder = coll.initializeOrderedBulkOperation(); builder.insert(new BasicDBObject("_id", 1)); builder.insert(new BasicDBObject("_id", 2)); builder.insert(new BasicDBObject("_id", 3)); builder.find(new BasicDBObject("_id", 1)).updateOne(new BasicDBObject("$set", new BasicDBObject("x", 2))); builder.find(new BasicDBObject("_id", 2)).removeOne(); builder.find(new BasicDBObject("_id", 3)).replaceOne(new BasicDBObject("_id", 3).append("x", 4)); BulkWriteResult result = builder.execute(); System.out.println("Ordered bulk write result : " + result); // Unordered bulk operation - no guarantee of order of operation builder = coll.initializeUnorderedBulkOperation(); builder.find(new BasicDBObject("_id", 1)).removeOne(); builder.find(new BasicDBObject("_id", 2)).removeOne(); result = builder.execute(); System.out.println("Ordered bulk write result : " + result); // parallelScan ParallelScanOptions parallelScanOptions = ParallelScanOptions.builder().numCursors(3).batchSize(300) .build(); List<Cursor> cursors = coll.parallelScan(parallelScanOptions); for (Cursor pCursor : cursors) { while (pCursor.hasNext()) { System.out.println(pCursor.next()); } } // release resources db.dropDatabase(); mongoClient.close(); }
From source file:edu.sjsu.carbonated.client.MongoDBDOA.java
License:Apache License
public static void main(String[] args) throws Exception { // connect to the local database server Mongo m = new Mongo(); // get handle to "mydb" DB db = m.getDB("mydb"); // Authenticate - optional // boolean auth = db.authenticate("foo", "bar"); // get a list of the collections in this database and print them out Set<String> colls = db.getCollectionNames(); for (String s : colls) { System.out.println(s);//from w w w .j a va 2 s .c o m } // get a collection object to work with DBCollection coll = db.getCollection("testCollection"); // drop all the data in it coll.drop(); // make a document and insert it BasicDBObject doc = new BasicDBObject(); doc.put("test", new AlbumResource("name", "desc", "user", "asdf")); doc.put("name", "MongoDB"); doc.put("type", "database"); doc.put("count", 1); BasicDBObject info = new BasicDBObject(); info.put("x", 203); info.put("y", 102); doc.put("info", info); coll.insert(doc); // get it (since it's the only one in there since we dropped the rest earlier on) DBObject myDoc = coll.findOne(); System.out.println(myDoc); // now, lets add lots of little documents to the collection so we can explore queries and cursors for (int i = 0; i < 100; i++) { coll.insert(new BasicDBObject().append("i", i)); } System.out .println("total # of documents after inserting 100 small ones (should be 101) " + coll.getCount()); // lets get all the documents in the collection and print them out DBCursor cur = coll.find(); while (cur.hasNext()) { System.out.println(cur.next()); } // now use a query to get 1 document out BasicDBObject query = new BasicDBObject(); query.put("i", 71); cur = coll.find(query); while (cur.hasNext()) { System.out.println(cur.next()); } // now use a range query to get a larger subset query = new BasicDBObject(); query.put("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50 cur = coll.find(query); while (cur.hasNext()) { System.out.println(cur.next()); } // range query with multiple contstraings query = new BasicDBObject(); query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30 cur = coll.find(query); while (cur.hasNext()) { System.out.println(cur.next()); } // create an index on the "i" field coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending // list the indexes on the collection List<DBObject> list = coll.getIndexInfo(); for (DBObject o : list) { System.out.println(o); } // See if the last operation had an error System.out.println("Last error : " + db.getLastError()); // see if any previous operation had an error System.out.println("Previous error : " + db.getPreviousError()); // force an error db.forceError(); // See if the last operation had an error System.out.println("Last error : " + db.getLastError()); db.resetError(); }