![](/img/trans.png)
[英]MongoDB ignores readPreference while running MapReduce on a sharded cluster?
[英]mongodb mapreduce doesn't return right in a sharded cluster
非常有趣,mapreduce 在單個實例中運行良好,但不適用於分片集合。 如下所示,您可能會看到我得到了一個集合並編寫了一個簡單的 map-reduce 函數,
mongos> db.tweets.findOne()
{
"_id" : ObjectId("5359771dbfe1a02a8cf1c906"),
"geometry" : {
"type" : "Point",
"coordinates" : [
131.71778292855996,
0.21856835860911106
]
},
"type" : "Feature",
"properties" : {
"isflu" : 1,
"cell_id" : 60079,
"user_id" : 35,
"time" : ISODate("2014-04-24T15:42:05.048Z")
}
}
mongos> db.tweets.find({"properties.user_id":35}).count()
44247
mongos> map_flow
function () { var key=this.properties.user_id; var value={ "cell_id":1}; emit(key,value); }
mongos> reduce2
function (key,values){ var ros={flows:[]}; values.forEach(function(v){ros.flows.push(v.cell_id);});return ros;}
mongos> db.tweets.mapReduce(map_flow,reduce2, { out:"flows2", sort:{"properties.user_id":1,"properties.time":1} })
但結果不是我想要的
mongos> db.flows2.find({"_id":35})
{ "_id" : 35, "value" : { "flows" : [ null, null, null ] } }
我有很多 null 和有趣的都有三個。 mongodb mapreduce 在分片集合上似乎不對?
MapReduce 的第一條規則是:
您違反了這條規則,因此您的 MapReduce 僅適用於小型集合,其中每個鍵只調用一次 reduce(這是 MapReduce 的第二條規則——reduce 函數可能被調用零次、一次或多次)。
您的 map 函數為每個文檔發出這個值{cell_id:1}
。
你的reduce函數如何使用這個值? 好吧,您返回一個值,該值是一個帶有數組的文檔,您將cell_id
值推入其中。 這已經很奇怪了,因為那個值是 1,所以我不確定你為什么不只發出 1(如果你想計數的話)。
但是看看當多個分片將一堆 1 推入這個流數組時會發生什么(無論這是你想要的,這就是你的代碼正在做的)並且現在 reduce 在幾個已經減少的值上被調用:
reduce(key, [ {flows:[1,1,1,1]},{flows:[1,1,1,1,1,1,1,1,1]}, etc ] )
您的reduce 函數現在嘗試獲取values 數組的每個成員(這是一個具有單個字段flows
的文檔),然后將v.cell_id
推v.cell_id
您的flows 數組。 這里沒有 cell_id 字段,所以你當然會得到null
。 三個空值可能是因為您有三個分片?
我建議您向自己闡明您在這段代碼中究竟想聚合什么,然后重寫您的映射和化簡以符合 MongoDB 中的 mapReduce 期望您的代碼遵循的規則。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.