Thank you.
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;
import com.mongodb.Mongo;
public class MongoClient {
/**
* @param args
*/
public static void main(String[] args) {
Mongo mongo;
try {
mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("mr_demo");
DBCollection life = db.getCollection("life_expectancy");
DBCollection econ = db.getCollection("us_economic_assistance");
String map1 = "function() { " +
"emit(this.country, {life_expectancy: this.age, dollars: 0});" +
"}";
String map2 = "us_econ_map = function() {" +
" if (this.FY2009 !== undefined && this.FY2009 !== null) {"+
" emit(this.country_name, {" +
" dollars: this.FY2009," +
" life_expectancy: 0" +
" });" +
" }" +
"}";
String reduce = "function(key, values) { "+
"var result = {dollars: 0, life_expectancy: 0}; "+
" values.forEach(function(value) { "+
" result.dollars += (value.dollars !== null) ? value.dollars : 0;"+
" if (result.life_expectancy === 0 && " +
" value.life_expectancy !== null " +
" ) { " +
" result.life_expectancy = value.life_expectancy; " +
" } " +
" }); " +
" return result; " +
"} ";
MapReduceCommand cmd1 = new MapReduceCommand(life, map1, reduce,
"reduced", MapReduceCommand.OutputType.REPLACE, null);
MapReduceCommand cmd2 = new MapReduceCommand(econ, map2, reduce,
"reduced", MapReduceCommand.OutputType.REDUCE, null);
MapReduceOutput out1 = life.mapReduce(cmd1);
MapReduceOutput out2 = econ.mapReduce(cmd2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}