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();
}
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());
}
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();
}
}
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;
}
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));
}
Aggregations