List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(final InetSocketAddress inetSocketAddress)
From source file:org.springframework.data.mongodb.config.ServerAddressPropertyEditor.java
License:Apache License
/** * Parses the given source into a {@link ServerAddress}. * /*from w w w .j a va2 s . co m*/ * @param source * @return the */ private ServerAddress parseServerAddress(String source) { if (!StringUtils.hasText(source)) { LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source); return null; } String[] hostAndPort = extractHostAddressAndPort(source.trim()); if (hostAndPort.length > 2) { LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source); return null; } try { InetAddress hostAddress = InetAddress.getByName(hostAndPort[0]); Integer port = hostAndPort.length == 1 ? null : Integer.parseInt(hostAndPort[1]); return port == null ? new ServerAddress(hostAddress) : new ServerAddress(hostAddress, port); } catch (UnknownHostException e) { LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "host", hostAndPort[0]); } catch (NumberFormatException e) { LOG.warn(COULD_NOT_PARSE_ADDRESS_MESSAGE, "port", hostAndPort[1]); } return null; }
From source file:org.wikidata.couchbase.MongoPersistHandler.java
License:Open Source License
private void createMongoClient() throws UnknownHostException { // init CouchbaseClient List<ServerAddress> adressList = new LinkedList<ServerAddress>(); for (String url : conf.getDbUrls()) { adressList.add(new ServerAddress(url)); }/*from w ww . j a va 2s . co m*/ mongoClient = new MongoClient(adressList); }
From source file:org.wisdom.mongodb.MongoDBClient.java
License:Apache License
/** * Per the documentation of Mongo API the UnknownHost Exception is deprecated and will be removed in the next driver. * we currently use driver 2.13.0//w ww.java 2 s .c o m * * @return the server address */ private ServerAddress createAddress() { try { if (mongoDbPort == 0) { return new ServerAddress(mongoDbHost); } return new ServerAddress(mongoDbHost, mongoDbPort); } catch (UnknownHostException e) { throw new IllegalArgumentException("Cannot connect to MongoDB server", e); } }
From source file:parlare.application.server.model.Database.java
private void doConnectionMongo() { try {/*from w ww . j av a 2 s . co m*/ System.out.println("MONGO server: " + server); System.out.println("MONGO user: " + user); System.out.println("MONGO source: " + source); System.out.println(); MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(MongoCredential.createMongoCRCredential(user, source, password.toCharArray())), new MongoClientOptions.Builder().build()); DB testDB = mongoClient.getDB("html5apps"); System.out.println("Count: " + testDB.getCollection("html5apps").count()); System.out.println("Insert result: " + testDB.getCollection("html5apps").insert(new BasicDBObject())); } catch (UnknownHostException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:parlare.application.server.model.Database.java
private String doClientMongo() { String print = ""; System.out.println("User:" + user + " Source:" + source + " Password:" + password); try {/* w ww . j a va2 s. c om*/ // 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. ja v a 2 s . c o m * * @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:santandersensors_server.CollectThread.java
License:Open Source License
public CollectThread(JsonFile conf) { try {// w w w . j a va 2s . co m this.cred = MongoCredential.createMongoCRCredential(conf.getJson().get("username").toString(), conf.getJson().get("userdatabase").toString(), conf.getJson().get("password").toString().toCharArray()); this.address = new ServerAddress(conf.getJson().get("IP").toString()); this.db = conf.getJson().get("maindatabase").toString(); this.collection = conf.getJson().get("maincollection").toString(); this.discardColl = conf.getJson().get("discardcollection").toString(); this.typeColl = conf.getJson().get("typecollection").toString(); this.lastdataColl = conf.getJson().get("lastdatacollection").toString(); this.tagsColl = conf.getJson().get("tagscollection").toString(); this.URL = conf.getJson().get("URL").toString(); this.lapse = Integer.parseInt(conf.getJson().get("lapse").toString()); //Creo la connessione a MongoDB this.client = new MongoTools(this.address, Arrays.asList(this.cred)); this.client.setDatabase(this.db); this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); this.cal = new GregorianCalendar(); } catch (Exception e) { System.out.print("(" + new GregorianCalendar().getTime() + ") -> "); System.out.print("error: " + e + "\n"); } }
From source file:santandersensors_server.ConsoleThread.java
License:Open Source License
public ConsoleThread(JsonFile conf, Socket s) { try {/*from w w w . j a v a2 s . c o m*/ this.cred = MongoCredential.createMongoCRCredential(conf.getJson().get("username").toString(), conf.getJson().get("userdatabase").toString(), conf.getJson().get("password").toString().toCharArray()); this.address = new ServerAddress(conf.getJson().get("IP").toString()); this.db = conf.getJson().get("maindatabase").toString(); ; this.collection = conf.getJson().get("maincollection").toString(); this.discardColl = conf.getJson().get("discardcollection").toString(); this.typeColl = conf.getJson().get("typecollection").toString(); this.tagsColl = conf.getJson().get("tagscollection").toString(); //Creo la connessione a MongoDB this.client = new MongoTools(this.address, Arrays.asList(this.cred)); this.client.setDatabase(this.db); this.mapReduceOBJ = new MapReduce(this.client, this.collection); this.S = s; } catch (Exception e) { this.interrupt(); } }
From source file:simple.crawler.impl.PrototypeScheduler.java
License:Open Source License
public static void main(String[] args) throws Exception { CrawlerConfiguration config = new CrawlerConfiguration("http://congdongjava.com/forum/", "HTML/BODY[1]/DIV[@id='headerMover']/DIV[@id='content']/DIV[1]/DIV[1]/DIV[1]/DIV[1]/DIV[3]/H1[1]", "HTML/BODY[1]/DIV[@id='headerMover']/DIV[@id='content']/DIV[1]/DIV[1]/DIV[1]/DIV[1]/DIV[3]/H1[1]", "^forums/[a-zA-Z0-9-%]+\\.[0-9]+/(page-[0-9]+)?", "^threads/[a-zA-Z0-9-%]+\\.[0-9]+/(page-[0-9]+)?", true);/* ww w. jav a2 s.c o m*/ CrawlerImpl crawler = new CrawlerImpl(config, 10, new ServerAddress("localhost"), new MongoClientOptions.Builder().build()); crawler.schedule(1, 60 * 60, TimeUnit.SECONDS); crawler.start(); }
From source file:simple.crawler.mongo.CrawlingDB.java
License:Open Source License
public CrawlingDB(String dbName) throws UnknownHostException { this.dbName = dbName; ServerAddress address = new ServerAddress("localhost"); MongoClientOptions options = new MongoClientOptions.Builder().build(); this.client = new MongoClient(address, options); }