List of usage examples for com.mongodb MongoClient MongoClient
public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation)
From source file:com.intuit.utils.PopulateTweets.java
public static void main(String[] args) { Date now = new Date(); System.out.println("Current date is: " + now.toString()); MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("tweetsdb"); DBCollection collection = db.getCollection("tweetscollection"); WriteResult result = collection.remove(new BasicDBObject()); String[] users = { "user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8", "user9", "user10" }; // I am not introducing enough randomness in terms of the insertion of // tweets for users at a random time orders, due to lack of time. for (String user : users) { int tweetIndex = 0; for (int i = 1; i <= 10; i++) { BasicDBObject document = new BasicDBObject(); // This is a way to maintain uniqueness of the tweetid value across the system // Ideally, this should be the "_id" value, but due to lack of time, I am skipping // that part. That would help to partition the tweets across multiple shards in a // large scale system. String tweetId = user + "|tweet" + tweetIndex; document.put("tweetId", tweetId); document.put("user", user); document.put("text", "tweet number" + tweetIndex); document.put("tweetedOn", new Date().toString()); System.out.println("tweet number: " + tweetIndex + " " + document.toString()); collection.insert(document); tweetIndex++;// www . j ava2 s . com try { // Just introducing some delay between tweets to make the testing a bit easy Thread.sleep(3000); } catch (InterruptedException ex) { Logger.getLogger(PopulateTweets.class.getName()).log(Level.SEVERE, null, ex); } } } BasicDBObject indexObj = new BasicDBObject(); indexObj.put("user", 1); indexObj.put("tweetedOn", -1); collection.createIndex(indexObj); BasicDBObject tweetIdObj = new BasicDBObject(); tweetIdObj.put("tweetId", 1); collection.createIndex(tweetIdObj); }
From source file:com.intuit.utils.PopulateUsers.java
public static void main(String[] args) { Date now = new Date(); System.out.println("Current date is: " + now.toString()); MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("tweetsdb"); DBCollection collection = db.getCollection("userscollection"); WriteResult result = collection.remove(new BasicDBObject()); int userIndex = 1; for (int i = 1; i <= 10; i++) { JSONObject userDocument = new JSONObject(); String user = "user" + userIndex; userDocument.put("user", user); JSONArray followerList = new JSONArray(); Random randomGenerator = new Random(); for (int j = 0; j < 3; j++) { int followerId = randomGenerator.nextInt(10) + 1; // Assumption here is, a user will not be a follower on himself while (followerId == userIndex) { followerId = randomGenerator.nextInt(10) + 1; }//from w w w. j a v a2 s . co m String follower = "user" + followerId; if (!followerList.contains(follower)) { followerList.add(follower); } } userDocument.put("followers", followerList); JSONArray followingList = new JSONArray(); for (int k = 0; k < 3; k++) { int followingId = randomGenerator.nextInt(10) + 1; // Assumption here is, a user will not be following his own tweets while (followingId == userIndex) { followingId = randomGenerator.nextInt(10) + 1; } String followingUser = "user" + followingId; if (!followingList.contains(followingUser)) { followingList.add(followingUser); } } userDocument.put("following", followingList); System.out.println("Json string is: " + userDocument.toString()); DBObject userDBObject = (DBObject) JSON.parse(userDocument.toString()); collection.insert(userDBObject); userIndex++; } // try { // FileWriter file = new FileWriter("/Users/dmurty/Documents/MongoData/usersCollection.js"); // file.write(usersArray.toJSONString()); // file.flush(); // file.close(); // } catch (IOException ex) { // Logger.getLogger(PopulateUsers.class.getName()).log(Level.SEVERE, null, ex); // } }
From source file:com.jaeksoft.searchlib.crawler.database.DatabaseCrawlMongoDb.java
License:Open Source License
MongoClient getMongoClient() throws URISyntaxException, UnknownHostException { String user = getUser();/*w w w.j a v a 2 s.c o m*/ String password = getPassword(); URI uri = new URI(getUrl()); MongoCredential credential = null; if (!StringUtils.isEmpty(user) && !StringUtils.isEmpty(password)) { credential = MongoCredential.createMongoCRCredential(user, databaseName, password.toCharArray()); return new MongoClient(new ServerAddress(uri.getHost(), uri.getPort()), Arrays.asList(credential)); } return new MongoClient(new ServerAddress(uri.getHost(), uri.getPort())); }
From source file:com.janeluo.jfinalplus.plugin.monogodb.MongodbPlugin.java
License:Apache License
@Override public boolean start() { try {//from www.j a v a 2s. c o m client = new MongoClient(host, port); } catch (UnknownHostException e) { throw new RuntimeException("can't connect mongodb, please check the host and port:" + host + "," + port, e); } MongoKit.init(client, database); return true; }
From source file:com.jarova.mqtt.MongoDB.java
public static void init() throws UnknownHostException { System.out.println("Init connection with MongoDB"); mongoClient = new MongoClient(DB_HOST, DB_PORT); db = mongoClient.getDB(DB_NAME);/*from w ww . jav a2 s .c om*/ initialized = true; System.out.println("MongoDB connection initialized"); }
From source file:com.javamongodb.utils.DatabaseUtils.java
public static DBCollection openDBConnection() { try {//w ww . j a v a2s.c o m MongoClient mongoClient = new MongoClient(HOST_NAME, PORT_NUMBER); //connect to the mongodb server logger.debug("MongoClient object created"); DB database = mongoClient.getDB(DATABASE_NAME); //connect to the database logger.debug("Database achieved"); //boolean auth = db.authenticate(myUserName, myPassword); DBCollection collection = database.getCollection(COLLECTION_NAME); if (collection == null) { collection = database.createCollection(COLLECTION_NAME, null); } logger.debug("Database collection achieved"); return collection; } catch (UnknownHostException | HeadlessException exception) { logger.error("Error opening database connection:\n" + exception.getMessage()); JOptionPane.showMessageDialog(new JFrame(), MessageUtils.MESSAGES.getString("error.while.saving.data"), MessageUtils.MESSAGES.getString("error.message"), JOptionPane.ERROR_MESSAGE); } return null; }
From source file:com.jfinal.ext.zyq.plugin.MongodbPlugin.java
License:Apache License
@Override public boolean start() { client = new MongoClient(host, port); MongoKit.init(client, database); return true; }
From source file:com.jim.im.offline.config.OfflineMongoConfig.java
License:Open Source License
@Bean public MongoClient mongoClient() { ServerAddress serverAddress = null;/* w ww. j a v a 2 s . c o m*/ try { serverAddress = new ServerAddress(properties.getHost(), properties.getPort()); } catch (UnknownHostException e) { LOGGER.error("Init mongo client failure, cause in UnknownHostException!", e); } MongoCredential credential = MongoCredential.createScramSha1Credential(properties.getUsername(), properties.getAuthenticationDatabase(), properties.getPassword()); return new MongoClient(serverAddress, Arrays.asList(credential)); }
From source file:com.kurniakue.data.KurniaKueDb.java
public void init() { try {// ww w.ja va 2 s .co m mongoClient = new MongoClient(config.getProperty("DBIP"), config.getInt("DBPort")); if (mongoClient == null) { return; } db = mongoClient.getDatabase(config.getProperty("DBName")); allClients.add(mongoClient); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.left8.evs.utilities.dsretriever.MongoHandler.java
License:Open Source License
/** * Constructor, creates a connection with the MongoDB instance. * @param config A configuration object/*from w w w . j a v a 2 s . c o m*/ */ public MongoHandler(Config config) { try { this.config = config; client = new MongoClient(this.config.getServerName(), this.config.getServerPort()); } catch (MongoClientException e) { Utilities.printMessageln("Error connecting to client"); client = null; Logger.getLogger(MongoHandler.class.getName()).log(Level.SEVERE, null, e); } }