List of usage examples for com.mongodb MongoClient MongoClient
public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation)
From source file:br.bireme.scl.CopyMongoDb.java
License:Open Source License
private static void copyDB(final String from_host, final String to_host, final String from_db, final String to_db, final String from_port, final String to_port, final boolean appendCollections, final boolean displayAllIds) throws UnknownHostException, IOException { assert from_host != null; assert to_host != null; assert from_db != null; assert to_db != null; assert from_port != null; assert to_port != null; final int MAX_LOOP_SIZE = 15000; // MongoException$CursorNotFound final MongoClient fromClient = new MongoClient(from_host, Integer.parseInt(from_port)); final MongoClient toClient = new MongoClient(to_host, Integer.parseInt(to_port)); final DB fromDb = fromClient.getDB(from_db); final DB toDb = toClient.getDB(to_db); if (!appendCollections) { toDb.dropDatabase();//w w w. ja va 2s . co m } final Set<String> colls = fromDb.getCollectionNames(); for (String cname : colls) { if (cname.equals("system.indexes")) { continue; } final DBCollection fromColl = fromDb.getCollection(cname); final DBCollection toColl = toDb.getCollection(cname); DBCursor cursor = fromColl.find(); int curr = 0; System.out.println("Copying collection: " + cname); while (cursor.hasNext()) { if (curr % MAX_LOOP_SIZE == 0) { if (curr > 0) { cursor.close(); cursor = fromColl.find().skip(curr); if (!cursor.hasNext()) { throw new IOException("hasNext() failed"); } } } final DBObject doc = cursor.next(); final WriteResult ret = toColl.save(doc, WriteConcern.ACKNOWLEDGED); if (!ret.getCachedLastError().ok()) { System.err.println("write error doc id=" + doc.get("_id")); } if (++curr % 1000 == 0) { System.out.println("+++" + curr); } if (displayAllIds) { System.out.println(" id=" + doc.get("_id")); } } cursor.close(); System.out.println(); } }
From source file:br.bireme.scl.Gizmo.java
License:Open Source License
Collection<Element> loadRecords(final String host, final int port, final String user, final String password) throws IOException { assert host != null; assert port > 0; final MongoClient mongoClient = new MongoClient(host, port); final DB db = mongoClient.getDB(SOCIAL_CHECK_DB); if ((user != null) && (!user.trim().isEmpty())) { final boolean auth = db.authenticate(user, password.toCharArray()); if (!auth) { throw new IllegalArgumentException("invalid user/password"); }/*from w w w . j a v a2s.c om*/ } final DBCollection coll = db.getCollection(HISTORY_COL); final String fldName = ELEM_LST_FIELD + ".0." + EXPORTED_FIELD; final BasicDBObject query = new BasicDBObject(fldName, false); final DBCursor cursor = coll.find(query); final Collection<Element> col = getNotExportedElements(coll, cursor); cursor.close(); return col; }
From source file:br.bireme.scl.JoinTitle.java
License:Open Source License
public JoinTitle(final String host, final int port) throws UnknownHostException { if (host == null) { throw new NullPointerException("host"); }/*w w w . j a v a2 s .c om*/ if (port <= 0) { throw new IllegalArgumentException("port <= 0"); } final MongoClient mongoClient = new MongoClient(host, port); mongoClient.dropDatabase(DB_NAME); db = mongoClient.getDB(DB_NAME); }
From source file:br.bireme.scl.ResetExportFlag.java
License:Open Source License
private static void reset(final String host, final int port, final String database, final String collection, final String sdate) throws UnknownHostException, ParseException { assert host != null; assert port > 0; assert database != null; assert collection != null; final MongoClient client = new MongoClient(host, port); final DB db = client.getDB(database); final DBCollection coll = db.getCollection(collection); final String prefix = ELEM_LST_FIELD + ".0."; final BasicDBObject query; final DBCursor cursor; if (sdate == null) { query = new BasicDBObject(prefix + EXPORTED_FIELD, true); } else {//from ww w . ja va 2s . c o m final SimpleDateFormat simple = new SimpleDateFormat("yyyyMMdd"); final Date date = simple.parse(sdate); final BasicDBList list = new BasicDBList(); list.add(new BasicDBObject(prefix + EXPORTED_FIELD, true)); list.add(new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", date))); query = new BasicDBObject("$and", list); } cursor = coll.find(query); while (cursor.hasNext()) { final BasicDBObject doc = (BasicDBObject) cursor.next(); final BasicDBList list = (BasicDBList) doc.get(ELEM_LST_FIELD); final BasicDBObject elem = (BasicDBObject) list.get(0); elem.put(EXPORTED_FIELD, false); coll.save(doc); } cursor.close(); }
From source file:br.bireme.scl.SendEmailToCcs.java
License:Open Source License
public void sendEmails(final String toFile, final String host, final int port, final String user, final String password, final String database, final String collection) throws UnknownHostException { if (toFile == null) { throw new NullPointerException("toFile"); }//from ww w . j ava2s . c o m if (host == null) { throw new NullPointerException("host"); } if (port <= 0) { throw new IllegalArgumentException("port <= 0"); } if (database == null) { throw new NullPointerException("database"); } if (collection == null) { throw new NullPointerException("collection"); } final Map<String, CcProfile> profiles = getProfiles(toFile); final MongoClient mongoClient = new MongoClient(host, port); final DB db = mongoClient.getDB(database); if (user != null) { final boolean auth = db.authenticate(user, password.toCharArray()); if (!auth) { throw new IllegalArgumentException("invalid user/password"); } } final DBCollection coll = db.getCollection(collection); prepareEmails(coll, profiles); }
From source file:br.bireme.scl.ShowBrokenLinks.java
License:Open Source License
public ShowBrokenLinks(final String host, final int port, final String user, final String password, final String database, final String collection) throws UnknownHostException { if (host == null) { throw new NullPointerException("host"); }/*from w w w . j a v a 2 s . c o m*/ if (port <= 0) { throw new IllegalArgumentException("port=" + port); } if (database == null) { throw new NullPointerException("database"); } if (collection == null) { throw new NullPointerException("collection"); } final MongoClient mongoClient = new MongoClient(host, port); final DB db = mongoClient.getDB(database); if (user != null) { final boolean auth = db.authenticate(user, password.toCharArray()); if (!auth) { throw new IllegalArgumentException("invalid user/password"); } } coll = db.getCollection(collection); }
From source file:br.bireme.scl.ShowFixedLinks.java
License:Open Source License
public ShowFixedLinks(final String host, final int port, final String user, final String password, final String database, final String collection) throws UnknownHostException { if (host == null) { throw new NullPointerException("host"); }/*from w ww .j a v a2 s .c om*/ if (port <= 0) { throw new IllegalArgumentException("port=" + port); } if (database == null) { throw new NullPointerException("database"); } if (collection == null) { throw new NullPointerException("collection"); } final MongoClient mongoClient = new MongoClient(host, port); final DB db = mongoClient.getDB(database); if (user != null) { final boolean auth = db.authenticate(user, password.toCharArray()); if (!auth) { throw new IllegalArgumentException("invalid user/password"); } } coll = db.getCollection(collection); }
From source file:br.bireme.scl.TabulateField.java
License:Open Source License
public static void tabulate(final String host, final int port, final String user, final String password, final String database, final String collection, final String path) throws UnknownHostException { if (host == null) { throw new NullPointerException("host"); }/*from w w w . j a v a 2s .co m*/ if (port <= 0) { throw new IllegalArgumentException("port <= 0"); } if (collection == null) { throw new NullPointerException("collection"); } if (path == null) { throw new NullPointerException("path"); } final MongoClient mongoClient = new MongoClient(host, port); final DB db = mongoClient.getDB(database); if (user != null) { final boolean auth = db.authenticate(user, password.toCharArray()); if (!auth) { throw new IllegalArgumentException("invalid user/password"); } } final DBCollection coll = db.getCollection(collection); final DBCursor cursor = coll.find(); final TreeMap<String, Integer> map1 = new TreeMap<String, Integer>(); final TreeMap<Integer, String> map2 = new TreeMap<Integer, String>(); final int ADD_VALUE = 99999999; final int size = cursor.size(); while (cursor.hasNext()) { final BasicDBObject doc = (BasicDBObject) cursor.next(); final String key = getValue(doc, path); if (key != null) { Integer val = map1.get(key); if (val == null) { val = 0; } val += 1; map1.put(key, val); } } cursor.close(); for (Map.Entry<String, Integer> entry : map1.entrySet()) { map2.put(ADD_VALUE - entry.getValue(), entry.getKey()); } System.out.println("Total # of docs: " + size + "\n"); int idx = 0; for (Map.Entry<Integer, String> entry : map2.entrySet()) { final int val = ADD_VALUE - entry.getKey(); final String percent = String.format("%.2f", ((float) val / size) * 100); System.out.println((++idx) + ") " + entry.getValue() + ": " + val + " (" + percent + "%)"); } }
From source file:br.bireme.scl.UndoUpdate.java
License:Open Source License
private static void undo(final String host, final int port, final String database, final String fromDate, final String docId) throws UnknownHostException, ParseException { assert host != null; assert port > 0; assert database != null; assert (fromDate != null || docId != null); final MongoClient client = new MongoClient(host, port); final DB db = client.getDB(database); final DBCollection from_coll = db.getCollection(HISTORY_COL); final DBCollection to_coll = db.getCollection(BROKEN_LINKS_COL); final String prefix = ELEM_LST_FIELD + ".0."; final BasicDBObject query; if (fromDate == null) { query = new BasicDBObject("_id", docId); } else {/* w ww .jav a 2s . c o m*/ final SimpleDateFormat simple = new SimpleDateFormat( "yyyyMMdd" + (fromDate.contains(":") ? "-HH:mm:ss" : "")); final Date fDate = simple.parse(fromDate); query = new BasicDBObject(prefix + LAST_UPDATE_FIELD, new BasicDBObject("$gte", fDate)); } final DBCursor cursor = from_coll.find(query); int total = 0; int reverted = 0; System.out.println("host=" + host); System.out.println("port=" + port); System.out.println("database=" + database); if (fromDate == null) { System.out.println("doc id=" + docId); } else { System.out.println("from date=" + fromDate); } System.out.println("found=" + cursor.size()); while (cursor.hasNext()) { final BasicDBObject doc_from = (BasicDBObject) cursor.next(); final BasicDBObject doc_to = new BasicDBObject(doc_from); final String id = doc_from.getString(ID_FIELD); final BasicDBList list = (BasicDBList) doc_from.get(ELEM_LST_FIELD); final BasicDBObject elem = (BasicDBObject) list.get(0); final Date date = elem.getDate(LAST_UPDATE_FIELD); doc_to.put(LAST_UPDATE_FIELD, date); doc_to.put(BROKEN_URL_FIELD, elem.getString(BROKEN_URL_FIELD)); doc_to.put(MSG_FIELD, elem.getString(MSG_FIELD)); doc_to.put(CENTER_FIELD, elem.get(CENTER_FIELD)); doc_to.removeField(ELEM_LST_FIELD); final WriteResult wr = to_coll.save(doc_to, WriteConcern.ACKNOWLEDGED); if (wr.getCachedLastError().ok()) { final WriteResult wr2 = from_coll.remove(doc_from, WriteConcern.ACKNOWLEDGED); if (wr2.getCachedLastError().ok()) { reverted++; System.out.println("+++id=" + id + " date=" + date); } else { System.err.println("Document[" + id + "] delete error."); } } else { System.err.println("Document[" + id + "] update error."); } total++; } cursor.close(); System.out.println("total/undo: " + total + "/" + reverted); }
From source file:br.bireme.tmp.AddPrettyField.java
License:Open Source License
private static void add(final String mongo_host, final int mongo_port, final String mongo_db, final String mongo_col) throws UnknownHostException { assert mongo_host != null; assert mongo_port > 0; assert mongo_db != null; assert mongo_col != null; final MongoClient client = new MongoClient(mongo_host, mongo_port); final DB db = client.getDB(mongo_db); final DBCollection coll = db.getCollection(HISTORY_COL); final DBCursor cursor = coll.find(); int total = 0; System.out.println("host=" + mongo_host); System.out.println("port=" + mongo_port); System.out.println("database=" + mongo_db); System.out.println("collection=" + mongo_col); System.out.println("num of documents=" + cursor.size()); while (cursor.hasNext()) { final BasicDBObject doc1 = (BasicDBObject) cursor.next(); final BasicDBList list = (BasicDBList) doc1.get(ELEM_LST_FIELD); for (Object obj : list) { final BasicDBObject doc2 = (BasicDBObject) obj; if (!doc2.containsField(PRETTY_BROKEN_URL_FIELD)) { final String burl = doc2.getString(BROKEN_URL_FIELD); try { final String pburl = EncDecUrl.decodeUrl(burl); doc2.append(PRETTY_BROKEN_URL_FIELD, pburl); final WriteResult wr = coll.save(doc1, WriteConcern.ACKNOWLEDGED); if (wr.getCachedLastError().ok()) { total++;// w w w. j ava 2 s. c o m } else { System.err.println("Document[" + doc1.getString("_id") + "] update error."); } } catch (IOException ioe) { System.err.println("Document[" + doc1.getString("_id") + "] bad encode conversion" + " url=[" + burl + "]"); } } } } cursor.close(); System.out.println("num of added fields: " + total); }