Search in sources :

Example 1 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) {
    // 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;
}
Also used : MongoDatabase(com.mongodb.client.MongoDatabase)

Example 2 with MongoDatabase

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;
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) Configuration(com.alibaba.datax.common.util.Configuration) ArrayList(java.util.ArrayList) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 3 with MongoDatabase

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)));
    }
}
Also used : MongoClient(com.mongodb.MongoClient) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) BeforeClass(org.junit.BeforeClass)

Example 4 with MongoDatabase

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());
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test) BaseHadoopTest(com.mongodb.hadoop.testutils.BaseHadoopTest)

Example 5 with MongoDatabase

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"));
    }
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) Validation(org.mongodb.morphia.annotations.Validation) DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) WriteConcernException(com.mongodb.WriteConcernException) Document(org.bson.Document) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

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