2011-09-07 109 views
4

我有一個奇怪的MapReduce問題。MapReduce問題

地圖功能:

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

Reduce函數

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

運行MR 7個合同:

> 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 } } 
> 

所有它的確定,所有的合同都有結果:)

在所有合同上運行MR S:

> 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 

同一合同ID與不同的結果:

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

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

我沒有任何想法都沒有。

回答

11

如果您更改最後一行以下它應該工作:

return {qty:sum1, qtybs:sum2}; 

的規則是,減少函數的返回值必須是相同的「形」作爲第二個參數發射(其是減少的輸入),因爲reduce的輸出被反饋到reduce函數中。有關更多詳情,請參閱http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-ReduceFunction

+0

哇,非常感謝。我錯過了這條規則。 – Igorekk