List of usage examples for com.mongodb ServerAddress getHost
public String getHost()
From source file:dk.au.cs.karibu.backend.mongo.StandardMongoConfiguration.java
License:Apache License
/** * {@inheritDoc}/* w ww.ja v a 2 s . c om*/ */ @Override public String toString() { String adrlist = ""; for (ServerAddress adr : getServerAddressList()) { if (adrlist.length() > 0) { adrlist += ","; } adrlist += adr.getHost() + ":" + adr.getPort(); } return "StandardMongoConfiguration" + " (databaseName : " + getDatabaseName() + ", serverAddressList : [" + adrlist + "] User: " + getUsername() + ")"; }
From source file:io.debezium.connector.mongodb.MongoUtil.java
License:Apache License
protected static String toString(ServerAddress address) { String host = address.getHost(); if (host.contains(":")) { // IPv6 address, so wrap with square brackets ... return "[" + host + "]:" + address.getPort(); }//from w ww. j a va 2 s.c o m return host + ":" + address.getPort(); }
From source file:io.debezium.connector.mongodb.ReplicaSet.java
License:Apache License
protected static int compare(ServerAddress address1, ServerAddress address2) { int diff = address1.getHost().compareTo(address2.getHost()); if (diff != 0) return diff; return address1.getPort() - address2.getPort(); }
From source file:me.konglong.momei.mongodb.core.MongoClientFactory.java
License:Apache License
private ServerAddress createConfiguredOrDefaultServerAddress() throws UnknownHostException { ServerAddress defaultAddress = new ServerAddress(); return new ServerAddress(StringUtils.hasText(host) ? host : defaultAddress.getHost(), port != null ? port.intValue() : defaultAddress.getPort()); }
From source file:net.cellcloud.storage.mongodb.MongoDBStorage.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w ww . ja v a 2 s .c o m public boolean open(Properties properties) { if (properties.hasProperty(MongoDBProperties.SERVER_ADDRESS)) { List<ServerAddress> list = (List<ServerAddress>) properties .getProperty(MongoDBProperties.SERVER_ADDRESS).getValue(); for (ServerAddress addr : list) { addr.getHost(); } } return false; }
From source file:org.alfresco.mongo.MongoDBForTestsFactory.java
License:Open Source License
/** * Utility method to build a MongoDB URI that references the {@link #getObject() DB} provided by this factory * /*from w ww . j a v a 2 s . c o m*/ * @return a MongoDB URI that can be used to connect to the DB instance */ public String getMongoURI() { String dbName = db.getName(); ServerAddress mongoAddress = db.getMongo().getAddress(); String mongoUri = MONGO_PREFIX + mongoAddress.getHost() + ":" + mongoAddress.getPort() + "/" + dbName; return mongoUri; }
From source file:org.alfresco.mongo.MongoDBForTestsFactory.java
License:Open Source License
/** * Utility method to build a MongoDB URI that can be used to construct a {@link MongoClient mongo client}. * // www. j a v a 2 s .c om * @return a MongoDB URI that can be used to connect to mongo */ public String getMongoURIWithoutDB() { ServerAddress mongoAddress = db.getMongo().getAddress(); String mongoUri = MONGO_PREFIX + mongoAddress.getHost() + ":" + mongoAddress.getPort(); return mongoUri; }
From source file:org.anyframe.logmanager.bundle.core.Activator.java
License:Apache License
/** * @param context//from www. ja v a 2 s. co m * @throws Exception */ private void initMongo(BundleContext context) throws Exception { if (context == null) { throw new LogManagerBundleException("Context is null."); } String mongoSvr = context.getProperty("mongo.host"); int mongoPort = Integer.parseInt(context.getProperty("mongo.port")); logger.info("MongoDB connect to - " + mongoSvr + ":" + mongoPort); MongoOptions options = new MongoOptions(); options.connectionsPerHost = Integer.parseInt(context.getProperty("mongo.connectionsPerHost")); options.autoConnectRetry = Boolean.parseBoolean(context.getProperty("mongo.autoConnectRetry")); options.connectTimeout = Integer.parseInt(context.getProperty("mongo.connectTimeout")); options.threadsAllowedToBlockForConnectionMultiplier = Integer .parseInt(context.getProperty("mongo.threadsAllowedToBlockForConnectionMultiplier")); options.maxWaitTime = Integer.parseInt(context.getProperty("mongo.maxWaitTime")); options.connectTimeout = Integer.parseInt(context.getProperty("mongo.connectTimeout")); options.socketKeepAlive = Boolean.parseBoolean(context.getProperty("mongo.socketKeepAlive")); options.socketTimeout = Integer.parseInt(context.getProperty("mongo.socketTimeout")); ServerAddress addr = new ServerAddress(mongoSvr, mongoPort); mongo = new Mongo(addr, options); if (mongo != null) { logger.info("MongoDB connected - " + addr.getHost() + ":" + addr.getPort()); } }
From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java
License:Apache License
@SuppressWarnings({ "rawtypes" }) private void init() throws IOException { List<String> h = storagePluginConfig.getHosts(); List<ServerAddress> addresses = Lists.newArrayList(); for (String host : h) { addresses.add(new ServerAddress(host)); }// w ww .j av a 2s.com MongoClient client = storagePlugin.getClient(); chunksMapping = Maps.newHashMap(); chunksInverseMapping = Maps.newLinkedHashMap(); if (isShardedCluster(client)) { MongoDatabase db = client.getDatabase(CONFIG); MongoCollection<Document> chunksCollection = db.getCollection(CHUNKS); Document filter = new Document(); filter.put(NS, this.scanSpec.getDbName() + "." + this.scanSpec.getCollectionName()); Document projection = new Document(); projection.put(SHARD, select); projection.put(MIN, select); projection.put(MAX, select); FindIterable<Document> chunkCursor = chunksCollection.find(filter).projection(projection); MongoCursor<Document> iterator = chunkCursor.iterator(); MongoCollection<Document> shardsCollection = db.getCollection(SHARDS); projection = new Document(); projection.put(HOST, select); boolean hasChunks = false; while (iterator.hasNext()) { Document chunkObj = iterator.next(); String shardName = (String) chunkObj.get(SHARD); String chunkId = (String) chunkObj.get(ID); filter = new Document(ID, shardName); FindIterable<Document> hostCursor = shardsCollection.find(filter).projection(projection); MongoCursor<Document> hostIterator = hostCursor.iterator(); while (hostIterator.hasNext()) { Document hostObj = hostIterator.next(); String hostEntry = (String) hostObj.get(HOST); String[] tagAndHost = StringUtils.split(hostEntry, '/'); String[] hosts = tagAndHost.length > 1 ? StringUtils.split(tagAndHost[1], ',') : StringUtils.split(tagAndHost[0], ','); List<String> chunkHosts = Arrays.asList(hosts); Set<ServerAddress> addressList = getPreferredHosts(storagePlugin.getClient(addresses), chunkHosts); if (addressList == null) { addressList = Sets.newHashSet(); for (String host : chunkHosts) { addressList.add(new ServerAddress(host)); } } chunksMapping.put(chunkId, addressList); ServerAddress address = addressList.iterator().next(); List<ChunkInfo> chunkList = chunksInverseMapping.get(address.getHost()); if (chunkList == null) { chunkList = Lists.newArrayList(); chunksInverseMapping.put(address.getHost(), chunkList); } List<String> chunkHostsList = new ArrayList<String>(); for (ServerAddress serverAddr : addressList) { chunkHostsList.add(serverAddr.toString()); } ChunkInfo chunkInfo = new ChunkInfo(chunkHostsList, chunkId); Document minMap = (Document) chunkObj.get(MIN); Map<String, Object> minFilters = Maps.newHashMap(); Set keySet = minMap.keySet(); for (Object keyObj : keySet) { Object object = minMap.get(keyObj); if (!(object instanceof MinKey)) { minFilters.put(keyObj.toString(), object); } } chunkInfo.setMinFilters(minFilters); Map<String, Object> maxFilters = Maps.newHashMap(); Map maxMap = (Document) chunkObj.get(MAX); keySet = maxMap.keySet(); for (Object keyObj : keySet) { Object object = maxMap.get(keyObj); if (!(object instanceof MaxKey)) { maxFilters.put(keyObj.toString(), object); } } chunkInfo.setMaxFilters(maxFilters); chunkList.add(chunkInfo); } hasChunks = true; } // In a sharded environment, if a collection doesn't have any chunks, it is considered as an // unsharded collection and it will be stored in the primary shard of that database. if (!hasChunks) { handleUnshardedCollection(getPrimaryShardInfo(client)); } } else { handleUnshardedCollection(storagePluginConfig.getHosts()); } }
From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java
License:Apache License
private void handleUnshardedCollection(List<String> hosts) { String chunkName = Joiner.on('.').join(scanSpec.getDbName(), scanSpec.getCollectionName()); Set<ServerAddress> addressList = Sets.newHashSet(); for (String host : hosts) { addressList.add(new ServerAddress(host)); }// w ww. j a v a 2 s . c o m chunksMapping.put(chunkName, addressList); String host = hosts.get(0); ServerAddress address = new ServerAddress(host); ChunkInfo chunkInfo = new ChunkInfo(hosts, chunkName); chunkInfo.setMinFilters(Collections.<String, Object>emptyMap()); chunkInfo.setMaxFilters(Collections.<String, Object>emptyMap()); List<ChunkInfo> chunksList = Lists.newArrayList(); chunksList.add(chunkInfo); chunksInverseMapping.put(address.getHost(), chunksList); }