use of com.mongodb.client.MongoClient in project drill by apache.
the class MongoGroupScan method init.
@SuppressWarnings({ "rawtypes" })
private void init() {
List<String> h = storagePluginConfig.getHosts();
List<ServerAddress> addresses = Lists.newArrayList();
for (String host : h) {
addresses.add(new ServerAddress(host));
}
MongoClient client = storagePlugin.getClient();
chunksMapping = Maps.newHashMap();
chunksInverseMapping = Maps.newLinkedHashMap();
if (useAggregate && isShardedCluster(client)) {
handleUnshardedCollection(getPrimaryShardInfo());
} else 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);
// creates hexadecimal string representation of ObjectId
String chunkId = chunkObj.get(ID).toString();
filter = new Document(ID, shardName);
FindIterable<Document> hostCursor = shardsCollection.find(filter).projection(projection);
for (Document hostObj : hostCursor) {
String hostEntry = (String) hostObj.get(HOST);
String[] tagAndHost = StringUtils.split(hostEntry, '/');
String[] hosts = tagAndHost.length > 1 ? StringUtils.split(tagAndHost[1], ',') : StringUtils.split(tagAndHost[0], ',');
Set<ServerAddress> addressList = getPreferredHosts(storagePlugin.getClient(addresses));
if (addressList == null) {
addressList = Sets.newHashSet();
for (String host : hosts) {
addressList.add(new ServerAddress(host));
}
}
chunksMapping.put(chunkId, addressList);
ServerAddress address = addressList.iterator().next();
List<ChunkInfo> chunkList = chunksInverseMapping.computeIfAbsent(address.getHost(), k -> new ArrayList<>());
List<String> chunkHostsList = new ArrayList<>();
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;
}
// unsharded collection and it will be stored in the primary shard of that database.
if (!hasChunks) {
handleUnshardedCollection(getPrimaryShardInfo());
}
} else {
handleUnshardedCollection(storagePluginConfig.getHosts());
}
}
use of com.mongodb.client.MongoClient in project logging-log4j2 by apache.
the class MongoDb4CappedTest method test.
@Test
public void test() {
final Logger logger = LogManager.getLogger();
logger.info("Hello log");
try (final MongoClient mongoClient = mongoDbTestRule.getMongoClient()) {
final MongoDatabase database = mongoClient.getDatabase("testDb");
Assert.assertNotNull(database);
final MongoCollection<Document> collection = database.getCollection("testCollection");
Assert.assertNotNull(collection);
final Document first = collection.find().first();
Assert.assertNotNull(first);
Assert.assertEquals(first.toJson(), "Hello log", first.getString("message"));
}
}
use of com.mongodb.client.MongoClient in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeListDatabases.
OperationResult executeListDatabases(final BsonDocument operation) {
MongoClient client = entities.getClient(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
ClientSession session = getSession(arguments);
ListDatabasesIterable<BsonDocument> iterable = session == null ? client.listDatabases(BsonDocument.class) : client.listDatabases(session, BsonDocument.class);
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
// noinspection SwitchStatementWithTooFewBranches
switch(cur.getKey()) {
case "session":
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return resultOf(() -> new BsonArray(iterable.into(new ArrayList<>())));
}
use of com.mongodb.client.MongoClient in project mongo-java-driver by mongodb.
the class Entities method initDatabase.
private void initDatabase(final BsonDocument entity, final String id) {
MongoClient client = clients.get(entity.getString("client").getValue());
MongoDatabase database = client.getDatabase(entity.getString("databaseName").getValue());
if (entity.containsKey("databaseOptions")) {
for (Map.Entry<String, BsonValue> entry : entity.getDocument("databaseOptions").entrySet()) {
switch(entry.getKey()) {
case "readConcern":
database = database.withReadConcern(asReadConcern(entry.getValue().asDocument()));
break;
case "readPreference":
database = database.withReadPreference(asReadPreference(entry.getValue().asDocument()));
break;
case "writeConcern":
database = database.withWriteConcern(asWriteConcern(entry.getValue().asDocument()));
break;
default:
throw new UnsupportedOperationException("Unsupported database option: " + entry.getKey());
}
}
}
putEntity(id, database, databases);
}
use of com.mongodb.client.MongoClient in project mongo-java-driver by mongodb.
the class Crypts method createCrypt.
public static Crypt createCrypt(final MongoClientImpl client, final AutoEncryptionSettings options) {
MongoClient internalClient = null;
MongoClientSettings keyVaultMongoClientSettings = options.getKeyVaultMongoClientSettings();
if (keyVaultMongoClientSettings == null || !options.isBypassAutoEncryption()) {
MongoClientSettings settings = MongoClientSettings.builder(client.getSettings()).applyToConnectionPoolSettings(builder -> builder.minSize(0)).autoEncryptionSettings(null).build();
internalClient = MongoClients.create(settings);
}
MongoClient collectionInfoRetrieverClient = internalClient;
MongoClient keyVaultClient = keyVaultMongoClientSettings == null ? internalClient : MongoClients.create(keyVaultMongoClientSettings);
return new Crypt(MongoCrypts.create(createMongoCryptOptions(options.getKmsProviders(), options.getSchemaMap())), options.isBypassAutoEncryption() ? null : new CollectionInfoRetriever(collectionInfoRetrieverClient), new CommandMarker(options.isBypassAutoEncryption(), options.getExtraOptions()), new KeyRetriever(keyVaultClient, new MongoNamespace(options.getKeyVaultNamespace())), createKeyManagementService(options.getKmsProviderSslContextMap()), options.isBypassAutoEncryption(), internalClient);
}
Aggregations