mongo-mapreduce测试(5)——max/min
mongo-mapreduce测试(1)——count/sum/where条件
mongo-mapreduce测试(2)——列转行与finalize函数
mongo-mapreduce测试(3)——group by having
mongo-mapreduce测试(7)——使用js存储过程
mongo-mapreduce测试(9)——python调用
mongo-mapreduce测试(10)——阶段总结(2)
测试mapreduce的avg聚合函数的实现,类似如下SQL:
select name, max(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 max = values[0].dic;
... var tmp;
... for (var i=1; i<values.length; i++) {
... tmp = values[i].dic;
... if (max<tmp) {max=tmp;}
... }
... return {name:key, max : max};
... };
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", "max" : 4 } }
{ "_id" : "xtt", "value" : { "name" : "xtt", "max" : 5 } }
{ "_id" : "yct", "value" : { "name" : "yct", "max" : 2 } }
>
若使用min,则只需要修改reduce函数:
> var r = function (key,values) {
... var min = values[0].dic;
... var tmp;
... for (var i=1; i<values.length; i++) {
... tmp = values[i].dic;
... if (min>tmp) {min=tmp;}
... }
... return {name:key, min : min};
... };
>
> var res = db.runCommand({
... mapreduce:'tianyc_test3',
... map:m,
... reduce:r,
... out:'tianyc_mr_Result1'
... }
... )
> db.tianyc_mr_Result1.find()
{ "_id" : "neu", "value" : { "name" : "neu", "min" : 1 } }
{ "_id" : "xtt", "value" : { "name" : "xtt", "min" : 1 } }
{ "_id" : "yct", "value" : { "name" : "yct", "min" : 1 } }