在MongoDb上按日期MapReduce和分组

时间:2011-10-18 10:00:07

标签: mongodb mapreduce

我正在尝试测试mongoDb来管理我们的性能日志。

对于此测试,我使用以下格式在集合中插入了10M行

{ "_id" : ObjectId("4e9d3cc4621dc1dc11000000"), "date" : "Thu Oct 13 2011 15:37:21 GMT+0200 (CEST)", "loadtime" : 0.07, "msg" : "Lorem ipsum message" }

现在我希望能够白天获得平均加载时间。

所以根据我的理解,我需要做2次MapReduce传递。

第一个是创建一个天的集合。

所以我试过

map = function() {
  day = Date.UTC(this.date.getFullYear(), this.date.getMonth(), this.date.getDate());

  emit({day: day}, {count: 1});
}

reduce = function(key, values) {
  var count = 0;

  values.forEach(function(v) {
    count += v['count'];
  });

  return {count: count};
}

参考http://cookbook.mongodb.org/patterns/unique_items_map_reduce/

但这会在几秒钟之后杀死我的mongodb服务器。

db.loadTime.mapReduce(map, reduce, {out: 'days'});
Tue Oct 18 11:57:28 query failed : test.$cmd { mapreduce: "loadTime", map: function () {
    day = Date.UTC(this.date.getFullYear(), this.date.ge..., reduce: function (key, values) {
    var count = 0;
    values.forEach(functio..., out: "days" } to: 127.0.0.1
Tue Oct 18 11:57:28 Error: error doing query: failed (anon):1509

这是我的错误日志

Tue Oct 18 11:56:08 [conn1] CMD: drop test.tmp.mr.mapreduce_1318931768_1_inc
        55800/10000000  0%
        112800/10000000 1%
        171400/10000000 1%
        229600/10000000 2%
        288600/10000000 2%
        345600/10000000 3%
        404100/10000000 4%
        462900/10000000 4%
        522000/10000000 5%
        579100/10000000 5%
        629200/10000000 6%
        677000/10000000 6%
        724200/10000000 7%
        767500/10000000 7%
        818600/10000000 8%
        864300/10000000 8%
        921300/10000000 9%
        972200/10000000 9%
        1021600/10000000    10%
        1070700/10000000    10%
        1115600/10000000    11%
        1163600/10000000    11%
        1217400/10000000    12%
        1269100/10000000    12%
        1313300/10000000    13%
        1366200/10000000    13%
Tue Oct 18 11:57:28 Got signal: 11 (Segmentation fault).

Tue Oct 18 11:57:28 Backtrace:
0x843a16d 0x842dbcd 0x741400 0x1eadcd 
 /usr/lib/mongodb/mongod(_ZN5mongo15printStackTraceERSo+0x2d) [0x843a16d]
 /usr/lib/mongodb/mongod(_ZN5mongo10abruptQuitEi+0x3ed) [0x842dbcd]
 [0x741400]
 /usr/lib/xulrunner-2.0/libmozjs.so(+0xdadcd) [0x1eadcd]

我是朝着正确的方向前进的吗?

1 个答案:

答案 0 :(得分:4)

我也有同样的问题并按如下方式解决。

而不是

values.forEach(function(v) {
   count += v['count'];
});

使用

for(v in values){
   count += v['count'];
}