我有一個奇怪的情況與地圖減少。結果並不考慮所有記錄,儘管它應該。Mongodb mapreduce丟失文件
我收集了如下所示的推文。我有230個文件 - 我的查詢是在創建年份。這裏是一個樣本
{
"_id" : ObjectId("56e55b52330dfb156547d559"),
"message" : "RT @TwitFAKTA: Kiper MU, David De Gea mempunyai ritual unik sebelum bertanding, yaitu memutar lagu-lagu Metallica dengan keras.",
"createdyear" : "2016",
"handle" : "xxx",
"createdtime" : "13:23:33",
"searchtopic" : "Metallica",
"createdmonth" : "03",
"createddate" : "2016-03-13",
"user" : "xxx"
}
我的地圖功能是這樣的。非常簡單:最終結果應該是每個主題和每月的推文數量。
function(){
emit({topic: this.searchtopic, month: this.createdmonth},1)
};
和這裏的reduce函數:我只是計算給定鍵值的數量。
function(key,value) {
var counter=0;
for (var i=0;i<value.length;i++) {
counter = counter +1;
}
return counter;
};
然後我映射並將輸出存儲在集合中。
db.tweets.mapReduce(map,reduce,{out: "mapreduce_test"})
的結果是這樣的:
{
"result" : "mapreduce_test",
"timeMillis" : 6,
"counts" : {
"input" : 230,
"emit" : 230,
"reduce" : 4,
"output" : 2
},
"ok" : 1
}
地圖上減少工作,但結果是不正確的。當我列出從MapReduce的輸出我得到如下:
{ "_id" : { "topic" : "3 Doors Down", "month" : "03" }, "value" : 2 }
{ "_id" : { "topic" : "Metallica", "month" : "03" }, "value" : 31 }
當我手動搜索的文件,我得到228 Metallica的和2個3門下來。這些是230個輸入和發出的記錄。
那麼其他文件在哪裏?發生了什麼?
通常我有一個從Twitter獲取推文並將它們存儲在mongodb中的過程。所以收藏總是越來越大。當我通過cron定期運行mapreduce任務時,我注意到,它工作了一段時間,然後突然出現錯誤的結果。看看:
Sun Mar 13 14:30:02 CET 2016
running mapreduce for topic: Metallica
{"name": "Metallica","data":[0, 0, 47.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
running mapreduce for topic: 3 Doors Down
{"name": "3 Doors Down","data":[0, 0, 2.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
writing output file: /home/uwe/development/highcharts/highcharts_tweets.html
Sun Mar 13 14:40:02 CET 2016
running mapreduce for topic: Metallica
{"name": "Metallica","data":[0, 0, 67.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
running mapreduce for topic: 3 Doors Down
{"name": "3 Doors Down","data":[0, 0, 2.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
writing output file: /home/uwe/development/highcharts/highcharts_tweets.html
Sun Mar 13 14:50:02 CET 2016
running mapreduce for topic: Metallica
{"name": "Metallica","data":[0, 0, 87.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
running mapreduce for topic: 3 Doors Down
{"name": "3 Doors Down","data":[0, 0, 2.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
writing output file: /home/uwe/development/highcharts/highcharts_tweets.html
Sun Mar 13 15:00:02 CET 2016
running mapreduce for topic: Metallica
{"name": "Metallica","data":[0, 0, 7.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
running mapreduce for topic: 3 Doors Down
{"name": "3 Doors Down","data":[0, 0, 2.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
writing output file: /home/uwe/development/highcharts/highcharts_tweets.html
文件的數量正在增加,然後突然在15:00下降。雖然我仍然有數據庫中的文件 - 我多次檢查它。
我也在第二臺機器上運行它,但結果相同。
有沒有人對此行爲有解釋?
感謝,
烏韋
謝謝Joachim。嗯。我必須考慮這個。我假設,我總是從價值[i]中迴歸1。但這可能是錯誤的。 – uwegeercken
這是一個快速回復和正確的解決方案。而且你甚至正確地格式化了我的文本(我多年來沒有使用過stackoverflow)。 – uwegeercken
@uwegeercken如果答案有幫助,您可能需要考慮對其進行提升和/或將其標記爲回答您的問題。這將有助於其他類似問題的人更容易地找到解決方案。 –