Maven
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.5</version>
</dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.5</version>
</dependency>
Connect to Mongo
import com.mongodb.DB;
import com.mongodb.Mongo;
//...
Mongo mongo = new Mongo(host, 27017);
DB db = mongo.getDB("name of db"); //If db does not exist, then it will be created.
import com.mongodb.Mongo;
//...
Mongo mongo = new Mongo(host, 27017);
DB db = mongo.getDB("name of db"); //If db does not exist, then it will be created.
Insert JSON Data
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
//...
DBCollection coll = db.getCollection("testCollection"); //If collection does not exist, then it will be created.
BasicDBObject doc = new BasicDBObject();
doc.put("hello", "world");
BasicDBObject inner = new BasicDBObject();
inner.put("key","value");
doc.put("inner", inner);
coll.insert(doc);
import com.mongodb.DBCollection;
//...
DBCollection coll = db.getCollection("testCollection"); //If collection does not exist, then it will be created.
BasicDBObject doc = new BasicDBObject();
doc.put("hello", "world");
BasicDBObject inner = new BasicDBObject();
inner.put("key","value");
doc.put("inner", inner);
coll.insert(doc);
Fetch JSON Data
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection
import com.mongodb.DBCursor;
//...
DBCollection coll = db.getCollection("testCollection");
BasicDBObject query = new BasicDBObject();
query.put("hello", "world");
DBCursor cursor = coll.find(query);
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
import com.mongodb.DBCollection
import com.mongodb.DBCursor;
//...
DBCollection coll = db.getCollection("testCollection");
BasicDBObject query = new BasicDBObject();
query.put("hello", "world");
DBCursor cursor = coll.find(query);
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
GridFS
import com.mongodb.gridfs.GridFS;
//...
GridFS fs = new GridFS(db, "collection"); //If bucket does not exist, it will be created.
//...
GridFS fs = new GridFS(db, "collection"); //If bucket does not exist, it will be created.
GridFS Save File
import java.io.File;
import com.mongodb.gridfs.GridFSInputFile;
//...
GridFSInputFile file = fs.createFile(new File("/path/to/file.ext"));
file.save();
import com.mongodb.gridfs.GridFSInputFile;
//...
GridFSInputFile file = fs.createFile(new File("/path/to/file.ext"));
file.save();
GridFS List Files
import com.mongodb.DBCursor;
//...
DBCursor cursor = fs.getFileList();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
//...
DBCursor cursor = fs.getFileList();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
GridFS Read File
GridFSDBFile file = fs.findOne("file.ext");
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
String line = null;
while((line = reader.readLine()) != null){
System.out.println(line);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
String line = null;
while((line = reader.readLine()) != null){
System.out.println(line);
}
Map/Reduce
Example Dataset:
{ "_id" : ObjectId("4d8233d6d638a2ca105b1fab"), "name" : "John", "number" : 2 }
{ "_id" : ObjectId("4d8233d6d638a2ca105b1fac"), "name" : "Jane", "number" : 2 }
{ "_id" : ObjectId("4d8233dbd638e40a183d27f8"), "name" : "John", "number" : 2 }
{ "_id" : ObjectId("4d8233dcd638e40a183d27f9"), "name" : "Jane", "number" : 2 }
Code Example:
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
//...
String map = "function(){" +
"emit(this.name, {count: 1, sum: this.number});" +
"};";
String reduce = "function( key , values ){" +
"var n = { count: 0, sum: 0}; " +
"for ( var i = 0; i < values.length; i ++ ) {" +
"n.sum += values[i].sum;" +
"n.count += values[i].count;" +
"};" +
"return n;" +
"};";
MapReduceOutput out = coll.mapReduce(map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
for ( DBObject obj : out.results() ) {
System.out.println( obj );
}
Expected Output:
{ "_id" : "Jane" , "value" : { "count" : 2.0 , "sum" : 4.0}}
{ "_id" : "John" , "value" : { "count" : 2.0 , "sum" : 4.0}}
{ "_id" : ObjectId("4d8233d6d638a2ca105b1fab"), "name" : "John", "number" : 2 }
{ "_id" : ObjectId("4d8233d6d638a2ca105b1fac"), "name" : "Jane", "number" : 2 }
{ "_id" : ObjectId("4d8233dbd638e40a183d27f8"), "name" : "John", "number" : 2 }
{ "_id" : ObjectId("4d8233dcd638e40a183d27f9"), "name" : "Jane", "number" : 2 }
Code Example:
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
//...
String map = "function(){" +
"emit(this.name, {count: 1, sum: this.number});" +
"};";
String reduce = "function( key , values ){" +
"var n = { count: 0, sum: 0}; " +
"for ( var i = 0; i < values.length; i ++ ) {" +
"n.sum += values[i].sum;" +
"n.count += values[i].count;" +
"};" +
"return n;" +
"};";
MapReduceOutput out = coll.mapReduce(map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
for ( DBObject obj : out.results() ) {
System.out.println( obj );
}
Expected Output:
{ "_id" : "Jane" , "value" : { "count" : 2.0 , "sum" : 4.0}}
{ "_id" : "John" , "value" : { "count" : 2.0 , "sum" : 4.0}}
Thanks Denis for such a wonderful tutorial
ReplyDelete