Search in sources :

Example 1 with MongoClient

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());
    }
}
Also used : Document(org.bson.Document) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) DrillbitEndpoint(org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint) PriorityQueue(java.util.PriorityQueue) LoggerFactory(org.slf4j.LoggerFactory) Sets(org.apache.drill.shaded.guava.com.google.common.collect.Sets) MongoDatabase(com.mongodb.client.MongoDatabase) VisibleForTesting(org.apache.drill.shaded.guava.com.google.common.annotations.VisibleForTesting) StringUtils(org.apache.commons.lang3.StringUtils) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) Map(java.util.Map) Joiner(org.apache.drill.shaded.guava.com.google.common.base.Joiner) JacksonInject(com.fasterxml.jackson.annotation.JacksonInject) MongoSubScanSpec(org.apache.drill.exec.store.mongo.MongoSubScan.MongoSubScanSpec) MaxKey(org.bson.types.MaxKey) SchemaPath(org.apache.drill.common.expression.SchemaPath) Set(java.util.Set) Objects(java.util.Objects) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Entry(java.util.Map.Entry) Stopwatch(org.apache.drill.shaded.guava.com.google.common.base.Stopwatch) Preconditions(org.apache.drill.shaded.guava.com.google.common.base.Preconditions) Queue(java.util.Queue) ReadPreference(com.mongodb.ReadPreference) StoragePluginRegistry(org.apache.drill.exec.store.StoragePluginRegistry) MongoClient(com.mongodb.client.MongoClient) PlanStringBuilder(org.apache.drill.common.PlanStringBuilder) MongoCollection(com.mongodb.client.MongoCollection) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) AbstractGroupScan(org.apache.drill.exec.physical.base.AbstractGroupScan) ShardedMongoSubScanSpec(org.apache.drill.exec.store.mongo.MongoSubScan.ShardedMongoSubScanSpec) ArrayList(java.util.ArrayList) GroupScanProperty(org.apache.drill.exec.physical.base.ScanStats.GroupScanProperty) Bson(org.bson.conversions.Bson) Maps(org.apache.drill.shaded.guava.com.google.common.collect.Maps) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) MongoCursor(com.mongodb.client.MongoCursor) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) MinKey(org.bson.types.MinKey) DocumentCodec(org.bson.codecs.DocumentCodec) LinkedList(java.util.LinkedList) ServerAddress(com.mongodb.ServerAddress) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ScanStats(org.apache.drill.exec.physical.base.ScanStats) TimeUnit(java.util.concurrent.TimeUnit) Lists(org.apache.drill.shaded.guava.com.google.common.collect.Lists) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) EndpointAffinity(org.apache.drill.exec.physical.EndpointAffinity) GroupScan(org.apache.drill.exec.physical.base.GroupScan) ChunkInfo(org.apache.drill.exec.store.mongo.common.ChunkInfo) Comparator(java.util.Comparator) Collections(java.util.Collections) Set(java.util.Set) ChunkInfo(org.apache.drill.exec.store.mongo.common.ChunkInfo) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) MaxKey(org.bson.types.MaxKey) FindIterable(com.mongodb.client.FindIterable) Document(org.bson.Document) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) MinKey(org.bson.types.MinKey) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) MongoCursor(com.mongodb.client.MongoCursor) Map(java.util.Map) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 2 with MongoClient

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"));
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) Logger(org.apache.logging.log4j.Logger) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 3 with MongoClient

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<>())));
}
Also used : MongoClient(com.mongodb.client.MongoClient) BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 4 with MongoClient

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);
}
Also used : MongoClient(com.mongodb.client.MongoClient) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) EventMatcher.getReasonString(com.mongodb.client.unified.EventMatcher.getReasonString) BsonString(org.bson.BsonString) Map(java.util.Map) HashMap(java.util.HashMap) MongoDatabase(com.mongodb.client.MongoDatabase) BsonValue(org.bson.BsonValue)

Example 5 with MongoClient

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);
}
Also used : MongoClient(com.mongodb.client.MongoClient) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace)

Aggregations

MongoClient (com.mongodb.client.MongoClient)45 Document (org.bson.Document)22 MongoDatabase (com.mongodb.client.MongoDatabase)19 Test (org.junit.Test)10 Map (java.util.Map)9 MongoClientSettings (com.mongodb.MongoClientSettings)8 ServerAddress (com.mongodb.ServerAddress)8 HashMap (java.util.HashMap)7 ConnectionString (com.mongodb.ConnectionString)6 BsonDocument (org.bson.BsonDocument)6 BsonString (org.bson.BsonString)6 Test (org.junit.jupiter.api.Test)6 List (java.util.List)5 SecureRandom (java.security.SecureRandom)4 Set (java.util.Set)4 TimeUnit (java.util.concurrent.TimeUnit)4 BsonValue (org.bson.BsonValue)4 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)3 ClientSessionOptions (com.mongodb.ClientSessionOptions)3 ClusterFixture.getMultiMongosConnectionString (com.mongodb.ClusterFixture.getMultiMongosConnectionString)3