MapReduce在pymongo中执行失败,但在mongoshell中成功

发布于 2024-11-28 21:51:55 字数 9245 浏览 3 评论 0原文

我遇到了关于 mongo map reduce 的问题,我的 map reduce 可以在 mongo shell 中执行成功,但是当我尝试执行 use pymongo 时,出现以下错误。

以下 json 是我的 Collections JSON 格式示例

{ "_id" : ObjectId( "4e41661ecacbd10e00012600" ),
  "timestamp" : "20110809",
  "variants" : { "407" : { "number_of_ad_clicks" : 101,
      "number_of_search_keywords" : 20,
      "total_duration" : 4,
      "os" : { "os_2" : 2,
        "os_1" : 1,
        "os_0" : 0 },
      "countries" : { "ge" : 2,
        "ca" : 7,
        "fr" : 2,
        "uk" : 5,
        "us" : 2 },
      "screen_resolutions" : { "(320, 240)" : 5,
        "(640, 480)" : 7,
        "(1024, 960)" : 9,
        "(1280, 768)" : 6 },
      "widgets" : { "widget_1" : 1,
        "widget_0" : 0 },
      "languages" : { "ua_uk" : 8,
        "ca_en" : 6,
        "ca_fr" : 8,
        "us_en" : 1 },
      "search_keywords" : { "search_keyword_15" : 15,
        "search_keyword_14" : 14,
        "search_keyword_17" : 17,
        "search_keyword_16" : 16,
        "search_keyword_11" : 11,
        "search_keyword_10" : 10,
        "search_keyword_13" : 13,
        "search_keyword_12" : 12,
        "search_keyword_19" : 19,
        "search_keyword_18" : 18,
        "search_keyword_9" : 9,
        "search_keyword_8" : 8,
        "search_keyword_5" : 5,
        "search_keyword_4" : 4,
        "search_keyword_7" : 7,
        "search_keyword_6" : 6,
        "search_keyword_1" : 1,
        "search_keyword_3" : 3,
        "search_keyword_2" : 2 },
      "number_of_pageviews" : 38,
      "browsers" : { "browser_4" : 4,
        "browser_0" : 0,
        "browser_1" : 1,
        "browser_2" : 2,
        "browser_3" : 3 },
      "keywords" : { "keyword_5" : 5,
        "keyword_4" : 4,
        "keyword_1" : 1,
        "keyword_0" : 0,
        "keyword_3" : 3,
        "keyword_2" : 2 },
      "number_of_keyword_clicks" : 205,
      "number_of_visits" : 91 },
    "306" : { "number_of_ad_clicks" : 29,
      "number_of_search_keywords" : 4,
      "total_duration" : 4,
      "os" : { "os_2" : 2,
        "os_1" : 1,
        "os_0" : 0 },
      "countries" : { "ge" : 7,
        "ca" : 7,
        "fr" : 6,
        "uk" : 1,
        "us" : 3 },
      "screen_resolutions" : { "(320, 240)" : 2,
        "(640, 480)" : 1,
        "(1024, 960)" : 9,
        "(1280, 768)" : 5 },
      "widgets" : { "widget_1" : 1,
        "widget_0" : 0 },
      "languages" : { "ua_uk" : 2,
        "ca_en" : 8,
        "ca_fr" : 5,
        "us_en" : 4 },
      "search_keywords" : { "search_keyword_1" : 1,
        "search_keyword_3" : 3,
        "search_keyword_2" : 2 },
      "number_of_pageviews" : 35,
      "browsers" : { "browser_4" : 4,
        "browser_0" : 0,
        "browser_1" : 1,
        "browser_2" : 2,
        "browser_3" : 3 },
      "keywords" : { "keyword_5" : 5,
        "keyword_4" : 4,
        "keyword_1" : 1,
        "keyword_0" : 0,
        "keyword_3" : 3,
        "keyword_2" : 2 },
      "number_of_keyword_clicks" : 18,
      "number_of_visits" : 57 },
    "408" : { "number_of_ad_clicks" : 180,
      "number_of_search_keywords" : 41,
      "total_duration" : 7,
      "os" : { "os_2" : 2,
        "os_1" : 1,
        "os_0" : 0 },
      "countries" : { "ge" : 3,
        "ca" : 6,
        "fr" : 3,
        "uk" : 9,
        "us" : 9 },
      "screen_resolutions" : { "(320, 240)" : 9,
        "(640, 480)" : 9,
        "(1024, 960)" : 5,
        "(1280, 768)" : 10 },
      "widgets" : { "widget_1" : 1,
        "widget_0" : 0 },
      "languages" : { "ua_uk" : 3,
        "ca_en" : 2,
        "ca_fr" : 10,
        "us_en" : 7 },
      "search_keywords" : { "search_keyword_37" : 37,
        "search_keyword_36" : 36,
        "search_keyword_28" : 28,
        "search_keyword_29" : 29,
        "search_keyword_24" : 24,
        "search_keyword_25" : 25,
        "search_keyword_26" : 26,
        "search_keyword_27" : 27,
        "search_keyword_20" : 20,
        "search_keyword_21" : 21,
        "search_keyword_22" : 22,
        "search_keyword_23" : 23,
        "search_keyword_39" : 39,
        "search_keyword_38" : 38,
        "search_keyword_40" : 40,
        "search_keyword_15" : 15,
        "search_keyword_14" : 14,
        "search_keyword_17" : 17,
        "search_keyword_16" : 16,
        "search_keyword_11" : 11,
        "search_keyword_10" : 10,
        "search_keyword_13" : 13,
        "search_keyword_12" : 12,
        "search_keyword_33" : 33,
        "search_keyword_32" : 32,
        "search_keyword_31" : 31,
        "search_keyword_30" : 30,
        "search_keyword_19" : 19,
        "search_keyword_18" : 18,
        "search_keyword_35" : 35,
        "search_keyword_34" : 34,
        "search_keyword_9" : 9,
        "search_keyword_8" : 8,
        "search_keyword_5" : 5,
        "search_keyword_4" : 4,
        "search_keyword_7" : 7,
        "search_keyword_6" : 6,
        "search_keyword_1" : 1,
        "search_keyword_3" : 3,
        "search_keyword_2" : 2 },
      "number_of_pageviews" : 25,
      "browsers" : { "browser_4" : 4,
        "browser_0" : 0,
        "browser_1" : 1,
        "browser_2" : 2,
        "browser_3" : 3 },
      "keywords" : { "keyword_5" : 5,
        "keyword_4" : 4,
        "keyword_1" : 1,
        "keyword_0" : 0,
        "keyword_3" : 3,
        "keyword_2" : 2 },
      "number_of_keyword_clicks" : 15,
      "number_of_visits" : 19 } },
  "site_name" : "radiotiempo.com",
  "number_of_variants" : 3 }

