Mapreduce two collections using Java

1,070 views
Skip to first unread message

Christopher Carver

unread,
Nov 7, 2012, 1:02:15 PM11/7/12
to mongod...@googlegroups.com
I read the following article on how to use mapreduce to join collections in MongoDB. I was able to perform the steps with no problems. Using the same tables from the article, I tried performing this task using Java.

However I am not getting the same results when porting this to Java and I'm not understanding why. The second mapReduce() call isn't doing anything. I get the "reduced" collection after the first call to mapReduce(), but the collection is not updated when make the second mapReduce() call; the dollars key is always 0.

Where am I going wrong? I tried different combinations to no avail. 

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();  
  }  
 }  
}  
MongoClient.java

Christopher Carver

unread,
Nov 7, 2012, 1:48:30 PM11/7/12
to mongod...@googlegroups.com
Ugh.

I found the issue.

Original:
   String map2 = "us_econ_map = function() {" +

Replace:
 String map2 = "function() {" +

Sloppy copy and paste. 

Canada Dennis Chen

unread,
Jan 29, 2015, 6:02:21 PM1/29/15
to mongod...@googlegroups.com
Thanks for posting ... This is what I looking for ... :-)


Christopher Carver於 2012年11月7日星期三 UTC-7上午11時02分15秒寫道:
Reply all
Reply to author
Forward
0 new messages