use of com.mongodb.client.MongoDatabase in project camel by apache.
the class MongoDbProducer method calculateDb.
// --------- Convenience methods -----------------------
private MongoDatabase calculateDb(Exchange exchange) {
// resolution logic on every Exchange if they won't be using this functionality at all
if (!endpoint.isDynamicity()) {
return endpoint.getMongoDatabase();
}
String dynamicDB = exchange.getIn().getHeader(MongoDbConstants.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 DataX by alibaba.
the class Entry method doSplit.
public static List<Configuration> doSplit(Configuration originalSliceConfig, int adviceNumber, MongoClient mongoClient) {
List<Configuration> confList = new ArrayList<Configuration>();
String dbName = originalSliceConfig.getString(KeyConstant.MONGO_DB_NAME);
String collectionName = originalSliceConfig.getString(KeyConstant.MONGO_COLLECTION_NAME);
if (Strings.isNullOrEmpty(dbName) || Strings.isNullOrEmpty(collectionName) || mongoClient == null) {
throw DataXException.asDataXException(MongoDBReaderErrorCode.ILLEGAL_VALUE, MongoDBReaderErrorCode.ILLEGAL_VALUE.getDescription());
}
String query = originalSliceConfig.getString(KeyConstant.MONGO_QUERY);
MongoDatabase db = mongoClient.getDatabase(dbName);
MongoCollection collection = db.getCollection(collectionName);
List<Entry> countInterval = doSplitInterval(adviceNumber, collection, query);
for (Entry interval : countInterval) {
Configuration conf = originalSliceConfig.clone();
conf.set(KeyConstant.SKIP_COUNT, interval.interval);
conf.set(KeyConstant.BATCH_SIZE, interval.batchSize);
confList.add(conf);
}
return confList;
}
use of com.mongodb.client.MongoDatabase in project mongo-hadoop by mongodb.
the class MongoSplitterFactoryTest method setUpClass.
@BeforeClass
public static void setUpClass() {
client = new MongoClient(uri);
// Create collection so that we can call "collstats" on it.
client.getDatabase(uri.getDatabase()).getCollection(uri.getCollection()).insertOne(new Document("this collection exists now", true));
// Shard the collection if we're connected to a sharded cluster.
Document isMaster = client.getDatabase("admin").runCommand(new Document("ismaster", 1));
if (isMaster.get("msg") != null && isMaster.get("msg").equals("isdbgrid")) {
String ns = uri.getDatabase() + "." + uri.getCollection();
MongoDatabase adminDB = client.getDatabase("admin");
adminDB.runCommand(new Document("enableSharding", uri.getDatabase()));
adminDB.runCommand(new Document("shardCollection", ns).append("key", new Document("_id", 1)));
}
}
use of com.mongodb.client.MongoDatabase in project mongo-hadoop by mongodb.
the class PigTest method testPigSchemaless.
@Test
public void testPigSchemaless() throws IOException, ParseException {
// Seed data used by "schemaless.pig"
MongoDatabase db = mongoClient.getDatabase("mongo_hadoop");
List<Document> documents = new ArrayList<Document>(1000);
for (int i = 0; i < 1000; ++i) {
documents.add(new Document("_id", i));
}
db.getCollection("pig.schemaless").insertMany(documents);
runScript("/pig/schemaless.pig");
assertEquals(1000, db.getCollection("pig.schemaless.out").count());
assertNotNull(db.getCollection("pig.schemaless.out").find(new Document("_id", 999)).first());
}
use of com.mongodb.client.MongoDatabase in project morphia by mongodb.
the class TestDocumentValidation method overwriteValidation.
@Test
public void overwriteValidation() {
Document validator = Document.parse("{ jelly : { $ne : 'rhubarb' } }");
MongoDatabase database = addValidation(validator, "validation");
assertEquals(validator, getValidator());
Document rhubarb = new Document("jelly", "rhubarb").append("number", 20);
database.getCollection("validation").insertOne(new Document("jelly", "grape"));
try {
database.getCollection("validation").insertOne(rhubarb);
fail("Document should have failed validation");
} catch (MongoWriteException e) {
assertTrue(e.getMessage().contains("Document failed validation"));
}
getMorphia().map(DocumentValidation.class);
getDs().enableDocumentValidation();
assertEquals(Document.parse(DocumentValidation.class.getAnnotation(Validation.class).value()), getValidator());
try {
database.getCollection("validation").insertOne(rhubarb);
} catch (MongoWriteException e) {
assertFalse(e.getMessage().contains("Document failed validation"));
}
try {
getDs().save(new DocumentValidation("John", 1, new Date()));
fail("Document should have failed validation");
} catch (WriteConcernException e) {
assertTrue(e.getMessage().contains("Document failed validation"));
}
}
Aggregations