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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations