这是我的 Map、Reduce 和 finalize 函数。有 4 条记录与键匹配,当使用较少的数据(100 秒)调用 mapreduce 时结果正确,但当使用更多数据(少数 1000 秒)调用 mapreduce 时,计数始终为 2。我检查了 Reduce 函数,对我来说它似乎是正确的,即使它在内部被多次调用以获取更大的数据。这越来越奇怪,我花了很长时间,但仍然无法正确处理。
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){});