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

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

1. 列转行

> var map = function() {
... emit(this.name, this.dic);
... };
>
> var reduce = function(key, values) {
... return {dics:values};
... };
> db.tianyc_test3.mapReduce(map, reduce, {out: "tianyc_test3_result"});
{
"result" : "tianyc_test3_result",
"timeMillis" : 12,
"counts" : {
"input" : 11,
"emit" : 11,
"reduce" : 3,
"output" : 3
},
"ok" : 1,
}
> db.tianyc_test3_result.find()
{ "_id" : "neu", "value" : { "dics" : [ 1, 2, 3, 4 ] } }
{ "_id" : "xtt", "value" : { "dics" : [ 1, 2, 3, 4, 5 ] } }
{ "_id" : "yct", "value" : { "dics" : [ 1, 2 ] } }

2. 列转行的另一种执行方式:使用runCommand。

> db.tianyc_test3_result.remove()
> db.runCommand({
... mapreduce:'tianyc_test3',
... map:map,
... reduce:reduce,
... out:'tianyc_test3_result'
... }
... )
{
"result" : "tianyc_test3_result",
"timeMillis" : 12,
"counts" : {
"input" : 11,
"emit" : 11,
"reduce" : 3,
"output" : 3
},
"ok" : 1
}
> db.tianyc_test3_result.find()
{ "_id" : "neu", "value" : { "dics" : [ 1, 2, 3, 4 ] } }
{ "_id" : "xtt", "value" : { "dics" : [ 1, 2, 3, 4, 5 ] } }
{ "_id" : "yct", "value" : { "dics" : [ 1, 2 ] } }

3. 列转行时使用finalize参数

使用finalize参数,还可以对上面reduce后的结果进行再次处理。比如我们将_id为'neu'的行加上属性msg1:"It's not a name"。

> var final = function(key, reduce_values){
... if (key=='neu')
... reduce_values.msg1="It's not a name";
... return reduce_values;
... }
> db.runCommand({
... mapreduce:'tianyc_test3',
... map:map,
... reduce:reduce,
... finalize:final,
... out:'tianyc_test3_result'
... }
... )
{
"result" : "tianyc_test3_result",
"timeMillis" : 13,
"counts" : {
"input" : 11,
"emit" : 11,
"reduce" : 3,
"output" : 3
},
"ok" : 1
}
> db.tianyc_test3_result.find()
{ "_id" : "neu", "value" : { "dics" : [ 1, 2, 3, 4 ], "msg1" : "It's not a name" } }
{ "_id" : "xtt", "value" : { "dics" : [ 1, 2, 3, 4, 5 ] } }
{ "_id" : "yct", "value" : { "dics" : [ 1, 2 ] } }

参考该贴

posted @ 2013-02-21 11:51  醇酒醉影  阅读(3236)  评论(0编辑  收藏  举报