Monogdb使用 MapReduce进行分组统计查询

1/** 2 3  *  4 5  * @param businessNo  6 7  * @param beginTime 开始时间 8 9  * @param endTime 结束时间 10 11  * @param pageNo  页码 12 13  * @param pageSize 分页大小 14 15  * @return 16 17  */ 18 19 public static List<DBObject> query(Integer businessNo,Date beginTime,Date endTime,int pageNo,int pageSize){ 20 21 22  StringBuffer map = new StringBuffer(); 23 24  map.append("function(){ "); 25 26  map.append("  var t = new Date(this.createTime);"); 27 28  map.append("  emit({ "); 29 30  map.append("     orgId:this.orgId,"); 31 32  map.append("     operatorId:this.operatorId,"); 33 34  map.append("     objId:this.businessParam.objId,"); 35 36  map.append("     creatDate:t.getFullYear() +'-'+(t.getMonth() + 1)+'-'+t.getDate()"); 37 38  map.append("   },"); 39 40  map.append("   {count:1}"); 41 42  map.append("  ); "); 43 44  map.append("} "); 45 46  log.info(">>>>>>>map:{}",map.toString()); 47 48   49 50  StringBuffer reduce = new StringBuffer(); 51 52  reduce.append("function(key, values) {"); 53 54  reduce.append("  var total = 0;"); 55 56  reduce.append("  for (var i=0; i<values.length; i++) { "); 57 58  reduce.append("     total += values[i].count;"); 59 60  reduce.append("  } "); 61 62  reduce.append("  return {count: total}; "); 63 64  reduce.append("} "); 65 66  log.info(">>>>>>>reduce:{}",reduce.toString()); 67 68   69 70  StringBuffer finalize = new StringBuffer(); 71 72  finalize.append("function(key, rvalues) { "); 73 74  finalize.append("   var sum = 0; "); 75 76  finalize.append("   if(rvalues.count>0){ "); 77 78  finalize.append("      rvalues.get='true'; "); 79 80  finalize.append("   }else{ "); 81 82  finalize.append("      rvalues.get='false'"); 83 84  finalize.append("   } "); 85 86  finalize.append("   return rvalues; "); 87 88  finalize.append("}"); 89 90  log.info(">>>>>>>finalize:{}",finalize.toString()); 91 92   93 94  //查询条件 95 96  DBObject query = new BasicDBObject(); 97 98  query.put("businessNo", businessNo); 99 100  query.put("operatorId", new BasicDBObject("$exists", true).append("$ne", 0)); 101 102  query.put("createTime", new BasicDBObject("$gte", beginTime.getTime()).append("$lte", endTime.getTime())); 103 104  query.put("businessParam.objId", new BasicDBObject("$exists", true)); 105 106  107  DBObject command = new BasicDBObject(); 108 109  command.put("mapreduce", "MyLogs"); 110 111  command.put("query", query); 112 113  command.put("map", map.toString()); 114 115  command.put("reduce", reduce.toString()); 116 117  command.put("out", "MapReduceCommand.OutputType.INLINE"); 118 119  command.put("verbose", true); 120 121   122 123  DBCollection db = MongoDbInit.getDb().getCollection("MyLogs"); 124 125  command.put("finalize", finalize.toString()); 126 127  MapReduceOutput out = db.mapReduce(command); 128 129   130 131  DBObject having = new BasicDBObject(); 132 133  having.put("value.get", "true"); 134 135   136 137  DBObject orderBy = new BasicDBObject(); 138 139  orderBy.put("createTime", -1);//按createTime倒序排   140 141   142 143  int skip = (pageNo-1)*pageSize; 144 145  List<DBObject> resultList = new ArrayList(); 146 147  resultList = out.getOutputCollection().find(having).sort(orderBy).skip(skip).limit(pageSize).toArray(); 148 149  return resultList; 150 151 }

数据结构

1{ 2 3  "_id" : ObjectId("52f9c392b7603fe75cff49e7"), 4 5  "deviceId" : "ffffffff-d0b9-867d-191b-921900000000", 6 7  "operationTime" : NumberLong("1392100740000"), 8 9  "operatorType" : 1, 10 11  "operatorId" : 3587, 12 13  "platform" : "android", 14 15  "businessStatus" : 1, 16 17  "sessionId" : "", 18 19  "orgId" : 104, 20 21  "businessName" : "活动详情", 22 23  "createTime" : NumberLong("1392100242676"), 24 25  "businessParam" : { 26 27    "objId" : 653, 28 29    "time" : 715, 30 31    "objType" : 1 32 33  }, 34 35  "businessNo" : 1101 36 37}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )