mongo java mapreduce_MongoDB JAVA-API执行mapreduce的几种方法

本文详细介绍了MongoDB中的MapReduce操作,包括如何使用Java API执行MapReduce,展示了不同方法的参数和返回结果。示例代码演示了如何通过map和reduce函数进行数据聚合,并将结果存储到临时集合。文章最后提到了MongoDB的新聚合框架,提供了更多参数选择。
摘要由CSDN通过智能技术生成

1> db.version();21.6.3

1mongodb-2.0.jar

0105db.runCommand(06{07mapreduce : ,08map : ,09reduce : 10[, query : ]11[, sort : ]for12[, limit : ]return13[, out : ]14[, keeptemp: < | >]true false15[, finalize : ]16[, scope : ]17[, verbose :  ]true18});

1//JAVA-API中可以执行mapreduce的方法2//该方法只允许携带4个参数3public MapReduceOutput mapReduce(String map, String reduce,4String outputCollection,5DBObject query)6throws MongoException7//以下两个方法可以使用全部参数列表8public MapReduceOutput mapReduce(DBObject command)throws MongoException9public CommandResult command(DBObject cmd)throws MongoException

0104res = db.runCommand({05mapreduce:'mo_log_201208',06query:{Logtime:{$gte:'20120823', $lte:'20120824'}},07map:function() {08emit({Spcode:this.Spcode, Spname:this.Spname,09Consignid:this.Consignid, Consname:this.Consname,10Region:this.Region, Regionname:this.Regionname,11Serviceid:this.Serviceid,12Servicename:this.Servicename,13Srctermid:this.Srctermid}, {count:1});14},15reduce:function(key, value) {16var ret = {count:0};17ret.count++;18return ret;19},20out:'tmp_mo_spcode_consignid_region_serviceid_201208_1'21})01//JAVA-API mapreduce02Mongo db =new Mongo("host","port");03DBCollection dbc = db.getCollection("mo_log_201208");04String 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"}";12String reducefun ="function(key, value) {" +13"var ret = {count:0};" +14"ret.count++;" +15"return ret;" +16"}";17DBObject query =new BasicDBObject("Logtime",18new BasicDBObject("$gte","20120823").append("$lte","20120824"));19MapReduceOutput mro = dbc.mapReduce(mapfun, reducefun,20"tmp_mo_spcode_consignid_region_serviceid_201208_1", query);0104res = db.runCommand({05mapreduce:'mo_log_201208',06query:{Logtime:{$gte:'20120823', $lte:'20120824'}},07map:function() {08emit({Spcode:this.Spcode, Spname:this.Spname,09Consignid:this.Consignid, Consname:this.Consname,10Region:this.Region, Regionname:this.Regionname,11Serviceid:this.Serviceid, Servicename:this.Servicename,12Srctermid:this.Srctermid}, {count:1});13},14reduce:function(key, value) {15var ret = {count:0};16ret.count++;17return ret;18},19finalize:function(key, value){20db.tmp_mo_spcode_consignid_region_serviceid_201208.insert({"_id":key,"value":value});21return value;22},23out:'tmp_mo_spcode_consignid_region_serviceid_201208_1',24verbose:true25})01//JAVA-API mapreduce02Mongo db =new Mongo("host","port");03DBCollection dbc = db.getCollection("mo_log_201208");04String 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"}";12String reducefun ="function(key, value) {" +13"var ret = {count:0};" +14"ret.count++;" +15"return ret;" +16"}";17String finalizefun ="function(key, value){" +18"db.tmp_mo_spcode_consignid_region_serviceid_201208.insert({"_id":key, "value":value});" +19"return value;" +20"}";21DBObject query =new BasicDBObject("Logtime",22new BasicDBObject("$gte","20120823").append("$lte","20120824"));23DBObject command =new BasicDBObject();24command.put("mapreduce","mo_log_201208");25command.put("query", query);26command.put("map", mapfun);27command.put("reduce", reducefun);28command.put("finalize", finalizefun);29command.put("out","tmp_mo_spcode_consignid_region_serviceid_201208_1");30command.put("verbose",true);31//在dbcollection上执行mapreduce32MapReduceOutput mro = dbc.mapReduce(command);33//在db上使用command执行mapreduce34CommandResult cr = db.command(command);

>>以上3种方法都可以执行mapreduce, 参数和返回结果各不相同, 根据自己的需要和使用习惯选择合适的方法

>>新版的Mongodb增加了新的聚合框架, 有更多的参数选择, 参见Aggregation Framework

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值