MongoDB中的MapReduce介绍与使用
一、简介
在用MongoDB查询返回的数据量很大的情况下,做一些比较复杂的统计和聚合操作做花费的时间很长的时候,可以用MongoDB中的MapReduce进行实现
MapReduce是个非常灵活和强大的数据聚合工具。它的好处是可以把一个聚合任务分解为多个小的任务,分配到多服务器上并行处理。MongoDB也提供了MapReduce,当然查询语肯定是JavaScript。
MongoDB中的MapReduce主要有以下几阶段:
Map:把一个操作Map到集合中的每一个文档
Shuffle: 根据Key分组对文档,并且为每个不同的Key生成一系列(>=1个)的值表(List of values)。
Reduce: 处理值表中的元素,直到值表中只有一个元素。然后将值表返回到Shuffle过程,循环处理,直到每个Key只对应一个值表,并且此值表中只有一个元素,这就是MR的结果。
Finalize:此步骤不是必须的。在得到MR最终结果后,再进行一些数据“修剪”性质的处理。
MongoDB中使用emit函数向MapReduce提供Key/Value对。
Reduce函数接受两个参数:Key,emits. Key即为emit函数中的Key。 emits是一个数组,它的元素就是emit函数提供的Value。
Reduce函数的返回结果必须要能被Map或者Reduce重复使用,所以返回结果必须与emits中元素结构一致。
Map或者Reduce函数中的this关键字,代表当前被Mapping文档。
二、介绍
db.runCommand({
mapreduce:<collection>,
map:<mapfunction>,
reduce:<reducefunction>,
[,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>]
[,out:<see output options below>]
[,keeptemp:<true|false>]
[,finalize:<finalizefunction>]
[,scope:<object where fields go into javascript global scope>]
[, jsMode : boolean,default true]
[,verbose:true]
});
参数说明:
Mapreduce:要操作的目标集合
Map:映射函数(生成键值对序列,作为reduce函数参数)
Reduce:统计函数
Query:目标记录过滤
Sort:目标记录排序
Limit:限制目标记录数量
Out:统计结果存放集合(不指定使用临时集合,在客户端断开后自动删除)
Keeptemp:是否保留临时集合
Finalize:最终处理函数(对reduce返回结果进行最终整理后存入结果集合)
Scope:向map、reduce、finalize导入外部变量
jsMode说明:为false时 BSON-->JS-->map-->BSON-->JS-->reduce-->BSON,可处理非常大的mapreduce,为true时 BSON-->js-->map-->reduce-->BSON
Verbose:显示详细的时间统计信息
行查询的步骤
MapReduce对指定的集合Collection进行查询
对A的结果集进行mapper方法采集
对B的结果执行finalize方法处理
最终结果集输出到临时Collection中
断开连接,临时Collection删除或保留
需要注意的
以下是来自文档的图,可以清楚的说明 Map-Reduce 的执行过程。
In this map-reduce operation, MongoDB applies the map phase to each input document (i.e. the documents in the collection that match the query condition). The map function emits key-value pairs. For those keys that have multiple values, MongoDB applies the reduce phase, which collects and condenses the aggregated data. MongoDB then stores the results in a collection. Optionally, the output of the reduce function may pass through a finalize function to further condense or process the results of the aggregation.
All map-reduce functions in MongoDB are JavaScript and run within the
mongod
process. Map-reduce operations take the documents of a single collection as the input and can perform any arbitrary sorting and limiting before beginning the map stage.mapReduce
can return the results of a map-reduce operation as a document, or may write the results to collections. The input and the output collections may be sharded.
NOTE
For most aggregation operations, the Aggregation Pipeline provides better performance and more coherent interface. However, map-reduce operations provide some flexibility that is not presently available in the aggregation pipeline.
Map-Reduce 的执行过程是先 map
然后 reduce
么?仔细再看一遍上文的图,不是每次 map
都有 reduce
的!如果 map
的结果不是数组,mongodb 就不会执行 reduce
。很合理的处理逻辑。
对于 map
到的数据,如果在 reduce
时希望做统一的处理,一定会发现数据结果是不完整的。
三、查询分析
测试数据:
> db.test.find()
{ "_id" : ObjectId("5a1d45ab893253f4d2e4bf91"), "name" : "yy1", "age" : "22" }
{ "_id" : ObjectId("5a1d45b1893253f4d2e4bf92"), "name" : "yy2", "age" : "23" }
{ "_id" : ObjectId("5a1d45c5893253f4d2e4bf93"), "name" : "yy3", "age" : "24" }
{ "_id" : ObjectId("5a1d45d4893253f4d2e4bf94"), "name" : "yy5", "age" : "25" }
{ "_id" : ObjectId("5a1d45f7893253f4d2e4bf95"), "name" : "yy6", "age" : "26" }
{ "_id" : ObjectId("5a1d45ff893253f4d2e4bf96"), "name" : "yy4", "age" : "25" }
1、查询年龄大于23岁的
map:
var m = function(){if(this.age > 23) emit(this.age,{name:this.name})};
reduce:
var r = function(key,values){return JSON.stringify(values);}
或者:
var r = function(key,values){ var ret={names:values};return ret;}
生成结果集:
var res = db.runCommand({mapreduce:"test",map:m,reduce:r,out:"emp_res"})
查询:
> db.emp_res.find()
{ "_id" : "24", "value" : { "name" : "yy3" } }
{ "_id" : "25", "value" : "[{\"name\":\"yy5\"},{\"name\":\"yy4\"}]" }
{ "_id" : "26", "value" : { "name" : "yy6" } }
或者:
> db.emp_res.find()
{ "_id" : "24", "value" : { "name" : "yy3" } }
{ "_id" : "25", "value" : { "names" : [ { "name" : "yy5" }, { "name" : "yy4" } ] } }
{ "_id" : "26", "value" : { "name" : "yy6" } }
最后,还可以编写finalize函数对reduce的返回值做最后处理:
var f=function(key,rval){ if(key==24){ rval.msg="do somethings";} return rval }
生成结果集:
> var f=function(key,rval){ if(key==24){ rval.msg="do somethings";} return rval }
> db.emp_res.find()
{ "_id" : "24", "value" : { "name" : "yy3", "msg" : "do somethings" } }
{ "_id" : "25", "value" : { "names" : [ { "name" : "yy5" }, { "name" : "yy4" } ] } }
{ "_id" : "26", "value" : { "name" : "yy6" } }
>
2、过滤出来age=25的
方法1:
> var m = function(){ emit(this.age,{name:this.name})};
> var r = function(key,values){ var ret={names:values};return ret;}
> var res = db.runCommand({mapreduce:"test",map:m,reduce:r,finalize:f,query:{age:"25"},out:"emp_res"})
> db.emp_res.find()
{ "_id" : "25", "value" : { "names" : [ { "name" : "yy5" }, { "name" : "yy4" } ] } }
方法2:
> var m = function(){ emit(this.age,{p:[this.name]})};
> var r = function(key, values) {
var ret = {p:[]};
for(var i = 0; i < values.length; i++){
ret.p.push(values[i].p[0]);
}
return ret;
};
> var res = db.runCommand({mapreduce:"test",map:m,reduce:r,finalize:f,query:{age:"25"},out:"emp_res"})
方法3:
> var m = function() {
emit(this.age, {name:[this.name]});
};
> var r = func tion(key, values) {
var ret = {locs:[]}
for(var i = 0; i < values.length; i++){
ret.locs.push(values[i].locs[0]);
}
return ret;
};
> var res = db.runCommand({mapreduce:"test",map:map,reduce:reduce,finalize:f,query:{age:"25"},out:"emp_res"})
这个过程中遇到很多坑,需要多练习,多debug
参考
MongoDB中的MapReduce介绍与使用的更多相关文章
- 【转载】MongoDB中的MapReduce 高级操作介绍
转载自残缺的孤独 1.概述 MongoDB中的MapReduce相当于关系数据库中的group by.使用MapReduce要实现两个函数Map和Reduce函数.Map函数调用emit(key,va ...
- MongoDB中通过MapReduce实现合计Sum功能及返回格式不一致问题分析
建立下述测试数据,通过MapReduce统计每个班级学生数及成绩和. 代码如下: public string SumStudentScore() { var collection = _dataBas ...
- MongoDB中MapReduce介绍与使用
一.简介 在用MongoDB查询返回的数据量很大的情况下,做一些比较复杂的统计和聚合操作做花费的时间很长的时候,可以用MongoDB中的MapReduce进行实现 MapReduce是个非常灵活和强大 ...
- MongoDB中聚合工具Aggregate等的介绍与使用
Aggregate是MongoDB提供的众多工具中的比较重要的一个,类似于SQL语句中的GROUP BY.聚合工具可以让开发人员直接使用MongoDB原生的命令操作数据库中的数据,并且按照要求进行聚合 ...
- MongoDB中mapReduce的使用
MongoDB中mapReduce的使用 制作人:全心全意 mapReduce的功能和group by的功能类似,但比group by处理的数据量更大 使用示例: var map = function ...
- MongoDB中4种日志的详细介绍
前言 任何一种数据库都有各种各样的日志,MongoDB也不例外.MongoDB中有4种日志,分别是系统日志.Journal日志.oplog主从日志.慢查询日志等.这些日志记录着MongoDB数据库不同 ...
- 浅析mongodb中group分组
这篇文章主要介绍了浅析mongodb中group分组的实现方法及示例,非常的简单实用,有需要的小伙伴可以参考下. group做的聚合有些复杂.先选定分组所依据的键,此后MongoDB就会将集合依据选定 ...
- 基于MongoDB分布式存储进行MapReduce并行查询
中介绍了如何基于Mongodb进行关系型数据的分布式存储,有了存储就会牵扯到查询.虽然用普通的方式也可以进行查询,但今天要介绍的是如何使用MONGODB中提供的MapReduce功能进行查询. ...
- MongoDB实战指南(五):MongoDB中的聚集分析
聚集操作是对数据进行分析的有效手段.MongoDB主要提供了三种对数据进行分析计算的方式:管道模式聚集分析,MapReduce聚集分析,简单函数和命令的聚集分析. 1. 管道模式进行聚集 这里所说的管 ...
随机推荐
- C#基础之转换
C#中一共有两种转换方式,隐式转换和显示转换 隐式转换:就是不需要声明就能进行的转换,通俗来说就是小范围内的数据类型转大范围数据类型 显示转换:就是通常说的强制转换,需要在代码中写明要的数据类型.通俗 ...
- angular1与swiper
angular1路由切换过程中swiper不能使用. 问题1:在刚开始使用angular1的路由时,好多人会将swiper的初始化写在模板的父控制器上,这样会造成一个问题,swiper的初始化只在页面 ...
- Maven优雅的添加第三方Jar包
在利用Maven构建项目的时候会出现某些Jar包无法下载到本地的Repository中,鉴于这种情况比较普遍存在,特归纳以下解决问题办法:以 ojdbc14-10.2.0.4.0.jar为例[其它Ja ...
- excel中添加拼接行
Sub 万途标签()Dim iFor i = 1 To Sheets.Count If Sheets(i).Name = "数据表" Then If MsgBo ...
- C++类与对象(05)
类是具有惟一标识符的实体:在类中声明的任何成员不能使用extern.auto和register关键字进行修饰:类中声明的变量属于该类,在某些情况下,变量也可以被该类的不同实例所共享. 访问权限用于控制 ...
- 开源纯C#工控网关+组态软件(四)上下位机通讯原理
一. 网关的功能:承上启下 最近有点忙,更新慢了.感谢园友们给予的支持,现在github上已经有.目标是最好的开源组态,看来又近一步^^ 之前有提到网关是物联网的关键环节,它的作用就是承上启下. ...
- macOs升级到10.13.1Beta || JAVA升级到最新版之后PhpStorm菜单栏问题
macOs升级到10.13.1Beta || JAVA升级到最新版之后PhpStorm菜单栏会消失,估计不止出现在PhpStorm,一系列jetbrains的产品可能都会有这个问题,包括eclipis ...
- CentOS6编译LAMP基于FPM模式的应用wordpress
CentOS6编译LAMP基于FPM模式的应用wordpress 引言:其实我们可以直接使用yum安装LAMP(Linux+Apache[httpd]+Mysql+PHP),比手动编译安装LAMP要简 ...
- octave中的一些基本操作
1.矩阵的表示:v = [1 2 2] %表示1行3列的矩阵 v = [1; 2; 2] %表示3行1列的矩阵 v = [1 2; 2 3; 4 5] %3*2矩阵 size(v) % 求v的行与列 ...
- bug:页面交互操作引发的问题
最近在测试一些h5页面,突然悟到一些测试点 需求点: 用户可以在页面领取礼物,领取的礼物在页面底部展示,用户点击礼物可调起分享弹窗,礼物超过一屏可左右滑动, bug的表现形式: 仅当礼物超过一屏时(一 ...