public void getIssueAndPull() throws Exception { MongoClient mongoClient = new MongoClient(MongoInfo.getMongoServerIp(), 27017); MongoDatabase database = mongoClient.getDatabase("ghcrawlerV3"); FindIterable<Document> issueIterable = database.getCollection("issueandpull").find(); Connection connection = MysqlInfo.getMysqlConnection(); connection.setAutoCommit(false); String sql = "update repotest set open_issues = ?,closed_issues = ?,open_pull=?,closed_pull=? where full_name = ?"; PreparedStatement stmt = connection.prepareStatement(sql); JsonParser parser = new JsonParser(); for (Document document : issueIterable) { String json = document.toJson(); JsonObject repoIssue = parser.parse(json).getAsJsonObject(); int openIssue = repoIssue.get("openissue").getAsInt(); int closedIssue = repoIssue.get("closedissue").getAsInt(); int openPull = repoIssue.get("openpull").getAsInt(); int closedPull = repoIssue.get("closedpull").getAsInt(); String repoName = repoIssue.get("fn").getAsString(); System.out.println(repoName); stmt.setInt(1, openIssue); stmt.setInt(2, closedIssue); stmt.setInt(3, openPull); stmt.setInt(4, closedPull); stmt.setString(5, repoName); stmt.execute(); } connection.commit(); connection.close(); mongoClient.close(); }
public void getCommitCount() throws Exception { MongoClient mongoClient = new MongoClient(MongoInfo.getMongoServerIp(), 27017); MongoDatabase database = mongoClient.getDatabase("ghcrawlerV3"); FindIterable<Document> issueIterable = database.getCollection("commitnumber").find(); Connection connection = MysqlInfo.getMysqlConnection(); connection.setAutoCommit(false); JsonParser parser = new JsonParser(); for (Document document : issueIterable) { String json = document.toJson(); JsonObject repoJsonObject = parser.parse(json).getAsJsonObject(); int commit = repoJsonObject.get("commitnumber").getAsInt(); String full_name = repoJsonObject.get("fn").getAsString(); System.out.println(full_name); String sql = "update repotest set commit = ? where full_name = ?"; PreparedStatement stmt = connection.prepareStatement(sql); stmt.setInt(1, commit); stmt.setString(2, full_name); stmt.execute(); } connection.commit(); connection.close(); mongoClient.close(); }
public static void updateSQLDB(String mongoHost, int mongoPort, String mongoDb) { MongoClient mongoClient = new MongoClient(mongoHost, mongoPort); MongoDatabase db = mongoClient.getDatabase(mongoDb); FindIterable<Document> iterable = db.getCollection("flights").find(); iterable.forEach( new Block<Document>() { public void apply(final Document document) { List<FlightLeg> legs = FlightToLegs.getInstance().getLegsFromFlightRecord(document); for (FlightLeg leg : legs) { FlightLegDAO legDAO = new FlightLegDAOJDBCImpl(); int result = legDAO.create(leg); if (result == 0) { System.out.println( "[" + result + "]: ERROR Leg Creation:" + leg.getDepartureAirportCode() + "->" + leg.getArrivalAirportCode() + " DepartureUTC: " + leg.getDepartureTimeUTC() + " ArrivalUTC: " + leg.getArrivalTimeUTC()); System.exit(0); } } } }); System.out.println("Done"); mongoClient.close(); }
public static void main(String[] args) throws UnknownHostException { MongoClient mongo = new MongoClient("localhost", 27017); DB feederDB = mongo.getDB("feeder"); DBCollection products = feederDB.getCollection("products"); System.out.println(products.find().size()); mongo.close(); }
@Override public boolean stop() { if (client != null) { client.close(); } return true; }
public static void updateMongoDB( String mongoHost, int mongoPort, String mongoDb, String flightsCol, String legsCollection) { MongoClient mongoClient = new MongoClient(mongoHost, mongoPort); MongoDatabase db = mongoClient.getDatabase(mongoDb); FindIterable<Document> iterable = db.getCollection(flightsCol).find(); db.getCollection(legsCollection).createIndex(new Document("flightID", 1)); db.getCollection(legsCollection).createIndex(new Document("departureAirport._id", 1)); db.getCollection(legsCollection).createIndex(new Document("arrivalAirport._id", 1)); db.getCollection(legsCollection).createIndex(new Document("effectiveDate", 1)); db.getCollection(legsCollection).createIndex(new Document("discontinuedDate", 1)); iterable.forEach( new Block<Document>() { public void apply(final Document document) { try { List<FlightLeg> legs = FlightToLegs.getInstance().getLegsFromFlightRecord(document); for (FlightLeg leg : legs) { FlightLegDAO legDAO = new FlightLegDAOMongoImpl(); legDAO.setHost(mongoHost); legDAO.setPort(mongoPort); legDAO.setDB(mongoDb); legDAO.create(leg); } } catch (Exception e) { e.printStackTrace(); } } }); System.out.println("Done"); mongoClient.close(); }
@Override public void run() { MongoProfile profile = options.getProfile(); MongoClient mongo = null; DBCursor cursor = null; try { mongo = new MongoClient(profile.getAddress(), profile.getCredentials()); DB db = mongo.getDB(options.getDatabase()); DBCollection col = db.getCollection(options.getCollection()); cursor = col.find(); while (cursor.hasNext()) { DBObject doc = cursor.next(); Map<String, Object> m = convert(doc); pushPipe(new Row(m)); } } catch (Throwable t) { slog.error("araqne logdb mongo: cannot run mongo.find", t); } finally { if (cursor != null) cursor.close(); if (mongo != null) mongo.close(); } }
private static void insertTest1(Document obj) { MongoClient client = new MongoClient("127.0.0.1", 27017); MongoDatabase db = client.getDatabase("test"); MongoCollection<Document> collection = db.getCollection("Test_table1"); collection.insertOne(obj); client.close(); }
public void getCollaborators() throws Exception { // get mysql connection Connection connection = MysqlInfo.getMysqlConnection(); connection.setAutoCommit(false); String conSql = "insert into collaborator(user_id,repo_id) values(?,?);"; PreparedStatement conStmt = connection.prepareStatement(conSql); String repoSql = "update repotest set collaborator = ? where id = ?"; PreparedStatement repoStmt = connection.prepareStatement(repoSql); // get repos from mongo MongoClient mongoClient = new MongoClient(MongoInfo.getMongoServerIp(), 27017); MongoDatabase database = mongoClient.getDatabase("ghcrawlerV3"); FindIterable<Document> repoIterable = database.getCollection("repo").find(); JsonParser parser = new JsonParser(); Map<String, Integer> repoMap = new HashMap<String, Integer>(); for (Document document : repoIterable) { String json = document.toJson(); JsonObject repoJsonObject = parser.parse(json).getAsJsonObject(); int id = repoJsonObject.get("id").getAsInt(); String full_name = repoJsonObject.get("full_name").getAsString(); System.out.println(id); repoMap.put(full_name, id); } Map<Integer, Integer> collaboratorMap = new HashMap<Integer, Integer>(); FindIterable<Document> collaboratorIterable = database.getCollection("assignees").find(); for (Document document : collaboratorIterable) { String json = document.toJson(); JsonObject contriJsonObject = parser.parse(json).getAsJsonObject(); int id = contriJsonObject.get("id").getAsInt(); String repoName = contriJsonObject.get("fn").getAsString(); int repo_id = repoMap.get(repoName); conStmt.setInt(1, id); conStmt.setInt(2, repo_id); conStmt.execute(); if (collaboratorMap.containsKey(repo_id)) { collaboratorMap.put(repo_id, collaboratorMap.get(repo_id) + 1); } else { collaboratorMap.put(repo_id, 1); } } Set<Integer> keySet = collaboratorMap.keySet(); for (Integer repoId : keySet) { int contri_count = collaboratorMap.get(repoId); repoStmt.setInt(1, contri_count); repoStmt.setInt(2, repoId); repoStmt.execute(); } mongoClient.close(); connection.commit(); conStmt.close(); repoStmt.close(); connection.close(); }
private void waitUntilConfigurationSpreadAcrossServersFromDefaultConnection() throws UnknownHostException { MongoClient mongoClient = getDefaultMongoClient(); waitingToBecomeStable(mongoClient); mongoClient.close(); }
/** Returns DBObject representing object with given id */ private DBObject getById(MongoDbServer entity, String id) throws Exception { MongoClient mongoClient = new MongoClient(entity.getAttribute(SoftwareProcess.HOSTNAME)); try { DB db = mongoClient.getDB(TEST_DB); DBCollection testCollection = db.getCollection(TEST_COLLECTION); return testCollection.findOne(new BasicDBObject("_id", new ObjectId(id))); } finally { mongoClient.close(); } }
private static void importToMongoDB( String host, int port, String databaseName, String collectionName, List<Document> docList) { MongoClient client = new MongoClient(host, port); MongoDatabase database = client.getDatabase(databaseName); MongoCollection<Document> collection = database.getCollection(collectionName); collection.insertMany(docList); client.close(); }
@POST @Path("/checkUser") @Consumes("application/json") @Produces(MediaType.APPLICATION_JSON) public String checkUser(String emailJson) { JSONObject res; StringBuilder sb = new StringBuilder(); String response; try { mongoClientUri = new MongoClientURI(uri); mongoClient = new MongoClient(mongoClientUri); db = mongoClient.getDB(mongoClientUri.getDatabase()); dbCollection = db.getCollection(COLLECTIONNAME); JSONObject obj = new JSONObject(emailJson); String email = obj.getString("email"); System.out.println("JSON: " + obj.toString() + "\n"); BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("email", email); DBCursor dbCursor = dbCollection.find(basicDBObject); while (dbCursor.hasNext()) { sb.append(dbCursor.next().toString()); // System.out.println(dbCursor.next()); } response = sb.toString(); mongoClient.close(); if (response.length() > 0) return response; return null; } catch (Exception e) { mongoClient.close(); System.out.println("EXCEPTION: " + e.getMessage() + " : " + e.getCause() + "\n"); e.printStackTrace(); return null; } }
/** cleanup. */ @PreDestroy public void closeClients() { LOGGER.info("Spring Container is destroy! Customer clean up"); try { jestClient.shutdownClient(); mongoClient.close(); } catch (Exception e) { LOGGER.error("Exception while closing {}", e.getMessage(), e); } LOGGER.info("Spring Container is destroy! Customer clean up completed"); }
public void waitUntilReplicaSetBecomesStable() { MongoClient mongoClient; try { mongoClient = getAvailableServersMongoClient(); } catch (UnknownHostException e) { throw new IllegalArgumentException(e); } waitingToBecomeStable(mongoClient); mongoClient.close(); }
public boolean delete(String id) { MongoClientURI uri = new MongoClientURI("mongodb://*****:*****@ds015909.mlab.com:15909/lab8_ase"); MongoClient client = new MongoClient(uri); DB db = client.getDB(uri.getDatabase()); DBCollection Users = db.getCollection("userdata"); BasicDBObject query = new BasicDBObject(); ObjectId oid = new ObjectId(id); query.put("_id", oid); Users.remove(query); client.close(); return true; }
/** Inserts new object with { key: value } at given server, returns new document's id */ private String insert(MongoDbServer entity, String key, Object value) throws Exception { MongoClient mongoClient = new MongoClient(entity.getAttribute(SoftwareProcess.HOSTNAME)); try { DB db = mongoClient.getDB(TEST_DB); DBCollection testCollection = db.getCollection(TEST_COLLECTION); BasicDBObject doc = new BasicDBObject(key, value); testCollection.insert(doc); ObjectId id = (ObjectId) doc.get("_id"); return id.toString(); } finally { mongoClient.close(); } }
protected void clearMongoDB(MongoDBRepositoryDescriptor descriptor) throws UnknownHostException { MongoClient mongoClient = MongoDBRepository.newMongoClient(descriptor); try { DBCollection coll = MongoDBRepository.getCollection(descriptor, mongoClient); coll.dropIndexes(); coll.remove(new BasicDBObject()); coll = MongoDBRepository.getCountersCollection(descriptor, mongoClient); coll.dropIndexes(); coll.remove(new BasicDBObject()); } finally { mongoClient.close(); } }
@Test public void testConnection() throws Exception { MongoClient mongoClient; try { mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); assertNotNull(db); mongoClient.close(); } catch (UnknownHostException e) { throw e; } }
public boolean updatedb(String input, String username) { MongoClientURI uri = new MongoClientURI("mongodb://*****:*****@ds037824.mongolab.com:37824/ase"); MongoClient client = new MongoClient(uri); DB db = client.getDB(uri.getDatabase()); DBCollection Users = db.getCollection("Patients"); DBObject inputDBObj = (DBObject) JSON.parse(input); System.out.println(inputDBObj.toString()); BasicDBObject searchQuery = new BasicDBObject().append("username", username); Users.update(searchQuery, inputDBObj); client.close(); return true; }
@Override public void close() { try { if (cursor != null) { cursor.close(); } } catch (Exception e) { LOGGER.warn("Error closing MongoDB cursor", e); } try { client.close(); } catch (Exception e) { LOGGER.warn("Error closing MongoDB client", e); } }
public void insertVehicleEventLog(List<VehicleEventLog> vehicleEventLogs) { MessageParserServiceConfig config = new MessageParserServiceConfig(); try (MongoClient mongoClient = new MongoClient( config.getConfig("mongo.db.host"), config.getConfigInt("mongo.db.port")); ) { MongoDatabase database = mongoClient.getDatabase("VehicleTrackingDB"); MongoCollection<Document> collection = database.getCollection("VehicleEventLog"); List<Document> documents = new ArrayList<Document>(); for (VehicleEventLog vehicleEventLog : vehicleEventLogs) { documents.add(vehicleEventLog.toDocument()); } collection.insertMany(documents); mongoClient.close(); } }
@Override public void after() { if (client != null) { client.close(); client = null; } if (mongoProc != null) { mongoProc.stop(); mongoProc = null; } if (mongoExec != null) { mongoExec.stop(); mongoExec = null; } }
public static ForensicReport getReport(String urlHash, String mongoHostIP) throws UnknownHostException { MongoClient mongoclient = new MongoClient(mongoHostIP, 27017); Morphia morphia = new Morphia(); morphia.map(ForensicReport.class).map(dqReport.class); Datastore ds = new Morphia().createDatastore(mongoclient, "ForensicDatabase"); ds.ensureCaps(); ForensicReport report = ds.get(ForensicReport.class, urlHash); if (report != null) { JsonParser parser = new JsonParser(); JsonObject tmpJson = parser.parse(report.metadataStringReport).getAsJsonObject(); GsonBuilder builder = new GsonBuilder(); report.metadataObjectReport = builder.create().fromJson(tmpJson, Object.class); } mongoclient.close(); return report; }
public String queryReturnEmployee(String name) throws Exception { ToJson converter = new ToJson(); // JSONArray json = new JSONArray(); String json = ""; try { /** ** Connect to MongoDB *** */ // Since 2.10.0, uses MongoClient // MongoClient mongo = new MongoClient("localhost", 27017); MongoClient mongo = new MongoClient("54.212.250.21", 27017); /** ** Get database *** */ // if database doesn't exists, MongoDB will create it for you DB db = mongo.getDB("telemedicine"); /** ** Get collection / table from 'testdb' *** */ // if collection doesn't exists, MongoDB will create it for you DBCollection table = db.getCollection("person"); /** ** Find and display *** */ BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("name", name); DBCursor cursor = table.find(searchQuery).limit(5000); while (cursor.hasNext()) { System.out.println(cursor.next()); } json = converter.toJsonArray(cursor); mongo.close(); } catch (MongoException e) { e.printStackTrace(); } catch (Exception Error) { Error.printStackTrace(); return json; } finally { // if (conn != null) conn.close(); } return json; }
@SuppressWarnings("deprecation") @POST @Path("/register") @Consumes("application/json") @Produces(MediaType.TEXT_PLAIN) public String registerUser(String jsonStr) { mongoClientUri = new MongoClientURI(uri); mongoClient = new MongoClient(mongoClientUri); db = mongoClient.getDB(mongoClientUri.getDatabase()); dbCollection = db.getCollection(COLLECTIONNAME); JSONObject obj = new JSONObject(jsonStr); firstName = obj.getString("first_name"); lastName = obj.getString("last_name"); email = obj.getString("email"); phone = obj.getString("phone_number"); countryCode = obj.getString("country_code"); imei = obj.getString("device_id"); nameToneDir = obj.getString("name_tone"); System.out.println("FIRST NAME: " + firstName + "\n"); System.out.println("LAST NAME: " + lastName + "\n"); System.out.println("EMAIL: " + email + "\n"); System.out.println("PHONE NUMBER: " + phone + "\n"); System.out.println("COUNTRY CODE: " + countryCode + "\n"); System.out.println("IMEI: " + imei + "\n"); System.out.println("NAME TONE DIR: " + nameToneDir + "\n"); BasicDBObject doc = new BasicDBObject("first_name", firstName) .append("last_name", lastName) .append("email", email) .append("phone_number", phone) .append("country_code", countryCode) .append("device_imei", imei) .append("name_tone_dir", nameToneDir); dbCollection.insert(doc); mongoClient.close(); return "SUCCESS"; }
public Usage getMdnUsageDetail(String mdn) { Usage usage = null; MongoClient mongo = null; String database = "usage"; try { Map<String, Object> dbMap = DataUtils.getConnection(); DB db = (DB) dbMap.get("db"); mongo = (MongoClient) dbMap.get("mongo"); /*MongoClientURI uri = new MongoClientURI("mongodb://*****:*****@ds051863.mongolab.com:51863/CloudFoundry_omfu0lp3_t4cigvf3"); mongo = new MongoClient(uri); DB db = mongo.getDB(uri.getDatabase());*/ /*if(!DataUtils.auth){ DataUtils.auth = db.authenticate("yoga", "test123".toCharArray()); System.out.println("db authenticated "+DataUtils.auth); }*/ DBCollection col = db.getCollection("usage"); DBObject query = BasicDBObjectBuilder.start().add("mdn", mdn).get(); DBCursor cursor = col.find(query); ObjectMapper objectMapper = new ObjectMapper(); if (cursor.hasNext()) { // System.out.println(cursor.next()); DBObject obj = cursor.next(); usage = objectMapper.readValue(obj.toString(), Usage.class); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { mongo.close(); } return usage; }
private CommandResult runCommandToAdmin(ConfigurationDocument cmd) throws UnknownHostException { MongoClient mongoClient = getDefaultMongoClient(); CommandResult commandResult = null; if (this.replicaSetGroup.isAuthenticationSet()) { commandResult = MongoDbCommands.replicaSetInitiate( mongoClient, cmd, this.replicaSetGroup.getUsername(), this.replicaSetGroup.getPassword()); } else { commandResult = MongoDbCommands.replicaSetInitiate(mongoClient, cmd); } mongoClient.close(); return commandResult; }
public void find() throws UnknownHostException { // Connect to Mongo DB MongoClientURI mongoURI = new MongoClientURI("mongodb://localhost:27017"); MongoClient mongo = new MongoClient(mongoURI); try { // Get "logs" collection from the "websites" DB. MongoDatabase db = mongo.getDatabase("websites"); MongoCollection<Document> coll = db.getCollection("logs"); // Find all DB object from the DB collection // Document log = null; System.out.println(coll.count()); FindIterable<Document> cursor = coll.find(); // Loop for each db object of the cursor. // while (cursor.iterator().hasNext()) { // log = cursor.iterator().next(); // System.out.println(log.toString()); // } // JAVA 8 : these codes become // cursor.forEach(new Block<Document>() { // // @Override // public void apply(Document t) { // System.out.println(t.toString()); // // } // }); cursor.forEach((Block<Document>) t -> System.out.println(t.toString())); } finally { // close mongo connection mongo.close(); } }
/** 该方法必须在写锁开启的情况下使用 */ protected void reloadProxyList() { MongoClient mongo = null; try { mongo = new MongoClient(Params.MongoDB.PROXIES.host, Params.MongoDB.PROXIES.port); } catch (UnknownHostException e) { System.err.println("mongo proxies unreachable"); } DBObject query = new BasicDBObject(); if (_tag != null) { query.put("tag", _tag); } DBObject key = new BasicDBObject().append("_id", 1); DBCursor cursor = mongo.getDB(Params.MongoDB.PROXIES.database).getCollection("proxies").find(query, key); for (DBObject o : cursor) { proxypool.add((String) o.get("_id")); } mongo.close(); if (proxypool.isEmpty()) { throw new RuntimeException("no useful proxy in mongo"); } System.out.println("load proxy list form db, count: " + proxypool.size()); }