|
转载自:http://my.oschina.net/huzorro/blog/75210
1> db.version();21.6.31mongodb-2.0.jar0105db.runCommand( 06{ 07 mapreduce : , 08 map : , 09 reduce : 10 [, query : ] 11 [, sort : ] for 12 [, limit : ] return 13 [, out : ] 14 [, keeptemp: < | >] true false 15 [, finalize : ] 16 [, scope : ] 17 [, verbose : ] true 18});1//JAVA-API中可以执行mapreduce的方法2//该方法只允许携带4个参数3public MapReduceOutput mapReduce(String map, String reduce, 4 String outputCollection, 5 DBObject query) 6 throws MongoException7//以下两个方法可以使用全部参数列表8public MapReduceOutput mapReduce(DBObject command) throws MongoException9public CommandResult command(DBObject cmd) throws MongoException0104res = db.runCommand({05 mapreduce:'mo_log_201208',06 query:{Logtime:{$gte:'20120823', $lte:'20120824'}},07 map:function() {08 emit({Spcode:this.Spcode, Spname:this.Spname, 09 Consignid:this.Consignid, Consname:this.Consname, 10 Region:this.Region, Regionname:this.Regionname, 11 Serviceid:this.Serviceid, 12 Servicename:this.Servicename, 13 Srctermid:this.Srctermid}, {count:1});14 },15 reduce:function(key, value) {16 var ret = {count:0};17 ret.count++;18 return ret;19 },20 out:'tmp_mo_spcode_consignid_region_serviceid_201208_1'21})01//JAVA-API mapreduce 02 Mongo db = new Mongo("host", "port");03 DBCollection dbc = db.getCollection("mo_log_201208");04 String mapfun = "function() {" +05 "emit({Spcode:this.Spcode, Spname:this.Spname, " +06 "Consignid:this.Consignid, Consname:this.Consname, " +07 "Region:this.Region, Regionname:this.Regionname, " +08 "Serviceid:this.Serviceid, " + 09 "Servicename:this.Servicename, " +10 "Srctermid:this.Srctermid}, {count:1});" +11 "}";12 String reducefun = "function(key, value) {" +13 "var ret = {count:0};" +14 "ret.count++;" +15 "return ret;" +16 "}";17 DBObject query = new BasicDBObject("Logtime", 18 new BasicDBObject("$gte", "20120823").append("$lte", "20120824"));19 MapReduceOutput mro = dbc.mapReduce(mapfun, reducefun, 20 "tmp_mo_spcode_consignid_region_serviceid_201208_1", query);0104res = db.runCommand({05 mapreduce:'mo_log_201208',06 query:{Logtime:{$gte:'20120823', $lte:'20120824'}},07 map:function() {08 emit({Spcode:this.Spcode, Spname:this.Spname, 09 Consignid:this.Consignid, Consname:this.Consname, 10 Region:this.Region, Regionname:this.Regionname, 11 Serviceid:this.Serviceid, Servicename:this.Servicename, 12 Srctermid:this.Srctermid}, {count:1});13 },14 reduce:function(key, value) {15 var ret = {count:0};16 ret.count++;17 return ret;18 },19 finalize:function(key, value){20 db.tmp_mo_spcode_consignid_region_serviceid_201208.insert({"_id":key, "value":value});21 return value;22 },23 out:'tmp_mo_spcode_consignid_region_serviceid_201208_1',24 verbose:true25})01//JAVA-API mapreduce02 Mongo db = new Mongo("host", "port");03 DBCollection dbc = db.getCollection("mo_log_201208");04 String mapfun = "function() {" +05 "emit({Spcode:this.Spcode, Spname:this.Spname, " +06 "Consignid:this.Consignid, Consname:this.Consname, " +07 "Region:this.Region, Regionname:this.Regionname, " +08 "Serviceid:this.Serviceid, " + 09 "Servicename:this.Servicename, " +10 "Srctermid:this.Srctermid}, {count:1});" +11 "}";12 String reducefun = "function(key, value) {" +13 "var ret = {count:0};" +14 "ret.count++;" +15 "return ret;" +16 "}";17 String finalizefun = "function(key, value){" +18 "db.tmp_mo_spcode_consignid_region_serviceid_201208.insert({"_id":key, "value":value});" +19 "return value;" +20 "}";21 DBObject query = new BasicDBObject("Logtime", 22 new BasicDBObject("$gte", "20120823").append("$lte", "20120824"));23 DBObject command = new BasicDBObject();24 command.put("mapreduce", "mo_log_201208");25 command.put("query", query);26 command.put("map", mapfun);27 command.put("reduce", reducefun);28 command.put("finalize", finalizefun);29 command.put("out", "tmp_mo_spcode_consignid_region_serviceid_201208_1");30 command.put("verbose", true);31 //在dbcollection上执行mapreduce32 MapReduceOutput mro = dbc.mapReduce(command);33 //在db上使用command执行mapreduce34 CommandResult cr = db.command(command); >>以上3种方法都可以执行mapreduce, 参数和返回结果各不相同, 根据自己的需要和使用习惯选择合适的方法
>>新版的Mongodb增加了新的聚合框架, 有更多的参数选择, 参见Aggregation Framework
|
|