mongo-mapreduce测试(4)——avg

mongo-mapreduce测试(1)——count/sum/where条件

mongo-mapreduce测试(2)——列转行与finalize函数

mongo-mapreduce测试(3)——group by having

mongo-mapreduce测试(4)——avg

mongo-mapreduce测试(5)——max/min

mongo-mapreduce测试(6)——综合测试

mongo-mapreduce测试(7)——使用js存储过程

mongo-mapreduce测试(8)——阶段总结(1)

mongo-mapreduce测试(9)——python调用

mongo-mapreduce测试(10)——阶段总结(2)

mongo-mapreduce测试(11)——跟踪调试

测试mapreduce的avg聚合函数的实现,类似如下SQL:

select name, avg(dic)
from tianyc_test3
group by name;

1. 测试数据如下:

> db.tianyc_test3.find()
{ "_id" : ObjectId("51258e47ff13bc6383473137"), "name" : "xtt", "dic" : 1 }
{ "_id" : ObjectId("51258e49ff13bc6383473138"), "name" : "xtt", "dic" : 2 }
{ "_id" : ObjectId("51258e4bff13bc6383473139"), "name" : "xtt", "dic" : 3 }
{ "_id" : ObjectId("51258e4eff13bc638347313a"), "name" : "xtt", "dic" : 4 }
{ "_id" : ObjectId("51258e50ff13bc638347313b"), "name" : "xtt", "dic" : 5 }
{ "_id" : ObjectId("51258e5aff13bc638347313c"), "name" : "yct", "dic" : 1 }
{ "_id" : ObjectId("51258e5cff13bc638347313d"), "name" : "yct", "dic" : 2 }
{ "_id" : ObjectId("51258e62ff13bc638347313e"), "name" : "neu", "dic" : 1 }
{ "_id" : ObjectId("51258e64ff13bc638347313f"), "name" : "neu", "dic" : 2 }
{ "_id" : ObjectId("51258e65ff13bc6383473140"), "name" : "neu", "dic" : 3 }
{ "_id" : ObjectId("51258e68ff13bc6383473141"), "name" : "neu", "dic" : 4 }

2. 编写map函数

> var m = function(){
... emit(this.name,{dic:this.dic});
... }

3. 编写reduce函数

> var r = function (key,values) {
... var sum = 0;
... for (var i=0; i<values.length; i++) {
... sum += values[i].dic;
... }
... return {name:key, avg: sum/values.length};
... };

4. 执行mapreduce
> var res = db.runCommand({
... mapreduce:'tianyc_test3',
... map:m,
... reduce:r,
... out:'tianyc_mr_Result1'
... }
... )

5. 查询结果

> db.tianyc_mr_Result1.find()
{ "_id" : "neu", "value" : { "name" : "neu", "avg" : 2.5 } }
{ "_id" : "xtt", "value" : { "name" : "xtt", "avg" : 3 } }
{ "_id" : "yct", "value" : { "name" : "yct", "avg" : 1.5 } }
>

posted @ 2013-02-22 15:44  醇酒醉影  阅读(1004)  评论(0编辑  收藏  举报