Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapReduce problem

Tags:

mongodb

I have a strange MapReduce problem.

Map function:

> mp
function () {
    emit(this.ContractID, {qty:this.Qty, qtybs:this.QtyBs});
}

Reduce function

> red
function (key, values) {
    var sum1 = 0, sum2 = 0;
    values.forEach(function (doc) {sum1 += doc.qty;sum2 += doc.qtybs;});
    return {a:sum1, b:sum2};
}

Run MR for 7 contracts:

> result = db.fact_payments.mapReduce(mp, red, {out:"myout2", query:{ContractID:{$lte:10000100042}}});
{
        "result" : "myout2",
        "timeMillis" : 670,
        "counts" : {
                "input" : 591,
                "emit" : 591,
                "output" : 7
        },
        "ok" : 1,
}
> db.myout2.find()
{ "_id" : NumberLong("10000000042"), "value" : { "a" : 8331.04, "b" : 253835.07999999996 } }
{ "_id" : NumberLong("10000000084"), "value" : { "a" : 4728.480000000001, "b" : 142879.88000000003 } }
{ "_id" : NumberLong("10000000129"), "value" : { "a" : 25421.859999999997, "b" : 756036.9499999998 } }
{ "_id" : NumberLong("10000000140"), "value" : { "a" : 477292.0000000002, "b" : 477292.0000000002 } }
{ "_id" : NumberLong("10000000148"), "value" : { "a" : 7912.0599999999995, "b" : 237926.87999999998 } }
{ "_id" : NumberLong("10000000165"), "value" : { "a" : 35391.31999999999, "b" : 1074180.95 } }
{ "_id" : NumberLong("10000000171"), "value" : { "a" : 62189.52, "b" : 62189.52 } }
>

All it's ok, all contracts has results :)

Run MR on all contracts:

> result = db.fact_payments.mapReduce(mp, red, {out:"myout2", query:{ContractID:{$lte:100100000042}}});
{
        "result" : "myout2",
        "timeMillis" : 26273,
        "counts" : {
                "input" : 295765,
                "emit" : 295765,
                "output" : 7793
        },
        "ok" : 1,
}
> db.myout2.find()
{ "_id" : NumberLong("10000000042"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10000000084"), "value" : { "a" : 4728.480000000001, "b" : 142879.88000000003 } }
{ "_id" : NumberLong("10000000129"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10000000140"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10000000148"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10000000165"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10000000171"), "value" : { "a" : 62189.52, "b" : 62189.52 } }
{ "_id" : NumberLong("10005000172"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10005000173"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10005000189"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10005000191"), "value" : { "a" : 8261.759999999998, "b" : 253916.7 } }
{ "_id" : NumberLong("10005000199"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10005000206"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10005000213"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10010000200"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10010000224"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10010000229"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10010000240"), "value" : { "a" : 32843.32, "b" : 32843.32 } }
{ "_id" : NumberLong("10010000243"), "value" : { "a" : NaN, "b" : NaN } }
{ "_id" : NumberLong("10010000244"), "value" : { "a" : NaN, "b" : NaN } }
has more

Same contract id's with different results:

{ "_id" : NumberLong("10000000042"), "value" : { "a" : 8331.04, "b" : 253835.07999999996 } }

and

{ "_id" : NumberLong("10000000042"), "value" : { "a" : NaN, "b" : NaN } }

I don't have any ideas at all.

like image 911
Igorekk Avatar asked Dec 22 '22 09:12

Igorekk


1 Answers

If you change the last line to the following it should work:

return {qty:sum1, qtybs:sum2};

The rule is that the return value of the reduce function must be the same "shape" as the second argument to emit (which is the input to reduce) as the output of reduce is fed back into the reduce function. See http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-ReduceFunction for more details.

like image 171
mstearn Avatar answered Jan 16 '23 00:01

mstearn