List of usage examples for com.mongodb Mongo getServerAddressList
@Deprecated
public List<ServerAddress> getServerAddressList()
From source file:com.tomtom.speedtools.tracer.mongo.MongoDBTraceHandler.java
License:Apache License
@Nonnull static DBCollection getDBCollection(@Nonnull final String servers, @Nonnull final String database, @Nonnull final String userName, @Nonnull final String password, final int sizeMB, final int connectTimeoutMsecs) throws UnknownHostException { assert servers != null; assert database != null; assert userName != null; assert password != null; assert connectTimeoutMsecs >= 0; LOG.info("getDBCollection: Creating MongoDB connection for traces: {}", servers); final Mongo mongo = MongoConnectionCache.getMongoDB(servers, connectTimeoutMsecs, userName, database, password);/*from w w w . j a va 2 s.co m*/ // If this is a replica set, set read preference to secondary for traces. final List<ServerAddress> serverAddressList = mongo.getServerAddressList(); if (serverAddressList.size() > 1) { mongo.setReadPreference(ReadPreference.secondary()); } // Should writes fail, then don't throw exceptions, just ignore. // We care more about not disturbing the primary system then our own (traces) integrity. mongo.setWriteConcern(WriteConcern.UNACKNOWLEDGED); // The connection point may actually be null... Not an error. final String connectPoint = mongo.getConnectPoint(); LOG.info("getDBCollection: MongoDB connection for traces established: '{}' at {}", database, connectPoint); final DB db = mongo.getDB(database); final DBObject options = new BasicDBObject(); options.put("capped", true); options.put("size", sizeMB * MEGABYTE); final DBCollection collection; if (!db.getCollectionNames().contains(Constants.COLLECTION_NAME)) { collection = db.createCollection(Constants.COLLECTION_NAME, options); } else { collection = db.getCollection(Constants.COLLECTION_NAME); } return collection; }
From source file:org.apache.hadoop.contrib.mongoreduce.MongoInputFormat.java
License:Apache License
public static String[] hostsForShard(String shardName, boolean primaryOk) throws UnknownHostException, MongoException { ArrayList<String> hosts = new ArrayList<String>(); String[] parts = shardName.split("/"); if (parts.length == 1) { // no replicas hosts.add(shardName);/*from w w w . j ava 2s . c o m*/ } else { // replicas // get first or only host listed String host = parts[1].split(",")[0]; Mongo h = new Mongo(host); List<ServerAddress> addresses = h.getServerAddressList(); h.close(); h = null; // only one node in replica set ... - use it if (addresses.size() == 1) { ServerAddress addr = addresses.get(0); hosts.add(addr.getHost() + ":" + Integer.toString(addr.getPort())); } else { for (ServerAddress addr : addresses) { // use secondaries and primaries if (primaryOk) { hosts.add(addr.getHost() + ":" + Integer.toString(addr.getPort())); } // only use secondaries else { String haddr = addr.getHost() + ":" + Integer.toString(addr.getPort()); h = new Mongo(haddr); if (!(Boolean) h.getDB(h.getDatabaseNames().get(0)).command(cmd).get("ismaster")) { hosts.add(haddr); } } } } } return hosts.toArray(new String[0]); }
From source file:org.mongeez.dao.MongeezDao.java
License:Apache License
public MongeezDao(Mongo mongo, String databaseName, MongoAuth auth) { final List<MongoCredential> credentials = new LinkedList<MongoCredential>(); if (auth != null) { if (auth.getAuthDb() == null || auth.getAuthDb().equals(databaseName)) { credentials.add(MongoCredential.createCredential(auth.getUsername(), databaseName, auth.getPassword().toCharArray())); } else {/*from www . j a v a 2 s .com*/ credentials.add(MongoCredential.createCredential(auth.getUsername(), auth.getAuthDb(), auth.getPassword().toCharArray())); } } final MongoClient client = new MongoClient(mongo.getServerAddressList(), credentials); db = client.getDB(databaseName); configure(); }