Search in sources :

Example 11 with MongoClient

use of com.mongodb.MongoClient in project mongo-hadoop by mongodb.

the class StandaloneMongoSplitterTest method setUp.

@BeforeClass
public static void setUp() {
    MongoClient client = new MongoClient("localhost", 27017);
    uri = new MongoClientURIBuilder().collection("mongo_hadoop", "splitter_test").build();
    collection = client.getDB(uri.getDatabase()).getCollection(uri.getCollection());
    collection.drop();
    collection.createIndex("value");
    for (int i = 0; i < 40000; i++) {
        collection.insert(new BasicDBObject("_id", i).append("value", i));
    }
}
Also used : MongoClient(com.mongodb.MongoClient) BasicDBObject(com.mongodb.BasicDBObject) MongoClientURIBuilder(com.mongodb.hadoop.util.MongoClientURIBuilder) BeforeClass(org.junit.BeforeClass)

Example 12 with MongoClient

use of com.mongodb.MongoClient in project mongo-hadoop by mongodb.

the class HiveQueryTest method setUp.

@Before
public void setUp() {
    MongoClient client = new MongoClient("localhost:27017");
    coll = client.getDatabase("mongo_hadoop").getCollection("hive_query");
    for (int i = 0; i < 1000; ++i) {
        coll.insertOne(new Document("i", i).append("j", i % 5));
    }
}
Also used : MongoClient(com.mongodb.MongoClient) Document(org.bson.Document) Before(org.junit.Before)

Example 13 with MongoClient

use of com.mongodb.MongoClient in project mongo-hadoop by mongodb.

the class PigTest method testMongoStorageEnsureIndex.

@Test
public void testMongoStorageEnsureIndex() throws IOException, ParseException {
    runScript("/pig/ensure_index.pig");
    MongoClient client = new MongoClient("localhost:27017");
    // There should be an index on the "last" field, ascending.
    MongoCollection<Document> coll = client.getDatabase("mongo_hadoop").getCollection("ensure_indexes");
    assertTrue("Should have the index \"last_1\"", indexExists(coll, "last_1"));
    //  Drop the index.
    coll.dropIndex("last_1");
    // Run the second pig script, which ensures a different index.
    runScript("/pig/ensure_index_2.pig");
    assertTrue("Should have the index \"first_1\"", indexExists(coll, "first_1"));
    assertFalse("Should not have the index \"last_1\"", indexExists(coll, "last_1"));
}
Also used : MongoClient(com.mongodb.MongoClient) Document(org.bson.Document) Test(org.junit.Test) BaseHadoopTest(com.mongodb.hadoop.testutils.BaseHadoopTest)

Example 14 with MongoClient

use of com.mongodb.MongoClient in project mongo-hadoop by mongodb.

the class TablePropertiesTest method setUp.

@Before
public void setUp() {
    MongoClientURI clientURI = new MongoClientURI("mongodb://localhost:27017/mongo_hadoop.tabletest");
    MongoClient client = new MongoClient(clientURI);
    // Seed some documents into MongoDB.
    collection = client.getDatabase(clientURI.getDatabase()).getCollection(clientURI.getCollection());
    ArrayList<Document> documents = new ArrayList<Document>(1000);
    for (int i = 0; i < 1000; ++i) {
        documents.add(new Document("i", i));
    }
    collection.insertMany(documents);
    // Make sure table doesn't exist already.
    dropTable("props_file_test");
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI) ArrayList(java.util.ArrayList) Document(org.bson.Document) Before(org.junit.Before)

Example 15 with MongoClient

use of com.mongodb.MongoClient in project mongo-java-driver by mongodb.

the class Decimal128LegacyAPIQuickTour method main.

/**
     * Run this main method to see the output of this quick example.
     *
     * @param args takes an optional single argument for the connection string
     */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }
    // get handle to "mydb" database
    DB database = mongoClient.getDB("mydb");
    // get a handle to the "test" collection
    DBCollection collection = database.getCollection("test");
    // drop all the data in it
    collection.drop();
    // make a document and insert it
    BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("amount1", Decimal128.parse(".10")).append("amount2", new Decimal128(42L)).append("amount3", new Decimal128(new BigDecimal(".200")));
    collection.insert(doc);
    DBObject first = collection.findOne(QueryBuilder.start("amount1").is(new Decimal128(new BigDecimal(".10"))).get());
    Decimal128 amount3 = (Decimal128) first.get("amount3");
    BigDecimal amount2AsBigDecimal = amount3.bigDecimalValue();
    System.out.println(amount3.toString());
    System.out.println(amount2AsBigDecimal.toString());
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MongoClientURI(com.mongodb.MongoClientURI) Decimal128(org.bson.types.Decimal128) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB) BigDecimal(java.math.BigDecimal)

Aggregations

MongoClient (com.mongodb.MongoClient)81 MongoClientURI (com.mongodb.MongoClientURI)27 Test (org.junit.Test)20 Document (org.bson.Document)15 ServerAddress (com.mongodb.ServerAddress)13 MongoDatabase (com.mongodb.client.MongoDatabase)13 Before (org.junit.Before)10 BasicDBObject (com.mongodb.BasicDBObject)9 MongoCredential (com.mongodb.MongoCredential)8 ArrayList (java.util.ArrayList)8 DBCollection (com.mongodb.DBCollection)7 DB (com.mongodb.DB)6 DBObject (com.mongodb.DBObject)5 IOException (java.io.IOException)5 MongoClientURIBuilder (com.mongodb.hadoop.util.MongoClientURIBuilder)4 File (java.io.File)4 Closer (com.google.common.io.Closer)3 BasicDBList (com.mongodb.BasicDBList)3 MongoClientOptions (com.mongodb.MongoClientOptions)3 MongoException (com.mongodb.MongoException)3