这是我的地图缩减。

map = function(){
    emit(1, {variants:this.variants});
}

reduce = function(key, vals) {
    var returnValue = { 
      clicks: 0, 
     };
    for(var j = 0 ; j < vals.length; j++){
       for(var i = 0 ; i < variant_ids.length; i++){
          try{
             returnValue.clicks += vals[j].variants[variant_ids[i]].number_of_ad_clicks;
          }catch(err)
          {}
       }
    }
    return returnValue;
}


function emit(k, v) {
    print("emit");
    print("  k:" + k + " v:" + tojson(v));
}

res = db.variant_daily_collection.mapReduce(map, reduce, {"scope": {'variant_ids': ['4519','4518']}, "out" : "myoutput", "query":{"site_name": {'$in':['julie2.com','julie3.com']}, 'timestamp': {'$gte':'20110601','$lte':'20110603'}}})
db.myoutput.find()

以下代码是我使用 pymongo lib 的 python 代码。

map = Code("function () {"
                   "    emit(1, {variants:this.variants});"
                   "}")
        reduce = Code("function (key, values) {"
                      "   var result = {"
                      "       clicks: 0"
                      "   };"
                      "   for (var i = 0; i < values.length; i++) {"
                      "       for(var j = 0 ; j < variant_ids.length; j++){"
                      "           result.clicks += values[i].variants[variant_ids[j]].number_of_ad_clicks;"
                      "       }"
                      "    }"
                      "    return result;"
                      "}")

以下是 pymongo 返回的错误

