mongodb mapreduce - 缺少数据

时间:2013-02-25 03:58:13

标签: mongodb mapreduce

这是我的Map,Reduce和finalize功能。有4条匹配键的记录,当使用较少的数据(100s)调用mapreduce时结果正确,但是当使用更多数据(少量1000s)调用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){});

1 个答案:

答案 0 :(得分:1)

没有文档来测试它我猜了一下,但我可以看到几个问题:

  1. 发出/减少的值不应包括关键字段。因此,您的发布值应该是这样的:{ posCnt: mapPosCnt, posSum: mapPosSum, zeroCnt: mapZeroCnt }
  2. reduce函数不应该像你正在做的那样尝试重新应用emit逻辑,而应该通过对它们的值进行求和来聚合使用相同键的值。
  3. 所以reduce1应该是这样的:

    var reduce1 = function(key, values){
        var retval = { posCnt: 0, posSum: 0, zeroCnt: 0 };
        values.forEach(function(value){
            retval.posCnt += value.posCnt;
            retval.posSum += value.posSum;
            retval.zeroCnt += value.zeroCnt;
        });
        return retval;
    };