mapreduce分组统计_Monogdb使用 MapReduce进行分组统计查询

/**

*

* @param businessNo

* @param beginTime 开始时间

* @param endTime 结束时间

* @param pageNo  页码

* @param pageSize 分页大小

* @return

*/

public static List query(Integer businessNo,Date beginTime,Date endTime,int pageNo,int pageSize){

StringBuffer map = new StringBuffer();

map.append("function(){ ");

map.append("  var t = new Date(this.createTime);");

map.append("  emit({ ");

map.append("     orgId:this.orgId,");

map.append("     operatorId:this.operatorId,");

map.append("     objId:this.businessParam.objId,");

map.append("     creatDate:t.getFullYear() +'-'+(t.getMonth() + 1)+'-'+t.getDate()");

map.append("   },");

map.append("   {count:1}");

map.append("  ); ");

map.append("} ");

log.info(">>>>>>>map:{}",map.toString());

StringBuffer reduce = new StringBuffer();

reduce.append("function(key, values) {");

reduce.append("  var total = 0;");

reduce.append("  for (var i=0; i

reduce.append("     total += values[i].count;");

reduce.append("  } ");

reduce.append("  return {count: total}; ");

reduce.append("} ");

log.info(">>>>>>>reduce:{}",reduce.toString());

StringBuffer finalize = new StringBuffer();

finalize.append("function(key, rvalues) { ");

finalize.append("   var sum = 0; ");

finalize.append("   if(rvalues.count>0){ ");

finalize.append("      rvalues.get='true'; ");

finalize.append("   }else{ ");

finalize.append("      rvalues.get='false'");

finalize.append("   } ");

finalize.append("   return rvalues; ");

finalize.append("}");

log.info(">>>>>>>finalize:{}",finalize.toString());

//查询条件

DBObject query = new BasicDBObject();

query.put("businessNo", businessNo);

query.put("operatorId", new BasicDBObject("$exists", true).append("$ne", 0));

query.put("createTime", new BasicDBObject("$gte", beginTime.getTime()).append("$lte", endTime.getTime()));

query.put("businessParam.objId", new BasicDBObject("$exists", true));

DBObject command = new BasicDBObject();

command.put("mapreduce", "MyLogs");

command.put("query", query);

command.put("map", map.toString());

command.put("reduce", reduce.toString());

command.put("out", "MapReduceCommand.OutputType.INLINE");

command.put("verbose", true);

DBCollection db = MongoDbInit.getDb().getCollection("MyLogs");

command.put("finalize", finalize.toString());

MapReduceOutput out = db.mapReduce(command);

DBObject having = new BasicDBObject();

having.put("value.get", "true");

DBObject orderBy = new BasicDBObject();

orderBy.put("createTime", -1);//按createTime倒序排

int skip = (pageNo-1)*pageSize;

List resultList = new ArrayList();

resultList = out.getOutputCollection().find(having).sort(orderBy).skip(skip).limit(pageSize).toArray();

return resultList;

}

数据结构

{

"_id" : ObjectId("52f9c392b7603fe75cff49e7"),

"deviceId" : "ffffffff-d0b9-867d-191b-921900000000",

"operationTime" : NumberLong("1392100740000"),

"operatorType" : 1,

"operatorId" : 3587,

"platform" : "android",

"businessStatus" : 1,

"sessionId" : "",

"orgId" : 104,

"businessName" : "活动详情",

"createTime" : NumberLong("1392100242676"),

"businessParam" : {

"objId" : 653,

"time" : 715,

"objType" : 1

},

"businessNo" : 1101

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值