In [103]:  reduce = Code("function (key, valudb.variant_daily_collection.map_reduce(map, reduce, out = 'output',full_response = True, fields = {"scope": {'variant_ids': ['398']}, "query":{"site_name": 'routeplanner.net', 'timestamp': '20110809'}} )                                                                                                                   .....: ---------------------------------------------------------------------------OperationFailure                          Traceback (most recent call last)/workspace/construction/<ipython console> in <module>()/workspace/construction/.ve/lib/python2.7/site-packages/pymongo-2.0-py2.7-macosx-10.7-intel.egg/pymongo/collection.py in map_reduce(self, map, reduce, out, merge_output, reduce_output, full_response, **kwargs)
   1031         response = self.__database.command("mapreduce", self.__name,
   1032                                            map=map, reduce=reduce,
-> 1033                                            out=out_conf, **kwargs)
   1034 
   1035         if full_response or not response.get('result'):

/workspace/construction/.ve/lib/python2.7/site-packages/pymongo-2.0-py2.7-macosx-10.7-intel.egg/pymongo/database.py in command(self, command, value, check, allowable_errors, **kwargs)
    338             msg = "command %r failed: %%s" % command
    339             helpers._check_command_response(result, self.connection.disconnect,
--> 340                                             msg, allowable_errors)
    341 
    342         return result

/workspace/construction/.ve/lib/python2.7/site-packages/pymongo-2.0-py2.7-macosx-10.7-intel.egg/pymongo/helpers.py in _check_command_response(response, reset, msg, allowable_errors)
    123                     ex_msg += (", assertionCode: %d" %
    124                                (response["assertionCode"],))
--> 125                 raise OperationFailure(ex_msg, response.get("assertionCode"))
    126             raise OperationFailure(msg % response["errmsg"])
    127 

OperationFailure: db assertion failure, assertion: 'invoke failed: JS Error: TypeError: values[i].variants[variant_ids[j]] has no properties nofile_b:0', assertionCode: 9004

In [104]: 

I got a problem about mongo map reduce, my map reduce can execute success in mongo shell but when i try to execute use pymongo i got the following errors.

The following json is my Collections JSON format sample

{ "_id" : ObjectId( "4e41661ecacbd10e00012600" ),
  "timestamp" : "20110809",
  "variants" : { "407" : { "number_of_ad_clicks" : 101,
      "number_of_search_keywords" : 20,
      "total_duration" : 4,
      "os" : { "os_2" : 2,
        "os_1" : 1,
        "os_0" : 0 },
      "countries" : { "ge" : 2,
        "ca" : 7,
        "fr" : 2,
        "uk" : 5,
        "us" : 2 },
      "screen_resolutions" : { "(320, 240)" : 5,
        "(640, 480)" : 7,
        "(1024, 960)" : 9,
        "(1280, 768)" : 6 },
      "widgets" : { "widget_1" : 1,
        "widget_0" : 0 },
      "languages" : { "ua_uk" : 8,
        "ca_en" : 6,
        "ca_fr" : 8,
        "us_en" : 1 },
      "search_keywords" : { "search_keyword_15" : 15,
        "search_keyword_14" : 14,
        "search_keyword_17" : 17,
        "search_keyword_16" : 16,
        "search_keyword_11" : 11,
        "search_keyword_10" : 10,
        "search_keyword_13" : 13,
        "search_keyword_12" : 12,
        "search_keyword_19" : 19,
        "search_keyword_18" : 18,
        "search_keyword_9" : 9,
        "search_keyword_8" : 8,
        "search_keyword_5" : 5,
        "search_keyword_4" : 4,
        "search_keyword_7" : 7,
        "search_keyword_6" : 6,
        "search_keyword_1" : 1,
        "search_keyword_3" : 3,
        "search_keyword_2" : 2 },
      "number_of_pageviews" : 38,
      "browsers" : { "browser_4" : 4,
        "browser_0" : 0,
        "browser_1" : 1,
        "browser_2" : 2,
        "browser_3" : 3 },
      "keywords" : { "keyword_5" : 5,
        "keyword_4" : 4,
        "keyword_1" : 1,
        "keyword_0" : 0,
        "keyword_3" : 3,
        "keyword_2" : 2 },
      "number_of_keyword_clicks" : 205,
      "number_of_visits" : 91 },
    "306" : { "number_of_ad_clicks" : 29,
      "number_of_search_keywords" : 4,
      "total_duration" : 4,
      "os" : { "os_2" : 2,
        "os_1" : 1,
        "os_0" : 0 },
      "countries" : { "ge" : 7,
        "ca" : 7,
        "fr" : 6,
        "uk" : 1,
        "us" : 3 },
      "screen_resolutions" : { "(320, 240)" : 2,
        "(640, 480)" : 1,
        "(1024, 960)" : 9,
        "(1280, 768)" : 5 },
      "widgets" : { "widget_1" : 1,
        "widget_0" : 0 },
      "languages" : { "ua_uk" : 2,
        "ca_en" : 8,
        "ca_fr" : 5,
        "us_en" : 4 },
      "search_keywords" : { "search_keyword_1" : 1,
        "search_keyword_3" : 3,
        "search_keyword_2" : 2 },
      "number_of_pageviews" : 35,
      "browsers" : { "browser_4" : 4,
        "browser_0" : 0,
        "browser_1" : 1,
        "browser_2" : 2,
        "browser_3" : 3 },
      "keywords" : { "keyword_5" : 5,
        "keyword_4" : 4,
        "keyword_1" : 1,
        "keyword_0" : 0,
        "keyword_3" : 3,
        "keyword_2" : 2 },
      "number_of_keyword_clicks" : 18,
      "number_of_visits" : 57 },
    "408" : { "number_of_ad_clicks" : 180,
      "number_of_search_keywords" : 41,
      "total_duration" : 7,
      "os" : { "os_2" : 2,
        "os_1" : 1,
        "os_0" : 0 },
      "countries" : { "ge" : 3,
        "ca" : 6,
        "fr" : 3,
        "uk" : 9,
        "us" : 9 },
      "screen_resolutions" : { "(320, 240)" : 9,
        "(640, 480)" : 9,
        "(1024, 960)" : 5,
        "(1280, 768)" : 10 },
      "widgets" : { "widget_1" : 1,
        "widget_0" : 0 },
      "languages" : { "ua_uk" : 3,
        "ca_en" : 2,
        "ca_fr" : 10,
        "us_en" : 7 },
      "search_keywords" : { "search_keyword_37" : 37,
        "search_keyword_36" : 36,
        "search_keyword_28" : 28,
        "search_keyword_29" : 29,
        "search_keyword_24" : 24,
        "search_keyword_25" : 25,
        "search_keyword_26" : 26,
        "search_keyword_27" : 27,
        "search_keyword_20" : 20,
        "search_keyword_21" : 21,
        "search_keyword_22" : 22,
        "search_keyword_23" : 23,
        "search_keyword_39" : 39,
        "search_keyword_38" : 38,
        "search_keyword_40" : 40,
        "search_keyword_15" : 15,
        "search_keyword_14" : 14,
        "search_keyword_17" : 17,
        "search_keyword_16" : 16,
        "search_keyword_11" : 11,
        "search_keyword_10" : 10,
        "search_keyword_13" : 13,
        "search_keyword_12" : 12,
        "search_keyword_33" : 33,
        "search_keyword_32" : 32,
        "search_keyword_31" : 31,
        "search_keyword_30" : 30,
        "search_keyword_19" : 19,
        "search_keyword_18" : 18,
        "search_keyword_35" : 35,
        "search_keyword_34" : 34,
        "search_keyword_9" : 9,
        "search_keyword_8" : 8,
        "search_keyword_5" : 5,
        "search_keyword_4" : 4,
        "search_keyword_7" : 7,
        "search_keyword_6" : 6,
        "search_keyword_1" : 1,
        "search_keyword_3" : 3,
        "search_keyword_2" : 2 },
      "number_of_pageviews" : 25,
      "browsers" : { "browser_4" : 4,
        "browser_0" : 0,
        "browser_1" : 1,
        "browser_2" : 2,
        "browser_3" : 3 },
      "keywords" : { "keyword_5" : 5,
        "keyword_4" : 4,
        "keyword_1" : 1,
        "keyword_0" : 0,
        "keyword_3" : 3,
        "keyword_2" : 2 },
      "number_of_keyword_clicks" : 15,
      "number_of_visits" : 19 } },
  "site_name" : "radiotiempo.com",
  "number_of_variants" : 3 }

Here is my map reduce.

map = function(){
    emit(1, {variants:this.variants});
}

reduce = function(key, vals) {
    var returnValue = { 
      clicks: 0, 
     };
    for(var j = 0 ; j < vals.length; j++){
       for(var i = 0 ; i < variant_ids.length; i++){
          try{
             returnValue.clicks += vals[j].variants[variant_ids[i]].number_of_ad_clicks;
          }catch(err)
          {}
       }
    }
    return returnValue;
}


function emit(k, v) {
    print("emit");
    print("  k:" + k + " v:" + tojson(v));
}

res = db.variant_daily_collection.mapReduce(map, reduce, {"scope": {'variant_ids': ['4519','4518']}, "out" : "myoutput", "query":{"site_name": {'$in':['julie2.com','julie3.com']}, 'timestamp': {'$gte':'20110601','$lte':'20110603'}}})
db.myoutput.find()

The following codes is my python codes using pymongo lib.

map = Code("function () {"
                   "    emit(1, {variants:this.variants});"
                   "}")
        reduce = Code("function (key, values) {"
                      "   var result = {"
                      "       clicks: 0"
                      "   };"
                      "   for (var i = 0; i < values.length; i++) {"
                      "       for(var j = 0 ; j < variant_ids.length; j++){"
                      "           result.clicks += values[i].variants[variant_ids[j]].number_of_ad_clicks;"
                      "       }"
                      "    }"
                      "    return result;"
                      "}")

Following is error return from pymongo

In [103]:  reduce = Code("function (key, valudb.variant_daily_collection.map_reduce(map, reduce, out = 'output',full_response = True, fields = {"scope": {'variant_ids': ['398']}, "query":{"site_name": 'routeplanner.net', 'timestamp': '20110809'}} )                                                                                                                   .....: ---------------------------------------------------------------------------OperationFailure                          Traceback (most recent call last)/workspace/construction/<ipython console> in <module>()/workspace/construction/.ve/lib/python2.7/site-packages/pymongo-2.0-py2.7-macosx-10.7-intel.egg/pymongo/collection.py in map_reduce(self, map, reduce, out, merge_output, reduce_output, full_response, **kwargs)
   1031         response = self.__database.command("mapreduce", self.__name,
   1032                                            map=map, reduce=reduce,
-> 1033                                            out=out_conf, **kwargs)
   1034 
   1035         if full_response or not response.get('result'):

/workspace/construction/.ve/lib/python2.7/site-packages/pymongo-2.0-py2.7-macosx-10.7-intel.egg/pymongo/database.py in command(self, command, value, check, allowable_errors, **kwargs)
    338             msg = "command %r failed: %%s" % command
    339             helpers._check_command_response(result, self.connection.disconnect,
--> 340                                             msg, allowable_errors)
    341 
    342         return result

/workspace/construction/.ve/lib/python2.7/site-packages/pymongo-2.0-py2.7-macosx-10.7-intel.egg/pymongo/helpers.py in _check_command_response(response, reset, msg, allowable_errors)
    123                     ex_msg += (", assertionCode: %d" %
    124                                (response["assertionCode"],))
--> 125                 raise OperationFailure(ex_msg, response.get("assertionCode"))
    126             raise OperationFailure(msg % response["errmsg"])
    127 

OperationFailure: db assertion failure, assertion: 'invoke failed: JS Error: TypeError: values[i].variants[variant_ids[j]] has no properties nofile_b:0', assertionCode: 9004

In [104]: 

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

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

发布评论

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

评论(2

时光是把杀猪刀 2024-12-05 21:51:55

问题是您将“范围”作为“字段”传递。

这是可行的:

db.variant_daily_collection.map_reduce(m, r, out="myoutput", query={"site_name": {'$in':['julie2.com','julie3.com','radiotiempo.com']}, 'timestamp': {'$gte':'20110601','$lte':'20110809'}}, scope={'variant_ids': ['4519','4518']})

其中 m 是上面的映射函数,r 是上面的归约函数。我刚刚插入你的示例文档三次来测试。

The problem is you are passing 'scope' as 'fields'.

This works:

db.variant_daily_collection.map_reduce(m, r, out="myoutput", query={"site_name": {'$in':['julie2.com','julie3.com','radiotiempo.com']}, 'timestamp': {'$gte':'20110601','$lte':'20110809'}}, scope={'variant_ids': ['4519','4518']})

Where m is your map function from above and r is your reduce function above. I just inserted your example document three times to test.

叹沉浮 2024-12-05 21:51:55

除了我在上面的评论中指出的内容之外,我在这里看到了许多潜在的问题:

  1. 您在 shell 示例中重写了 emit ,这意味着您的 reduce 函数实际上不会运行(如果没有被发出,没有什么可以减少)
  2. 发出reduce函数返回的格式不同。 reduce 需要以与 emit 的第二个参数相同的格式返回值,因为 reduce 可能会对包括其自身结果在内的值调用多次(请参阅Map-Reduce 文档
  3. 您始终发出使用键 1,这可能是一个问题,具体取决于您打算如何工作
  4. 如上所述,您的代码至少在您使用的循环变量方面似乎有所不同;我还没有逐行比较它来判断是否存在任何其他差异,但最好的选择是直接将 javascript 函数定义复制到 pymongo 中的 Code

另外,如果可以的话,这也会有所帮助显示您对 map_reduce 以及 mapreduce 函数的调用。

I see a number of potential problems here, aside from what I noted in the comment above:

  1. You're overriding emit in your shell example, which means your reduce function won't actually get run (if nothing is emitted, there's nothing to reduce)
  2. You're emitting a different format than your reduce function returns. reduce needs to return values in the same format as the second argument to emit, since reduce may be called multiple times on values including its own results (see the Map-Reduce docs)
  3. You're always emitting with the key 1, which might be a problem depending on how you intend this to work
  4. As noted above, your code appears to differ at least in the loop variables you use; I haven't compared it line by line to tell if there are any other differences, but your best bet is to directly copy your javascript function definition into a Code in pymongo

Also, it would help if you could show your invocation of map_reduce as well as the map and reduce functions.

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