List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(@Nullable final String host, final int port)
From source file:com.torodb.testing.mongodb.docker.ServerAddressConverter.java
License:Apache License
public static ServerAddress convert(HostAndPort hostAndPort) { return new ServerAddress(hostAndPort.getHost(), hostAndPort.getPort()); }
From source file:com.ttpod.rest.web.spring.MongoFactoryBean.java
License:Apache License
@Override public void afterPropertiesSet() throws Exception { String[] host_ports = urls.split(","); List<ServerAddress> addList = new ArrayList<ServerAddress>(host_ports.length); for (String host_port : host_ports) { String[] kv = host_port.split(":"); addList.add(new ServerAddress(kv[0], Integer.parseInt(kv[1]))); }// www. j ava 2 s . c o m if (autoSlaveOk && addList.size() > 1) { options.readPreference = ReadPreference.secondaryPreferred(); } this.mongo = new Mongo(addList, options); }
From source file:com.vcredit.lrh.db.mongodb.LRHMongoConfiguration.java
@Override public Mongo mongo() throws Exception { if (mongoProperties.getUsername() != null && mongoProperties.getPassword() != null) { return new MongoClient( singletonList(new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort())), singletonList(MongoCredential.createCredential(mongoProperties.getUsername(), mongoProperties.getDatabase(), mongoProperties.getPassword().toCharArray()))); } else {//w ww . j ava2 s .com return new MongoClient( singletonList(new ServerAddress(mongoProperties.getHost(), mongoProperties.getPort()))); } }
From source file:com.wincere.lamda.storm.bolt.CreateTable.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 av a 2 s. c o m*/ * @throws UnknownHostException if it cannot connect to a MongoDB instance at localhost:27017 */ public void update(BasicDBObject doc, OutputCollector collector, Tuple input) throws UnknownHostException { // connect to the local database server MongoCredential credential = MongoCredential.createMongoCRCredential("superuser", "admin", "12345678".toCharArray()); try { MongoClient mongoClient = new MongoClient(new ServerAddress("172.16.1.171", 27017), Arrays.asList(credential)); // MongoClient mongoClient = new MongoClient("172.16.1.171",27017); /* // 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("UCAPBatchTest"); // get a collection object to work with DBCollection coll = db.getCollection("Queries1"); // DBCollection status = db.getCollection("statustest1"); //DBCollection coll1 = db.getCollection("queryaudittest1"); // drop all the data in it //coll.drop(); //status.drop(); //coll1.drop(); /* status.insert(new BasicDBObject().append("queryStatus", "Open").append("QueryStatusID","1")); status.insert(new BasicDBObject().append("queryStatus", "Answered").append("QueryStatusID","2")); status.insert(new BasicDBObject().append("queryStatus", "Closed").append("QueryStatusID","3")); status.insert(new BasicDBObject().append("queryStatus", "Cancelled").append("QueryStatusID","4")); */ // make a document and insert it int count = 0; DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"); try { //.equals("Open")?"1":(splitValue[5].equals("Answered")?"2":"3") BasicDBObject searchQuery = new BasicDBObject().append("queryRepeatKey", (String) doc.get("queryRepeatKey")); BasicDBObject newDocument = new BasicDBObject(); DBCursor cursor = coll.find(searchQuery); //DBObject result = cursor.next(); if (cursor.hasNext()) { DBObject result = cursor.next(); String queryValue = (String) result.get("queryValue"); String queryStatusID = (String) result.get("queryStatusID"); String queryResponse = (String) result.get("queryResponse"); String queryResolvedTimeStamp = (String) result.get("queryResolvedTimeStamp"); String queryAnsweredTimeStamp = (String) result.get("queryAnsweredTimeStamp"); String queryCreatedTimeStamp = (String) result.get("queryCreatedTimeStamp"); if (doc.get("queryValue").equals("\\N")) { doc.append("queryValue", queryValue); } if (doc.get("queryStatusID").equals("\\N")) { doc.append("queryStatusID", queryStatusID); } if (doc.get("queryResponse").equals("\\N")) { doc.append("queryResponse", queryResponse); } if (doc.get("queryResolvedTimeStamp").equals("\\N")) { doc.append("queryResolvedTimeStamp", queryResolvedTimeStamp); } if (doc.get("queryAnsweredTimeStamp").equals("\\N")) { doc.append("queryAnsweredTimeStamp", queryAnsweredTimeStamp); } doc.append("queryCreatedTimeStamp", queryCreatedTimeStamp); } if (doc.get("queryStatusID").equals("Open")) doc.append("queryCreatedTimeStamp", doc.get("queryCreatedTimeStamp")); //System.out.println(count); newDocument.append("$set", doc); try { coll.update(searchQuery, newDocument, true, true); } catch (MongoException me) { collector.fail(input); } // collector.ack(input); //coll.insert(doc); } catch (Exception e) { System.err.println("CSV file cannot be read : " + e); } //System.out.println(count); // lets get all the documents in the collection and print them out /*DBCursor cursor = coll1.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(); }*/ // release resources //db.dropDatabase(); mongoClient.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.wlobs.wilqor.server.config.MongoConfig.java
License:Apache License
@Override public Mongo mongo() throws Exception { return new MongoClient(Collections.singletonList(new ServerAddress(host, port)), Collections .singletonList(MongoCredential.createCredential(userName, database, password.toCharArray()))); }
From source file:com.wordpress.kuylim.configuration.MongoConfig.java
@Override @Bean// w w w. ja va2 s . co m public Mongo mongo() throws Exception { return new MongoClient(singletonList(new ServerAddress(host, port)), singletonList(MongoCredential.createCredential(username, database, password.toCharArray()))); }
From source file:controlador.PaginaControlador.java
public PaginaControlador() { try {/* ww w . j a v a2 s . c om*/ // Creo una instancia de cliente de MongoDB cliente = new MongoClient(new ServerAddress("localhost", 27017)); } catch (UnknownHostException ex) { Logger.getLogger(ex.toString()); } //Creo conexion con base de datos database = cliente.getDB("DBIndex"); paginaCollection = database.getCollection("paginas"); }
From source file:controlador.PalabraControlador.java
public PalabraControlador() { try {// w w w . ja v a 2s . com // Creo una instancia de cliente de MongoDB cliente = new MongoClient(new ServerAddress("localhost", 27017)); } catch (UnknownHostException ex) { Logger.getLogger(ex.toString()); } //Creo conexion con base de datos database = cliente.getDB("DBIndex"); palabraCollection = database.getCollection("palabras"); }
From source file:dataBase.Connect.java
public Connect() { cliente = new MongoClient(new ServerAddress("localhost", 27017)); System.out.println("Conexion establecida"); db = cliente.getDB(dbName);/*from w ww . jav a 2 s . c o m*/ }
From source file:DBLayer.DBHandle.java
private DBHandle() { try {/*from ww w.ja va2 s. c o m*/ // To connect to mongodb server // Now connect to your databases // db = mongoClient.getDB("test"); MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017)); db = mongoClient.getDatabase("schedule"); job = db.getCollection(JOB); inventory = db.getCollection(INVENTORY); inventoryTransaction = db.getCollection(INVENTORYSTORE); System.out.println("Connect to database successfully"); // boolean auth = db.authenticate(myUserName, myPassword); // System.out.println("Authentication: "+auth); // DBCollection coll = db.getCollection("mycol"); // System.out.println("Collection mycol selected successfully"); // // BasicDBObject doc = new BasicDBObject("title", "MongoDB"). // append("description", "database"). // append("likes", 100). // append("url", "http://www.tutorialspoint.com/mongodb/"). // append("by", "tutorials point"); // // coll.insert(doc); // System.out.println("Document inserted successfully"); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }