Search in sources :

Example 21 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project drill by apache.

the class MongoRecordReader method init.

private void init(MongoSubScan.MongoSubScanSpec subScanSpec) {
    List<String> hosts = subScanSpec.getHosts();
    List<ServerAddress> addresses = Lists.newArrayList();
    for (String host : hosts) {
        addresses.add(new ServerAddress(host));
    }
    MongoClient client = plugin.getClient(addresses);
    MongoDatabase db = client.getDatabase(subScanSpec.getDbName());
    this.unionEnabled = fragmentContext.getOptions().getOption(ExecConstants.ENABLE_UNION_TYPE);
    collection = db.getCollection(subScanSpec.getCollectionName(), BsonDocument.class);
}
Also used : MongoClient(com.mongodb.MongoClient) BsonDocument(org.bson.BsonDocument) ServerAddress(com.mongodb.ServerAddress) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 22 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project camel by apache.

the class MongoDbProducer method calculateDb.

// --------- Convenience methods -----------------------
private MongoDatabase calculateDb(Exchange exchange) {
    // functionality at all
    if (!endpoint.isDynamicity()) {
        return endpoint.getMongoDatabase();
    }
    String dynamicDB = exchange.getIn().getHeader(DATABASE, String.class);
    MongoDatabase db;
    if (dynamicDB == null) {
        db = endpoint.getMongoDatabase();
    } else {
        db = endpoint.getMongoConnection().getDatabase(dynamicDB);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Dynamic database selected: {}", db.getName());
    }
    return db;
}
Also used : MongoDatabase(com.mongodb.client.MongoDatabase)

Example 23 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project camel by apache.

the class MongoDbProducer method calculateCollection.

private MongoCollection<BasicDBObject> calculateCollection(Exchange exchange) {
    // resolution logic on every Exchange if they won't be using this functionality at all
    if (!endpoint.isDynamicity()) {
        return endpoint.getMongoCollection().withWriteConcern(endpoint.getWriteConcern());
    }
    String dynamicDB = exchange.getIn().getHeader(MongoDbConstants.DATABASE, String.class);
    String dynamicCollection = exchange.getIn().getHeader(MongoDbConstants.COLLECTION, String.class);
    @SuppressWarnings("unchecked") List<BasicDBObject> dynamicIndex = exchange.getIn().getHeader(MongoDbConstants.COLLECTION_INDEX, List.class);
    MongoCollection<BasicDBObject> dbCol;
    if (dynamicDB == null && dynamicCollection == null) {
        dbCol = endpoint.getMongoCollection().withWriteConcern(endpoint.getWriteConcern());
    } else {
        MongoDatabase db = calculateDb(exchange);
        if (dynamicCollection == null) {
            dbCol = db.getCollection(endpoint.getCollection(), BasicDBObject.class);
        } else {
            dbCol = db.getCollection(dynamicCollection, BasicDBObject.class);
            // on the fly add index
            if (dynamicIndex == null) {
                endpoint.ensureIndex(dbCol, endpoint.createIndex());
            } else {
                endpoint.ensureIndex(dbCol, dynamicIndex);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Dynamic database and/or collection selected: {}->{}", endpoint.getDatabase(), endpoint.getCollection());
    }
    return dbCol;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 24 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project presto by prestodb.

the class MongoSession method deleteTableMetadata.

private boolean deleteTableMetadata(SchemaTableName schemaTableName) {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    MongoDatabase db = client.getDatabase(schemaName);
    if (!collectionExists(db, tableName)) {
        return false;
    }
    DeleteResult result = db.getCollection(schemaCollection).deleteOne(new Document(TABLE_NAME_KEY, tableName));
    return result.getDeletedCount() == 1;
}
Also used : Document(org.bson.Document) DeleteResult(com.mongodb.client.result.DeleteResult) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 25 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project presto by prestodb.

the class MongoSession method getTableMetadata.

// Internal Schema management
private Document getTableMetadata(SchemaTableName schemaTableName) throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    MongoDatabase db = client.getDatabase(schemaName);
    MongoCollection<Document> schema = db.getCollection(schemaCollection);
    Document doc = schema.find(new Document(TABLE_NAME_KEY, tableName)).first();
    if (doc == null) {
        if (!collectionExists(db, tableName)) {
            throw new TableNotFoundException(schemaTableName);
        } else {
            Document metadata = new Document(TABLE_NAME_KEY, tableName);
            metadata.append(FIELDS_KEY, guessTableFields(schemaTableName));
            schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
            schema.insertOne(metadata);
            return metadata;
        }
    }
    return doc;
}
Also used : TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

MongoDatabase (com.mongodb.client.MongoDatabase)34 Document (org.bson.Document)25 MongoClient (com.mongodb.MongoClient)14 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 MongoClientURI (com.mongodb.MongoClientURI)5 Bson (org.bson.conversions.Bson)4 ServerAddress (com.mongodb.ServerAddress)3 MongoCollection (com.mongodb.client.MongoCollection)3 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)3 IndexOptions (com.mongodb.client.model.IndexOptions)3 DeleteResult (com.mongodb.client.result.DeleteResult)3 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)2 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)2 TypeSignature (com.facebook.presto.spi.type.TypeSignature)2 ImmutableList (com.google.common.collect.ImmutableList)2 MongoCommandException (com.mongodb.MongoCommandException)2 ValidationOptions (com.mongodb.client.model.ValidationOptions)2 Date (java.util.Date)2 Configuration (com.alibaba.datax.common.util.Configuration)1