MapReduce 和 MongoDb 上的按日期分组

发布于 2024-12-10 15:37:21 字数 2473 浏览 0 评论 0原文

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

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

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

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

所以根据我的理解,我需要执行两次 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]

我的方向正确吗?

I'm trying to test out mongoDb to manage our performance logs.

For this test I inserted 10M rows in a collection using the following format

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

Now I would like to be able to get average load time by day.

So from what I understood I need to do 2 pass of MapReduce.

the first one would be to create a collection of days.

So I tried

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};
}

Refering to http://cookbook.mongodb.org/patterns/unique_items_map_reduce/

But this kills my mongodb server after a few seconds.

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

Here is my error log

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]

Am I going in the right direction ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2024-12-17 15:37:21

我也遇到了同样的问题并解决如下。

而不是

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

使用

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

I also got same problem and solve it as follows.

Instead of

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

use

for(v in values){
   count += v['count'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文