mongoDB(3) mapReduce

mapReduce是大数据的核心内容,但实际操作中别用这个,所谓的mapReduce分两步

  1.map:将数据分别取出,Map函数调用emit(key,value)遍历集合中所有的记录,将key与value传给Reduce函数进行处理

  2.reduce:负责数据的最后处理,function(key,value){} 参数是map传来的key和value

Mongodb中的Map/reduce主要是用来对数据进行批量处理和聚合操作,有点类似于使用Hadoop对集合数据进行处理,所有输入数据都是从集合中获取,而MapReduce后输出的数据也都会写入到集合中。通常类似于我们在SQL中使用Group By语句一样。

在mongodb中实现mapReduce是复杂度相当高的。

Map函数和Reduce函数是使用Javascript编写的,并可以通过db.runCommand或mapreduce命令来执行MapReduce操作.

db.runCommand(
 { mapreduce : <collection>,
   map : <mapfunction>,
   reduce : <reducefunction>,
   out : <see output options below>
   [, query : <query filter object>]
   [, sort : <sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces>]
   [, limit : <number of objects to return from collection, not supported with sharding>]
   [, keeptemp: <true|false>]
   [, finalize : <finalizefunction>]
   [, scope : <object where fields go into javascript global scope >]
   [, jsMode : true]
   [, verbose : true]
 }
);

参数讲解:

  mapreduce:要操作的目标集合

  map:映射函数(生成键值对序列,作为Reduce函数的参数) 

  reduce:统计函数

  query:目标记录过滤

  sort:对目标记录排序

  limit:限制目标记录数量

  out:统计结果存放集合(如果不指定则使用临时集合,在客户端断开后自动删除)

  keeptemp:是否保留临时集合

  finalize:最终处理函数(对reduce返回结果执行最终整理后存入结果集合)

  scope:向map、reduce、finalize导入外部变量

  verbose:显示详细的时间统计信息

/*
* 1.map:定义好分组的条件,以及每个集合要取出来的内容
* emit传给了reduce{key,[value1,value2,value3,……]}
* 2.{sex:'sex'+ this.sex,name:'333'+this.name} 取出来的所有的内容,都给了values [{},{},{},{},{}]
* 3.reducekey,values,
* key:是emit的第一个值,{sex:this.sex,name:this.name}
* values: emit的第二个组成的数组
* */

db.runCommand({
mapReduce:'logs',
map:function(){
emit({sex:this.sex,name:this.name}, {sex:'sex'+ this.sex,age:3+this.age});
},

reduce:function(key,values){
var x = 0;
values.forEach(function (v) {x += v.age;});
return {key:key,x:x};
},
out:"students_result"
});
/*
{ "_id" : { "sex" : "女", "name" : 0 }, "value" : { "sex" : "sex女", "age" : 23 } }
{ "_id" : { "sex" : "女", "name" : 1 }, "value" : { "sex" : "sex女", "age" : 24 } }
{ "_id" : { "sex" : "女", "name" : 2 }, "value" : { "sex" : "sex女", "age" : 25 } }
{ "_id" : { "sex" : "女", "name" : 3 }, "value" : { "sex" : "sex女", "age" : 26 } }
{ "_id" : { "sex" : "女", "name" : 4 }, "value" : { "sex" : "sex女", "age" : 27 } }
{ "_id" : { "sex" : "女", "name" : 5 }, "value" : { "sex" : "sex女", "age" : 28 } }
{ "_id" : { "sex" : "女", "name" : 6 }, "value" : { "sex" : "sex女", "age" : 29 } }
{ "_id" : { "sex" : "女", "name" : 7 }, "value" : { "sex" : "sex女", "age" : 30 } }
{ "_id" : { "sex" : "女", "name" : 8 }, "value" : { "sex" : "sex女", "age" : 31 } }
{ "_id" : { "sex" : "女", "name" : 9 }, "value" : { "sex" : "sex女", "age" : 32 } }
*/

/*
* key:是emit的第一个值,this.sex
* values: emit的第二个组成的数组
* */

db.runCommand({
mapReduce:'logs',
map:function(){
emit(this.sex, {sex:'sex'+ this.sex,age:3+this.age});
},

reduce:function(key,values){
var x = 0;
values.forEach(function (v) {x += v.age;});
return {key:key,x:x};
},
out:"students_result"
});

//{ "_id" : "女", "value" : { "key" : "女", "x" : 275 } }


 
 

-

      

原文地址:https://www.cnblogs.com/jay--zhang/p/5990940.html