Search in sources :

Example 61 with MongoClient

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

the class PigTest method setup.

@Before
public void setup() throws UnknownHostException {
    mongoClient = new MongoClient(URI);
    db = mongoClient.getDB("mongo_hadoop");
    db.dropDatabase();
}
Also used : MongoClient(com.mongodb.MongoClient) Before(org.junit.Before)

Example 62 with MongoClient

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

the class Decimal128QuickTour 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
    MongoDatabase database = mongoClient.getDatabase("mydb");
    // get a handle to the "test" collection
    MongoCollection<Document> collection = database.getCollection("test");
    // drop all the data in it
    collection.drop();
    // make a document and insert it
    Document doc = new Document("name", "MongoDB").append("amount1", Decimal128.parse(".10")).append("amount2", new Decimal128(42L)).append("amount3", new Decimal128(new BigDecimal(".200")));
    collection.insertOne(doc);
    Document first = collection.find().filter(Filters.eq("amount1", new Decimal128(new BigDecimal(".10")))).first();
    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) MongoClientURI(com.mongodb.MongoClientURI) Decimal128(org.bson.types.Decimal128) Document(org.bson.Document) BigDecimal(java.math.BigDecimal) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 63 with MongoClient

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

the class MongoConfigUtil method close.

public static void close(final Mongo client) {
    MongoClientURI uri = URI_MAP.get().remove(client);
    if (uri != null) {
        MongoClient remove;
        remove = CLIENTS.get().remove(uri);
        if (remove != client) {
            throw new IllegalStateException("different clients found");
        }
        client.close();
    }
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI)

Example 64 with MongoClient

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

the class MongoConfigUtil method getMongoClient.

private static MongoClient getMongoClient(final MongoClientURI uri) throws UnknownHostException {
    MongoClient mongoClient = CLIENTS.get().get(uri);
    if (mongoClient == null) {
        mongoClient = new MongoClient(uri);
        CLIENTS.get().put(uri, mongoClient);
        URI_MAP.get().put(mongoClient, uri);
    }
    return mongoClient;
}
Also used : MongoClient(com.mongodb.MongoClient)

Example 65 with MongoClient

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

the class MongoRecordReaderTest method testGetCurrentKey.

@Test
public void testGetCurrentKey() throws Exception {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoClientURI uri = new MongoClientURIBuilder().collection("mongo_hadoop", "mongo_record_reader_test").build();
    DBCollection collection = client.getDB(uri.getDatabase()).getCollection(uri.getCollection());
    collection.drop();
    BasicDBList colors = new BasicDBList() {

        {
            add(new BasicBSONObject("red", 255));
            add(new BasicBSONObject("blue", 255));
            add(new BasicBSONObject("green", 0));
        }
    };
    collection.insert(new BasicDBObject("_id", 0).append("address", new BasicDBObject("street", "foo street")).append("colors", colors));
    // Default case: "_id" is used as inputKey.
    MongoInputSplit split = new MongoInputSplit();
    split.setInputURI(uri);
    MongoRecordReader reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), 0);
    // Use a nested field as inputKey.
    split = new MongoInputSplit();
    split.setInputURI(uri);
    split.setKeyField("address.street");
    reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), "foo street");
    // Use a key within an array as the inputKey.
    split = new MongoInputSplit();
    split.setInputURI(uri);
    split.setKeyField("colors.1");
    reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), new BasicBSONObject("blue", 255));
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) MongoInputSplit(com.mongodb.hadoop.input.MongoInputSplit) MongoClientURIBuilder(com.mongodb.hadoop.util.MongoClientURIBuilder) MongoRecordReader(com.mongodb.hadoop.input.MongoRecordReader) MongoClientURI(com.mongodb.MongoClientURI) Test(org.junit.Test)

Aggregations

MongoClient (com.mongodb.MongoClient)126 Test (org.junit.Test)31 MongoClientURI (com.mongodb.MongoClientURI)29 Document (org.bson.Document)26 ServerAddress (com.mongodb.ServerAddress)21 MongoDatabase (com.mongodb.client.MongoDatabase)21 Before (org.junit.Before)20 BasicDBObject (com.mongodb.BasicDBObject)11 ArrayList (java.util.ArrayList)11 MongoCredential (com.mongodb.MongoCredential)9 MongoException (com.mongodb.MongoException)8 DB (com.mongodb.DB)7 DBCollection (com.mongodb.DBCollection)7 UnknownHostException (java.net.UnknownHostException)7 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)7 DBObject (com.mongodb.DBObject)6 MongoClientOptions (com.mongodb.MongoClientOptions)6 IOException (java.io.IOException)5 List (java.util.List)5 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)5