List of usage examples for com.mongodb DBCollection insert
public WriteResult insert(final List<? extends DBObject> documents)
From source file:paper.star.dominator.authors.algorithm.ForStart.java
public void writeFile(TreeMap<Integer, Double> treeMap, String name) throws Exception { try {//from w w w .j ava2 s.c o m DB db; DBCollection dbresult; MongoClient mongo; mongo = new MongoClient("localhost", 27017); db = mongo.getDB("nstar"); dbresult = db.getCollection("rank.C300" + name); System.out.println("Start write a file...!"); String path = "data/" + name + "--pageRank" + System.currentTimeMillis() + ".csv"; FileWriter writer = new FileWriter(path); writer.append("ID"); writer.append(','); writer.append("Value"); writer.append('\n'); for (Entry<Integer, Double> entry : treeMap.entrySet()) { writer.append("" + entry.getKey()); writer.append(','); writer.append("" + entry.getValue()); writer.append('\n'); dbresult.insert( new BasicDBObject().append("_id", entry.getKey()).append("value", entry.getValue())); } writer.flush(); writer.close(); System.out.println("...Write file is successfully!"); } catch (Exception e) { e.printStackTrace(); } }
From source file:paper.star.dominator.authors.algorithm.NStar.java
public void writeFile(TreeMap<Integer, Double> treeMap, String name) throws Exception { try {//from w w w .j av a 2 s .c o m DB db; DBCollection dbresult; MongoClient mongo; mongo = new MongoClient("localhost", 27017); db = mongo.getDB("nstar"); dbresult = db.getCollection("result.full300" + name); System.out.println("Start write a file...!"); String path = "data/" + name + "--pageRank" + System.currentTimeMillis() + ".csv"; FileWriter writer = new FileWriter(path); writer.append("ID"); writer.append(','); writer.append("Value"); writer.append('\n'); for (Entry<Integer, Double> entry : treeMap.entrySet()) { writer.append("" + entry.getKey()); writer.append(','); writer.append("" + entry.getValue()); writer.append('\n'); dbresult.insert( new BasicDBObject().append("_id", entry.getKey()).append("value", entry.getValue())); } writer.flush(); writer.close(); System.out.println("...Write file is successfully!"); } catch (Exception e) { e.printStackTrace(); } }
From source file:paper.star.dominator.authors.main.NewRank.java
public static void writeFile(TreeMap<Integer, Double> treeMap, String name) throws Exception { try {//w w w .ja va2 s .c o m DB db; DBCollection dbresult; MongoClient mongo; mongo = new MongoClient("localhost", 27017); db = mongo.getDB("nstar"); dbresult = db.getCollection("result.full300"); System.out.println("Start write a file...!"); String path = "data/" + name + "--pageRank" + System.currentTimeMillis() + ".csv"; FileWriter writer = new FileWriter(path); writer.append("ID"); writer.append(','); writer.append("Value"); writer.append('\n'); for (Entry<Integer, Double> entry : treeMap.entrySet()) { writer.append("" + entry.getKey()); writer.append(','); writer.append("" + entry.getValue()); writer.append('\n'); dbresult.insert( new BasicDBObject().append("_id", entry.getKey()).append("value", entry.getValue())); } writer.flush(); writer.close(); System.out.println("...Write file is successfully!"); } catch (Exception e) { e.printStackTrace(); } }
From source file:paper.star.dominator.authors.main.NRank.java
public static void writeFile(TreeMap<Integer, Double> treeMap, String name) throws Exception { try {//from ww w . j a v a 2 s . co m DB db; DBCollection dbresult; MongoClient mongo; mongo = new MongoClient("localhost", 27017); db = mongo.getDB("nstar"); dbresult = db.getCollection("rank.C300"); System.out.println("Start write a file...!"); String path = "data/" + name + "--pageRank" + System.currentTimeMillis() + ".csv"; FileWriter writer = new FileWriter(path); writer.append("ID"); writer.append(','); writer.append("Value"); writer.append('\n'); for (Entry<Integer, Double> entry : treeMap.entrySet()) { writer.append("" + entry.getKey()); writer.append(','); writer.append("" + entry.getValue()); writer.append('\n'); dbresult.insert( new BasicDBObject().append("_id", entry.getKey()).append("value", entry.getValue())); } writer.flush(); writer.close(); System.out.println("...Write file is successfully!"); } catch (Exception e) { e.printStackTrace(); } }
From source file:parlare.application.server.model.Database.java
private String doClientMongo() { String print = ""; System.out.println("User:" + user + " Source:" + source + " Password:" + password); try {//from w w w . j av a2 s . c o m // connect to the local database server MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(MongoCredential.createMongoCRCredential(user, source, password.toCharArray())), new MongoClientOptions.Builder().build()); // get handle to "mydb" DB db = mongoClient.getDB("html5apps"); // Authenticate - optional // boolean auth = db.authenticate("foo", "bar"); // get a list of the collections in this database and print them out Set<String> collectionNames = db.getCollectionNames(); for (String s : collectionNames) { System.out.println(s); } // get a collection object to work with DBCollection testCollection = db.getCollection("testCollection"); // drop all the data in it testCollection.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)); testCollection.insert(doc); // get it (since it's the only one in there since we dropped the rest earlier on) DBObject myDoc = testCollection.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++) { testCollection.insert(new BasicDBObject().append("i", i)); } System.out.println("total # of documents after inserting 100 small ones (should be 101) " + testCollection.getCount()); // lets get all the documents in the collection and print them out DBCursor cursor = testCollection.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 = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // now use a range query to get a larger subset query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50 cursor = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println("Cursor: " + cursor.next()); } } finally { cursor.close(); } // range query with multiple constraints query = new BasicDBObject("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30 cursor = testCollection.find(query); try { while (cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); } // create an index on the "i" field testCollection.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending // list the indexes on the collection List<DBObject> list = testCollection.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(); // release resources mongoClient.close(); } catch (UnknownHostException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } return print; }
From source file:parser.RestrictedParser.java
License:Open Source License
/** * To parse ANEW dictionaries//from w ww. j a v a 2 s .com * * @param dbName Name of the database * @param collectionName Name of the new collection * @param dicFile Dictionary file in tsv format */ public static void parseANEW(String dbName, String collectionName, String dicFile) { try { System.out.println("Se conecta a la base de datos"); // Credentials to login into your database String user = ""; String password = ""; MongoCredential credential = MongoCredential.createMongoCRCredential(user, dbName, password.toCharArray()); MongoClient mongoClient = new MongoClient(new ServerAddress("localhost"), Arrays.asList(credential)); // Get the DB DB db = mongoClient.getDB(dbName); // Create the collection db.createCollection(collectionName, null); // Get the collection DBCollection coll = db.getCollection(collectionName); // Parse the dictionary System.out.println("Comienza el parseo del diccionario"); FileReader input = new FileReader(dicFile); BufferedReader bufRead = new BufferedReader(input); String myLine = null; myLine = bufRead.readLine(); // The first line is not needed while ((myLine = bufRead.readLine()) != null) { String[] word = myLine.split(" "); BasicDBObject doc = new BasicDBObject("Word", word[0]).append("Wdnum", new Double(word[1])) .append("ValMn", new Double(word[2])).append("ValSD", new Double(word[3])) .append("AroMn", new Double(word[4])).append("AroSD", new Double(word[5])) .append("DomMn", new Double(word[6])).append("DomSD", new Double(word[7])); coll.insert(doc); System.out.println("Parseando -> " + myLine); } bufRead.close(); } catch (Exception e) { System.err.println(e); } }
From source file:partha.mongodb.manager.DBHelper.java
@Override public String addDefault(String tableName, String json) { if (tableName == null || tableName.equals("") || json == null || json.equals("")) { return "501"; }//from ww w.j a v a 2s . c o m DBCollection table = db.getCollection(tableName); DBObject dbObject = (DBObject) JSON.parse(json); dbObject.put("status", "active"); dbObject.put("createdate", System.currentTimeMillis() + ""); dbObject.put("updatedate", System.currentTimeMillis() + ""); WriteResult wRes = table.insert(dbObject); return ((ObjectId) dbObject.get("_id")).toString(); }
From source file:partha.mongodb.manager.DBHelper.java
@Override public String addDefaultMap(String tableName, Map columns) { if (tableName == null || tableName.equals("") || columns == null || columns.equals("")) { return "501"; }// w w w. ja v a 2s . c o m columns.put("status", "active"); columns.put("createdate", System.currentTimeMillis() + ""); columns.put("updatedate", System.currentTimeMillis() + ""); DBCollection table = db.getCollection(tableName); BasicDBObject document = new BasicDBObject(columns); WriteResult wRes = table.insert(document); return ((ObjectId) document.get("_id")).toString(); }
From source file:payroll.FrmInsert.java
private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed // TODO add your handling code here: int i = 0;/* www. j a va 2 s .co m*/ String nu = ""; if (txtFname.getText().equals(nu)) { JOptionPane.showMessageDialog(null, "First name cannot remain empty", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (txtLname.getText().equals(nu)) { JOptionPane.showMessageDialog(null, "Last name cannot remain empty", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (txtEmail.getText().equals(nu)) { JOptionPane.showMessageDialog(null, "Email cannot remain empty", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (txtareaAddress.getText().equals(nu)) { JOptionPane.showMessageDialog(null, "Address cannot remain empty", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (txtMobno.getText().equals(nu)) { JOptionPane.showMessageDialog(null, "Mobile number cannot remain empty", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (txtMobno.getText().length() != 10) { JOptionPane.showMessageDialog(null, "Enter valid mobile number", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (cmbDesig.getSelectedItem().equals(nu)) { JOptionPane.showMessageDialog(null, "Select a gender", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (cmbQual.getSelectedItem().equals(nu)) { JOptionPane.showMessageDialog(null, "Select your qualification", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (dtDOB.getDate().equals(nu)) { JOptionPane.showMessageDialog(null, "Date of birth cannot remain empty", "FAILED", JOptionPane.ERROR_MESSAGE); return; } if (dtDOJ.getDate().equals(nu)) { JOptionPane.showMessageDialog(null, "Date of joining cannot remain empty", "FAILED", JOptionPane.ERROR_MESSAGE); return; } try { java.util.Date d1 = dtDOB.getDate(); java.util.Date d2 = dtDOJ.getDate(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date = sdf.format(dtDOB.getDate()); String gender = null; ButtonGroup group = new ButtonGroup(); if (rdbMale.isSelected()) { gender = rdbMale.getText(); } else { gender = rdbFemale.getText(); } final JPanel p2 = new JPanel(); final JOptionPane jo = new JOptionPane(); Connect1 j = new Connect1(); DBCollection dbc = j.connect("Emp_Records"); DBCursor cursor = dbc.find(); while (cursor.hasNext() == true) { DBObject obj = cursor.next(); i++; } String ID = ""; if (i < 10) ID = "EMP00" + i; else ID = "EMP0" + i; String emailID = txtEmail.getText(); String emailRE = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; Boolean flag = emailID.matches(emailRE); BasicDBObject bdb = new BasicDBObject(); bdb.put("ID", ID); bdb.put("First Name", txtFname.getText()); bdb.put("Last Name", txtLname.getText()); if (flag != null) bdb.put("Email", txtEmail.getText()); else { JOptionPane.showMessageDialog(p2, "Invalid Email Id", "Enter a valid email address", JOptionPane.ERROR_MESSAGE); return; } bdb.put("Address", txtareaAddress.getText()); bdb.put("Mobile Number", txtMobno.getText()); bdb.put("Qualification", cmbQual.getSelectedItem()); bdb.put("Designation", cmbDesig.getSelectedItem()); Calendar c = new GregorianCalendar(); int year = c.get(Calendar.YEAR); /* if(year-d1.getYear()-1900>60) { JOptionPane.showMessageDialog(p2,"Only employees between 21 to 60 years can work ","failure",JOptionPane.ERROR_MESSAGE); return; } if(year-d1.getYear()<20) { JOptionPane.showMessageDialog(p2,"Only employees between 21 to 60 years can work ","failure",JOptionPane.ERROR_MESSAGE); return; }*/ bdb.put("DOB", d1); bdb.put("Gender", gender); bdb.put("DOJ", d2); dbc.insert(bdb); JOptionPane.showMessageDialog(p2, "Registered successfully", "Registration success", JOptionPane.INFORMATION_MESSAGE); clear(); this.setVisible(false); FrmPassword u1 = new FrmPassword(); new FrmPassword().setVisible(true); } catch (Exception e) { JOptionPane.showMessageDialog(null, "ONE OR MORE FIELDS ARE EMPTY", "FAILURE", JOptionPane.ERROR_MESSAGE); } }
From source file:payroll.FrmLeave.java
private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed // TODO add your handling code here: Connect1 j = new Connect1(); DBCollection dbc = j.connect("leave"); BasicDBObject bdb = new BasicDBObject(); Date d1 = dtFrom.getDate();//from w w w . j a va 2s . c o m Date d2 = dtTo.getDate(); Calendar calendar = new GregorianCalendar(); if ((d1.getDate() > d2.getDate()) && (d1.getMonth()) == d2.getMonth()) { JOptionPane.showMessageDialog(null, "Invalid order of dates", "FAILURE", JOptionPane.ERROR_MESSAGE); return; } if (d1.getDate() < calendar.get(Calendar.DATE) && d1.getMonth() == calendar.get(Calendar.DATE)) { JOptionPane.showMessageDialog(null, "Only future leaves can be taken", "FAILURE", JOptionPane.ERROR_MESSAGE); System.out.println("1"); return; } if (d1.getMonth() > d2.getMonth()) { JOptionPane.showMessageDialog(null, "Invalid order of dates", "FAILURE", JOptionPane.ERROR_MESSAGE); System.out.println("2"); return; } int end = 0; int from = d1.getDate(); int to = d2.getDate(); int from_month = d1.getMonth(); int to_month = d2.getMonth(); if (from_month != to_month) { switch (from_month) { case 0: end = 31; break; case 1: end = 28; break; case 2: end = 31; break; case 3: end = 30; break; case 4: end = 31; break; case 5: end = 30; break; case 6: end = 31; break; case 7: end = 31; break; case 8: end = 30; break; case 9: end = 31; break; case 10: end = 30; break; case 11: end = 31; break; } String type = ""; if (rdbCleave.isSelected()) { type = "Casual Leave"; } else { type = "Sick Leave"; } bdb.put("ID", lblEmpid.getText()); bdb.put("From", d1.getDate()); bdb.put("To", end); bdb.put("Month", d1.getMonth()); bdb.put("Type", type); BasicDBObject bdb3 = new BasicDBObject(); bdb3.put("ID", lblEmpid.getText()); bdb3.put("From", 1); bdb3.put("To", d2.getDate()); bdb3.put("Month", d2.getMonth()); bdb3.put("Type", type); DBCollection dbc1 = j.connect("remaining_leaves"); BasicDBObject bdb1 = new BasicDBObject(); bdb1.put("ID", lblEmpid.getText()); DBCursor cur = dbc1.find(bdb1); DBObject obj = cur.next(); int rcleaves = (Integer) obj.get("Casual_leaves"); int rsleaves = (Integer) obj.get("Sick_leaves"); if (type.equals("Casual Leave")) { if (rcleaves >= end - d1.getDate()) { bdb.put("Paid_Casual", end - d1.getDate() + 1); bdb.put("Paid_Sick", 0); rcleaves -= end - d1.getDate(); } else { bdb.put("Paid_Casual", rcleaves + 1); bdb.put("Paid_Sick", 0); rcleaves = 0; } } else { if (rsleaves >= end - d1.getDate()) { bdb.put("Paid_Sick", end - d1.getDate() + 1); bdb.put("Paid_Casual", 0); rsleaves -= end - d1.getDate(); } else { bdb.put("Paid_Sick", rsleaves + 1); bdb.put("Paid_Casual", 0); rsleaves = 0; } } if (type.equals("Casual Leave")) { if (rcleaves >= d2.getDate() - 1) { bdb3.put("Paid_Casual", d2.getDate() - 1 + 1); bdb3.put("Paid_Sick", 0); rcleaves -= d2.getDate() - 1; } else { bdb3.put("Paid_Casual", rcleaves + 1); bdb3.put("Paid_Sick", 0); rcleaves = 0; } } else { if (rsleaves >= d2.getDate() - 1) { bdb3.put("Paid_Sick", d2.getDate() - 1 + 1); bdb3.put("Paid_Casual", 0); rsleaves -= d2.getDate() - 1; } else { bdb3.put("Paid_Sick", rsleaves + 1); bdb3.put("Paid_Casual", 0); rsleaves = 0; } } dbc.insert(bdb); dbc.insert(bdb3); bdb1.put("Casual_leaves", rcleaves); bdb1.put("Sick_leaves", rsleaves); dbc1.update(obj, bdb1); final JPanel p2 = new JPanel(); final JOptionPane jo = new JOptionPane(); JOptionPane.showMessageDialog(p2, "Leave Taken", "Success", JOptionPane.INFORMATION_MESSAGE); this.setVisible(false); } BasicDBObject bdb2 = new BasicDBObject(); bdb2.put("Month", from_month); bdb2.put("ID", lblEmpid.getText()); DBCursor cur1 = dbc.find(bdb2); while (cur1.hasNext()) { DBObject obj = cur1.next(); int from1 = (Integer) obj.get("From"); int to1 = (Integer) obj.get("To"); if (from >= from1 && from <= to1) { JOptionPane.showMessageDialog(null, "You have already taken leave in this period", "Failure", JOptionPane.ERROR_MESSAGE); return; } else if (to >= from1 && to <= to1) { JOptionPane.showMessageDialog(null, "You have already taken leave in this period", "Failure", JOptionPane.ERROR_MESSAGE); return; } else if (from <= from1 && to >= to1) { JOptionPane.showMessageDialog(null, "You have already taken leave in this period", "Failure", JOptionPane.ERROR_MESSAGE); return; } } String type = ""; if (rdbCleave.isSelected()) { type = "Casual Leave"; } else { type = "Sick Leave"; } bdb.put("ID", lblEmpid.getText()); bdb.put("From", d1.getDate()); bdb.put("To", d2.getDate()); bdb.put("Month", d1.getMonth()); bdb.put("Type", type); DBCollection dbc1 = j.connect("remaining_leaves"); BasicDBObject bdb1 = new BasicDBObject(); bdb1.put("ID", lblEmpid.getText()); DBCursor cur = dbc1.find(bdb1); DBObject obj = cur.next(); int rcleaves = (Integer) obj.get("Casual_leaves"); int rsleaves = (Integer) obj.get("Sick_leaves"); if (type.equals("Casual Leave")) { if (rcleaves >= d2.getDate() - d1.getDate()) { bdb.put("Paid_Casual", d2.getDate() - d1.getDate()); bdb.put("Paid_Sick", 0); rcleaves -= d2.getDate() - d1.getDate(); } else { bdb.put("Paid_Casual", rcleaves); bdb.put("Paid_Sick", 0); rcleaves = 0; } } else { if (rsleaves >= d2.getDate() - d1.getDate()) { bdb.put("Paid_Sick", d2.getDate() - d1.getDate()); bdb.put("Paid_Casual", 0); rsleaves -= d2.getDate() - d1.getDate(); } else { bdb.put("Paid_Sick", rsleaves); bdb.put("Paid_Casual", 0); rsleaves = 0; } } dbc.insert(bdb); bdb1.put("Casual_leaves", rcleaves); bdb1.put("Sick_leaves", rsleaves); dbc1.update(obj, bdb1); final JPanel p2 = new JPanel(); final JOptionPane jo = new JOptionPane(); JOptionPane.showMessageDialog(p2, "Leave Taken", "Success", JOptionPane.INFORMATION_MESSAGE); this.setVisible(false); }