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

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)——跟踪调试

可以将mr中常用的代码编写成js函数,存储到mongo中,这样可以使得mr代码更简洁些,而且js函数还可以重复使用。

1. sum

#将sum功能写成函数,保存到js存储过程中。

> db.system.js.save( { _id : "Sum" ,
... value : function(key,values)
... {
... var total = 0;
... for(var i = 0; i < values.length; i++)
... total += values[i];
... return total;
... }});
#执行MR,在reduce过程中调用sum函数(注意根据sum函数,可知map中的value必须是一个数值类型,不能使文档型)。

> var map = function() {
... emit(this.name, this.dic);
... };
> var reduce = function(key, values) {
... return Sum(key, values);
... };
> var xjbu = db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result2"});
> db.tianyc_test3_result2.find()
{ "_id" : "neu", "value" : 10 }
{ "_id" : "xtt", "value" : 15 }
{ "_id" : "yct", "value" : 3 }

2. count

> db.system.js.save( { _id : "Count" ,
... value : function(key,values)
... {
... return values.length;
... }});
> var map = function() {
... emit(this.name, 1);
... };
> var reduce = function(key, values) {
... return Count(key, values);
... };
> var xjbu = db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result2"});
> db.tianyc_test3_result2.find()
{ "_id" : "neu", "value" : 4 }
{ "_id" : "xtt", "value" : 5 }
{ "_id" : "yct", "value" : 2 }

3. avg

> db.system.js.save( { _id : "Avg" ,
... value : function(key,values)
... {
... return Sum(key, values)/values.length;
... }});
> var map = function() {
... emit(this.name, this.dic);
... };
> var reduce = function(key, values) {
... return Avg(key, values);
... };
> db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result2"});
{
"result" : "tianyc_test3_result2",
"timeMillis" : 9,
"counts" : {
"input" : 11,
"emit" : 11,
"reduce" : 3,
"output" : 3
},
"ok" : 1,
}
> db.tianyc_test3_result2.find()
{ "_id" : "neu", "value" : 2.5 }
{ "_id" : "xtt", "value" : 3 }
{ "_id" : "yct", "value" : 1.5 }

4. max

> db.system.js.save( { _id : "Max" ,
... value : function(key,values)
... {
... var result=values[0];
... for (var i=0;i<values.length;i++){
... if (result<values[i]) result=values[i];
... }
... return result;
... }});
> var map = function() {
... emit(this.name, this.dic);
... };
> var reduce = function(key, values) {
... return Max(key, values);
... };
> db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result2"});
{
"result" : "tianyc_test3_result2",
"timeMillis" : 10,
"counts" : {
"input" : 11,
"emit" : 11,
"reduce" : 3,
"output" : 3
},
"ok" : 1,
}
> db.tianyc_test3_result2.find()
{ "_id" : "neu", "value" : 4 }
{ "_id" : "xtt", "value" : 5 }
{ "_id" : "yct", "value" : 2 }

5. min

> db.system.js.save( { _id : "Min" ,
... value : function(key,values)
... {
... var result=values[0];
... for (var i=0;i<values.length;i++){
... if (result>values[i]) result=values[i];
... }
... return result;
... }});
> var map = function() {
... emit(this.name, this.dic);
... };
> var reduce = function(key, values) {
... return Min(key, values);
... };
> db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result2"});
{
"result" : "tianyc_test3_result2",
"timeMillis" : 10,
"counts" : {
"input" : 11,
"emit" : 11,
"reduce" : 3,
"output" : 3
},
"ok" : 1,
}
> db.tianyc_test3_result2.find()
{ "_id" : "neu", "value" : 1 }
{ "_id" : "xtt", "value" : 1 }
{ "_id" : "yct", "value" : 1 }
>

当前共写了5个小函数:

> db.system.js.find({},{_id:1})
{ "_id" : "Sum" }
{ "_id" : "Count" }
{ "_id" : "Avg" }
{ "_id" : "Max" }
{ "_id" : "Min" }

参考这里

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