MapReduce问题 [英] MapReduce problem
问题描述
我有一个奇怪的MapReduce问题.
I have a strange MapReduce problem.
地图功能:
> mp
function () {
emit(this.ContractID, {qty:this.Qty, qtybs:this.QtyBs});
}
减少功能
> red
function (key, values) {
var sum1 = 0, sum2 = 0;
values.forEach(function (doc) {sum1 += doc.qty;sum2 += doc.qtybs;});
return {a:sum1, b:sum2};
}
为7个合同运行MR:
> 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 :)
对所有合同运行MR:
> 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 } }
和
{ "_id" : NumberLong("10000000042"), "value" : { "a" : NaN, "b" : NaN } }
我什么都没有.
推荐答案
如果将最后一行更改为以下内容,则应该可以:
If you change the last line to the following it should work:
return {qty:sum1, qtybs:sum2};
规则是,将reduce的输出反馈到reduce函数中时,reduce函数的返回值必须与要发出的第二个参数(要减少的输入)具有相同的形状".参见 http://www.mongodb.org/display/DOCS/MapReduce#MapReduce- ReduceFunction 以获得更多详细信息.
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.
这篇关于MapReduce问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!