MongoDB中使用MapReduce进行分组统计
MongoDB中使用MapReduce进行分组统计
最近在统计某一个时间段的url去重数,由于数据量巨大导致报错,提示:
distinct failed: {
"errmsg" : "exception: distinct too big, 16mb cap",
"code" : 17217,
"ok" : 0
} at src/mongo/shell/collection.js:1108
经过查阅资料,最终通过mapreduce来解决如下:
//定义map函数
map=function(){
emit(this.url,{"count":1});
}
//定义reduce函数
reduce=function(key,values){
var total=0;
for(var i=0; i < values.length; i++){
total+=values[i].count;
}
return {count:total}
}
//执行mapreduce函数,其中out的值是存储执行结果的集合
db.runCommand({"mapreduce":"visit","map":map,"reduce":reduce,"query":{"vtime":{"$gte":1412611200,"$lte":1413907119}},"out":"test.tmp"});
MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里
相关推荐
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09
zhushenghan 2020-11-09
sunnnyduan 2020-10-16
不要皱眉 2020-10-14
xiaohai 2020-09-29
songxiugongwang 2020-09-22
萌亖 2020-09-17
LuckyLXG 2020-09-08
sdmzhu 2020-09-01
mkhhxxttxs 2020-09-16
xiaohai 2020-09-16
newcome 2020-09-09
jaylong 2020-08-19
大秦铁骑 2020-08-19
thatway 2020-08-19