use of com.mongodb.client.MongoClient in project logging-log4j2 by apache.
the class MongoDb4Test 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"));
Assert.assertEquals(first.toJson(), "INFO", first.getString("level"));
}
}
use of com.mongodb.client.MongoClient in project gora by apache.
the class TestMongoStoreMetadataAnalyzer method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
Properties prop = DataStoreFactory.createProps();
Configuration conf = testDriver.getConfiguration();
MongoStoreParameters parameters = MongoStoreParameters.load(prop, conf);
MongoClient mongoClient = MongoStore.getClient(parameters);
mongoDatabase = mongoClient.getDatabase(parameters.getDbname());
storeMetadataAnalyzer = DataStoreMetadataFactory.createAnalyzer(conf);
}
use of com.mongodb.client.MongoClient in project drill by apache.
the class MongoGroupScan method getScanStats.
@Override
public ScanStats getScanStats() {
try {
MongoClient client = storagePlugin.getClient();
MongoDatabase db = client.getDatabase(scanSpec.getDbName());
MongoCollection<Document> collection = db.getCollection(scanSpec.getCollectionName());
long recordCount = collection.estimatedDocumentCount();
float approxDiskCost = 0;
if (recordCount != 0) {
// toJson should use client's codec, otherwise toJson could fail on
// some types not known to DocumentCodec, e.g. DBRef.
DocumentCodec codec = new DocumentCodec(db.getCodecRegistry(), new BsonTypeClassMap());
String json = collection.find().first().toJson(codec);
approxDiskCost = json.getBytes().length * recordCount;
}
return new ScanStats(GroupScanProperty.ESTIMATED_TOTAL_COST, recordCount, 1, approxDiskCost);
} catch (Exception e) {
throw new DrillRuntimeException(e.getMessage(), e);
}
}
use of com.mongodb.client.MongoClient in project drill by apache.
the class MongoStoragePlugin method getClient.
public synchronized MongoClient getClient(List<ServerAddress> addresses) {
// Take the first replica from the replicated servers
ServerAddress serverAddress = addresses.get(0);
MongoCredential credential = clientURI.getCredential();
String userName = credential == null ? null : credential.getUserName();
MongoCnxnKey key = new MongoCnxnKey(serverAddress, userName);
try {
return addressClientMap.get(key, () -> {
MongoClientSettings.Builder settings;
if (clientURI.isSrvProtocol()) {
settings = MongoClientSettings.builder().applyConnectionString(clientURI);
logger.info("Created srv protocol connection to {}.", key);
} else {
settings = MongoClientSettings.builder().applyToClusterSettings(builder -> builder.hosts(addresses));
if (credential != null) {
settings.credential(credential);
}
logger.info("Created connection to {}.", key);
}
// include this created
logger.info("Number of open connections {}.", addressClientMap.size() + 1);
return MongoClients.create(settings.build());
});
} catch (ExecutionException e) {
throw new DrillRuntimeException(e);
}
}
use of com.mongodb.client.MongoClient in project drill by apache.
the class MongoRecordReader method init.
private void init(BaseMongoSubScanSpec 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().getBoolean(ExecConstants.ENABLE_UNION_TYPE_KEY);
collection = db.getCollection(subScanSpec.getCollectionName(), BsonDocument.class);
}
Aggregations