1
這裏是我的地圖,減少和最終確定的功能。有4條匹配的密鑰和結果來正確時,MapReduce的是用更少的數據(100S)調用,但constanly得到數爲2時的MapReduce與更多的數據(1000少)調用。我檢查了Reduce函數,對我來說它似乎是正確的,即使它在內部被多次調用大數據。這是越來越奇怪,我花了很長時間,仍然不能正確。MongoDB中的MapReduce - 丟失的數據
var map1 = function(){
var mapPosCnt = 0, mapPosSum = 0, mapZeroCnt = 0;
if (isNumber(this.val1)){
if(this.val1.toPrecision(10) > 0.0000000000){
mapPosCnt = 1;
mapPosSum = this.val1;
}else{
mapZeroCnt = 1;
}
}else{
mapPosCnt = 0, mapPosSum = 0, mapZeroCnt = 0;
}
emit({key1: this.key1, key2: this.key2+'', val1: 'val1'}
,{key1: this.key1, key2: this.key2+'', posCnt: mapPosCnt, posSum: mapPosSum, posAvg: 0, zeroCnt: mapZeroCnt, val1: this.val1});
}
var reduce1 = function(key, values){
var retval = {key1: key.CE, key2: key.key2, posCnt: 0, posSum: 0, posAvg: 0, zeroCnt: 0, val1: 0};
values.forEach(function(value){
if (isNumber(value.val1)){
if(value.val1.toPrecision(10) > 0.0000000000){
retval.posCnt += 1;
retval.posSum += value.val1;
}else{
retval.zeroCnt += 1;
}
}
})
return retval;
}
var finalize1 = function(key, value){
value.key2 = value.key2.toString();
if(value.posCnt > 0){
value.posSum = Math.round(value.posSum * Math.pow(10, 6))/Math.pow(10, 6);
value.posAvg = Math.round((value.posSum/value.posCnt) * Math.pow(10, 6))/Math.pow(10, 6);
}
return value;
}
collection1.mapReduce(map1, reduce1, {out: {merge: 'collection2'}, finalize: finalize1}, function(err, collection){});
非常感謝約翰尼;修復您提到的兩個問題,解決了我面臨的隨機丟失的數據問題。很抱歉,由於我整個星期都被拖入不同的任務,並且只是在這方面做了工作。 – user1549605 2013-03-01 23:02:34