List of usage examples for com.mongodb MongoClient getDatabase
public MongoDatabase getDatabase(final String databaseName)
From source file:flipkart.mongo.node.discovery.ReplicaDiscovery.java
License:Apache License
private List<ReplicaSetConfig> getMongoSReplicaSets() throws MongoDiscoveryException { List<ReplicaSetConfig> replicaSetConfigs = Lists.newArrayList(); MongoClient client = MongoConnector.getMongoClient(configSvrNodes); FindIterable<Document> documents = client.getDatabase(CONFIG_DB_NAME).getCollection(CONFIG_TABLE_NAME) .find();/*from w w w. j a v a 2s . c o m*/ for (Document document : documents) { String shardName = (String) document.get("_id"); String hostString = (String) document.get("host"); List<Node> replicaNodes = this.getReplicaNodes(hostString); if (!replicaNodes.isEmpty()) { ReplicaSetConfig replicaConfig = new ReplicaSetConfig(shardName, replicaNodes); replicaSetConfigs.add(replicaConfig); } } return replicaSetConfigs; }
From source file:flipkart.mongo.replicator.node.ReplicationTask.java
License:Apache License
@Override public void run() { String shardId = rsConfig.shardName; Node masterNode = rsConfig.getMasterNode().get(); MongoClient client = MongoConnector.getMongoClient(Lists.newArrayList(masterNode)); MongoDatabase database = client.getDatabase("local"); lastCp = taskContext.checkPointHandler.getCheckPoint(shardId); logger.info(String.format("######## START REPLICATOR FOR MongoURI: %s. LastCheckpoint: %s #######", client.getAddress(), lastCp)); MongoCollection<Document> collection = database.getCollection("oplog.rs"); FindIterable<Document> iterable; MongoCursor<Document> cursor; do {//from w w w .j av a 2 s . c om if (lastCp == null) { iterable = collection.find(); } else { iterable = collection.find(new Document("ts", new Document("$gt", lastCp))); } cursor = iterable.sort(new Document("$natural", 1)).noCursorTimeout(true) .cursorType(CursorType.TailableAwait).batchSize(3000).iterator(); try { executeCursor(cursor); Thread.sleep(WAIT_FOR_NEXT_ITERATION); } catch (MongoCursorNotFoundException e) { logger.info("Cursor has been closed. About to open a new cursor. ID: " + cursor.getServerCursor().getId()); } catch (Exception e) { logger.error("Exception while replicating", e); throw new RuntimeException(e); } finally { cursor.close(); } } while (true); }
From source file:foam.dao.MongoDAO.java
License:Open Source License
public MongoDAO(String host, int port, String dbName, String collectionName, String username, String password) { if (SafetyUtil.isEmpty(dbName) || SafetyUtil.isEmpty(collectionName)) { throw new IllegalArgumentException("Illegal arguments"); }// ww w .j a v a 2 s .c o m host = (host != null) ? host : "localhost"; MongoClient mongoClient; if (isUserPassProvided(username, password)) { MongoCredential credential = MongoCredential.createCredential(username, dbName, password.toCharArray()); mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential)); } else { mongoClient = new MongoClient(host, port); } this.database = mongoClient.getDatabase(dbName); this.collectionName = collectionName; }
From source file:fr.utbm.repository.MongoDBDao.java
public void subscribeClientToCourseSession(Client cl) { MongoClient mongoClient = null; try {//from w w w. j av a2 s . com mongoClient = new MongoClient("localhost", 27017); MongoDatabase db; db = mongoClient.getDatabase("SCHOOL"); MongoCollection<Document> collection = db.getCollection("CLIENTS"); Document doc = new Document(); doc.put("lastName", cl.getLastName()); doc.put("firstName", cl.getFirstName()); doc.put("address", cl.getAddress()); doc.put("phone", cl.getPhone()); doc.put("email", cl.getEmail()); doc.put("courseSessionId", cl.getCourseSessionId().getId()); collection.insertOne(doc); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } finally { mongoClient.close(); } }
From source file:fucksocks.utils.MongoDBUtil.java
License:Apache License
public <T> T doJob(MongoDBCallback<T> callback) { MongoClient client = null; T t = null;/*w w w.ja v a2 s . c o m*/ try { client = new MongoClient(host, port); MongoDatabase database = client.getDatabase(databaseName); MongoCollection<Document> collection = database.getCollection(collectionName); t = callback.process(collection); } finally { if (client != null) { client.close(); } } return t; }
From source file:geriapp.dao.ReadingDAO.java
public static void storeReading(Reading reading) { MongoClient mongo = new MongoClient("54.254.204.169", 27017); MongoDatabase db = mongo.getDatabase("GERI"); MongoCollection newColl;//from ww w . j av a 2 s .c o m if (reading instanceof MedboxReading) { newColl = db.getCollection("Medbox"); Document medboxReading = new Document("gw_id", ((MedboxReading) reading).getGw_id()); medboxReading.append("server_timestamp", ((MedboxReading) reading).getServer_timestamp()); medboxReading.append("sequence", ((MedboxReading) reading).getSequence()); medboxReading.append("gw_timestamp", ((MedboxReading) reading).getGw_timestamp()); medboxReading.append("sensor_id", ((MedboxReading) reading).getSensor_id()); medboxReading.append("reed_val", ((MedboxReading) reading).getReed_val()); newColl.insertOne(medboxReading); //TODO: append reading attributes } else if (reading instanceof DoorReading) { newColl = db.getCollection("Door"); Document doorReading = new Document("gw_id", ((DoorReading) reading).getGw_id()); doorReading.append("server_timestamp", ((DoorReading) reading).getServer_timestamp()); doorReading.append("sequence", ((DoorReading) reading).getSequence()); doorReading.append("gw_timestamp", ((DoorReading) reading).getGw_timestamp()); doorReading.append("sensor_id", ((DoorReading) reading).getSensor_id()); doorReading.append("reed_val", ((DoorReading) reading).getReed_val()); newColl.insertOne(doorReading); } mongo.close(); }
From source file:geriapp.dao.ReadingDAO.java
public static Reading getLatestReading(String type) {//Change later to ID instead of type MongoClient mongo = new MongoClient("54.254.204.169", 27017); MongoDatabase db = mongo.getDatabase("GERI"); MongoCollection<Document> newColl; Gson gson = new Gson(); if (type.equals("medbox")) { newColl = db.getCollection("Medbox"); Document latestEntry = newColl.find().iterator().next(); String json = latestEntry.toJson(); Reading reading = gson.fromJson(json, MedboxReading.class); mongo.close();//from ww w . jav a2 s. c o m return reading; } else if (type.equals("door")) { } return null; //throw Exception?? }
From source file:geriapp.dao.ReadingDAO.java
public static Reading getReadingsBetween(String type, Timestamp startTime, Timestamp endTime) { MongoClient mongo = new MongoClient("54.254.204.169", 27017); MongoDatabase db = mongo.getDatabase("GERI"); MongoCollection<Document> newColl; Gson gson = new Gson(); if (type.equals("medbox")) { newColl = db.getCollection("Medbox"); Document latestEntry = newColl.find().iterator().next(); String json = latestEntry.toJson(); MedboxReading reading = gson.fromJson(json, MedboxReading.class); String thisTimestamp = reading.getGw_timestamp(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date parsedTimestamp = null; try {/*w ww.java2 s .c o m*/ parsedTimestamp = df.parse(thisTimestamp); } catch (ParseException e) { return null; } Timestamp gwTimestamp = new Timestamp(parsedTimestamp.getTime()); mongo.close(); if (gwTimestamp.after(startTime) && gwTimestamp.before(endTime)) { return reading; } } else if (type.equals("door")) { newColl = db.getCollection("Door"); Document latestEntry = newColl.find().iterator().next(); String json = latestEntry.toJson(); DoorReading reading = gson.fromJson(json, DoorReading.class); String thisTimestamp = reading.getGw_timestamp(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date parsedTimestamp = null; try { parsedTimestamp = df.parse(thisTimestamp); } catch (ParseException e) { return null; } Timestamp gwTimestamp = new Timestamp(parsedTimestamp.getTime()); mongo.close(); if (gwTimestamp.after(startTime) && gwTimestamp.before(endTime)) { return reading; } } return null; //throw Exception?? }
From source file:geriapp.dao.ReadingDAO.java
public static int getPastReadingsCountBetween(String type, Timestamp startTime, Timestamp endTime) { MongoClient mongo = new MongoClient("54.254.204.169", 27017); MongoDatabase db = mongo.getDatabase("GERI"); int size = 0; MongoCollection<Document> newColl; Gson gson = new Gson(); if (type.equals("medbox")) { newColl = db.getCollection("Medbox"); MongoCursor<Document> iterator = newColl.find().iterator(); Document latestEntry = null; boolean run = true; ArrayList<MedboxReading> results = new ArrayList<MedboxReading>(); while (run) { latestEntry = iterator.next(); if (latestEntry == null) { run = false;/*from w ww. j ava 2 s . c o m*/ size = 121; break; } String json = latestEntry.toJson(); MedboxReading reading = gson.fromJson(json, MedboxReading.class); String thisTimestamp = reading.getGw_timestamp(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date parsedTimestamp = null; try { parsedTimestamp = df.parse(thisTimestamp); //System.out.println(""+parsedTimestamp); } catch (ParseException e) { e.printStackTrace(); run = false; } Timestamp gwTimestamp = new Timestamp(parsedTimestamp.getTime()); //if (gwTimestamp.after(startTime)) { if (gwTimestamp.after(startTime) && gwTimestamp.before(endTime)) { results.add(reading); } if (!iterator.hasNext()) { run = false; } } /* while (iterator.hasNext()) { latestEntry = iterator.next(); String json = latestEntry.toJson(); MedboxReading reading = gson.fromJson(json, MedboxReading.class); results.add(reading); } */ mongo.close(); size = results.size(); return size; } else if (type.equals("door")) { newColl = db.getCollection("Door"); MongoCursor<Document> iterator = newColl.find().iterator(); Document latestEntry = null; boolean run = true; ArrayList<DoorReading> results = new ArrayList<DoorReading>(); while (run) { latestEntry = iterator.next(); if (latestEntry == null) { run = false; size = 121; break; } String json = latestEntry.toJson(); DoorReading reading = gson.fromJson(json, DoorReading.class); String thisTimestamp = reading.getGw_timestamp(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date parsedTimestamp = null; try { parsedTimestamp = df.parse(thisTimestamp); //System.out.println(""+parsedTimestamp); } catch (ParseException e) { e.printStackTrace(); run = false; } Timestamp gwTimestamp = new Timestamp(parsedTimestamp.getTime()); //if (gwTimestamp.after(startTime)) { if (gwTimestamp.after(startTime) && gwTimestamp.before(endTime)) { results.add(reading); } if (!iterator.hasNext()) { run = false; } } /* while (iterator.hasNext()) { latestEntry = iterator.next(); String json = latestEntry.toJson(); MedboxReading reading = gson.fromJson(json, MedboxReading.class); results.add(reading); } */ mongo.close(); size = results.size(); return size; } return size; //throw Exception?? }
From source file:geriapp.servlets.BootstrapServlet.java
private String storeInDatabase(String pathToFile, String dbName, String collectionName) { String output;/*from www . jav a2s. co m*/ try { MongoClient mongo = new MongoClient("localhost", 27017); MongoDatabase db = mongo.getDatabase(dbName); output = BootstrapController.importJSONFileToDB(pathToFile, db, collectionName); } catch (Exception e) { return "Database connection error"; } return output; }