2013-04-12 124 views
1

作为MongoDB的新手我试图让MapReduce调用进行。 不幸的是我得到以下错误:Mongo Map减少错误

mongos> db.events.mapReduce(m,r,"errors"); 
Fri Apr 12 11:39:15.637 JavaScript execution failed: map reduce failed:{ 
    "ok" : 0, 
    "errmsg" : "MR parallel processing failed: { errmsg: \"exception: reduce -> multiple not supported yet\", code: 10075, ok: 0.0 }" 
} at src/mongo/shell/collection.js:L970 
mongos> 

我使用蒙戈2.4.0

我的活动集合看起来是这样的:

{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "S", "service" : "service1" } 
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "I", "service" : "service1" } 
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.236Z"), "class" : "E", "text" : "error message" } 
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.239Z"), "class" : "F", "service" : "service1" } 

{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "S", "service" : "service2" } 
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "I", "service" : "service2" } 
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.173Z"), "class" : "E", "text" : "another error message" } 
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.188Z"), "class" : "F", "service" : "service2" } 

{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "I", "service" : "service1" } 
{ ""sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "S", "service" : "service1" } 
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.853Z"), "class" : "F", "service" : "service1" } 
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.852Z"), "class" : "I", "service" : "service1" } 

我的目的是让所有的 “E”条目并将它们与“F”条目的相应“sid”的“服务”字段组合。所以这应该是非常简单的,但我无法得到它(因为上述错误)。

地图/减少I使用的是功能:

var m = function() { 
    if (this.class == "E") { 
     value = { time: this.time, error: this.text }; 
     emit(this.sid, value); 
    } else if (this.class == "F") { 
     value = { service: this.service }; 
     emit(this.sid, value); 
    } 
} 


var r = function(key,values) { 
    return values; 
} 

db.events.mapReduce(m,r,"errors"); 

回答

4

问题是,你想积累每个键的所有错误,这意味着你必须发出一个文件数组,然后你的减少必须将它们组合成一个单一的数组(因为它将接收一个数组阵列)。

您遇到的另一个问题是您想将每个独特cid的不同类别合并为一些组合缩减结果,但您的缩减功能甚至不会尝试这样做。

很难知道你可能有多少种不同的类值获得每个SID,但如果这就像你给的例子数据,我猜你想是这样的:

var m = function() { 
    array = []; 
    if (this.class == "E") { 
     value = { time: this.time, error: this.text }; 
     array.push(value); 
     emit(this.sid, {a:array}); 
    } else if (this.class == "F") { 
     value = { service: this.service }; 
     array.push(value); 
     emit(this.sid, {a:array}); 
    } 
} 


var r = function(key,values) { 
    result = {a:[]}; 
    values.forEach(function (v) { 
     result.a = v.a.concat(result.a); 
    }); 
    return result; 
} 

db.events.mapReduce(m,r,"errors"); 

在你的榜样,所产生的收集将是这样的:

> db.errors.find().pretty() 
{ 
    "_id" : "1UADM45MCGRTW ", 
    "value" : { 
     "a" : [ 
      { 
       "service" : "service1" 
      }, 
      { 
       "time" : ISODate("2013-04-08T11:33:13.236Z"), 
       "error" : "error message" 
      } 
     ] 
    } 
} 
{ 
    "_id" : "1UDO3H7YUOK08 ", 
    "value" : { 
     "a" : [ 
      { 
       "service" : "service2" 
      }, 
      { 
       "time" : ISODate("2013-04-08T11:33:08.173Z"), 
       "error" : "another error message" 
      } 
     ] 
    } 
} 
{ 
    "_id" : "1UDO3JVXIS2UZ ", 
    "value" : { 
     "a" : [ 
      { 
       "service" : "service1" 
      } 
     ] 
    } 
} 

你或许可以添加一个finalize函数,这个数组转换成一个结果,这是一个单一的文件,但是这取决于你想有和上产生的收集格式每个sid的预期输入。

+0

非常有帮助 - 谢谢 – pitseeker

0

根据文档(troubleshoot reduce function) “的降低函数必须返回一个对象,其类型必须与由射出的值的类型地图功能

当你发射一个物体时,reduce也应该发射一个物体 - 所以把values包裹在一个物体中,你应该很好走。