一、简介

在用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:,

map:,

reduce:,

[,query:]

[,sort:]

[,limit:]

[,out:]

[,keeptemp:]

[,finalize:]

[,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 = function(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"})

mongo java mapreduce_MongoDB中MapReduce介绍与使用相关推荐

  1. mongo java mapreduce_MongoDB中的MapReduce简介

    MongoDB MapReduce MapReduce是一种计算模型,简单的说就是将大批量的工作(数据)分解(MAP)执行,然后再将结果合并成最终结果(REDUCE).这样做的好处是可以在任务被分解后 ...

  2. MongoDB中MapReduce介绍与使用

    本文来说下MongoDB中MapReduce介绍与使用 文章目录 概述 概述

  3. mongo java aggregate_Java中使用mongodb的aggregate聚合查询

    首先,我们在数据库中,mongodb的聚合查询是这样写. db.getCollection('parking_record').aggregate( {$match : {"appId&qu ...

  4. java EE中JPA介绍

    全称是:Java Persistence API (java 持久化应用接口),是由sun公司带头制定的.由于目前有很多java持久层的框架,比如Hibernate.TopLink.OpeanJPA等 ...

  5. JAVA大数据(二) Hadoop 分布式文件系统HDFS 架构,MapReduce介绍,Yarn资源调度

    文章目录 1.分布式文件系统HDFS 1.HDFS的来源 2.HDFS的架构图之基础架构 2.1 master/slave 架构 2.2 名字空间(NameSpace) 2.3 文件操作 2.4副本机 ...

  6. java中的jre里面有什么_Java中JRE介绍,JRE是什么

    首页 > 基础教程 > 基础知识 > JDK&JRE&JVM Java中JRE介绍,JRE是什么 JRE简介 JRE(Java Runtime Environment ...

  7. java 字符串 面试_JAVA中String介绍及常见面试题小结

    字符串广泛应用 在 Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串. 深刻认识String 1)String为字符串常量:即String对象一旦 ...

  8. java中的泛型是什么_Java中泛型是什么?Java泛型的详细介绍

    本篇文章给大家带来的内容是关于Java中泛型是什么?Java泛型的详细介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 一.什么是泛型? Java泛型设计原则:只要在编译时期没有出 ...

  9. java程序设计专业介绍_简介Java编程中的Object类

    这篇文章主要介绍了简介Java编程中的Object类,是Java入门学习中的基础知识,需要的朋友可以参考下 Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每 ...

最新文章

  1. 有没有想过,自己手写一个连接池?
  2. Mybatis逆向工程过程中出现targetRuntime in context mybatisGenerator is invalid
  3. Project Euler 1-25
  4. 数学老师出的谜语,语文老师已哭晕在厕所!
  5. nginx 直接在配置文章中设置日志分割
  6. Flask备注4(Structure)
  7. 没革哪有新?看云计算在医疗行业的版图
  8. 像excel一样规律填充(二)
  9. Python+Selenium+Edge浏览器安装与简单运行(2/2)
  10. Silverlight Xap和Html Asp.net参数传递
  11. iPhone UI 元素大小
  12. nas 和 远程文件夹同步_群晖NAS同步文件夹功能打开有什么需要注意的?
  13. 加载gif图片html,JS实现的自定义显示加载等待图片插件(loading.gif)
  14. 小程序tabBar无效
  15. RESTfull API简单项目的快速搭建
  16. Transforms的使用
  17. ios系统安装包下载_iOS 屏蔽系统升级,描述文件版本已复活,无需越狱,请速度下载!...
  18. tanlianjiyejiangemiao
  19. linux nc命令 测试网络连通性
  20. TS2532: xxx is possibly ‘undefined‘.

热门文章

  1. Tomcat8配置tomcat-users.xml配置
  2. Flask + Nginx + React + Webpack 配置解决跨域问题
  3. openfire单个插件编译
  4. cod cash on delivery
  5. nessus安全工具主要用途_发电技术 | 发电厂DCS 网络安全评估与防护
  6. 华为鸿蒙osbeta多场景展示,华为发布鸿蒙系统:全场景分布式OS
  7. defaultdict python_python中defaultdict的用法详解
  8. BufferedInputStream的read方法原理
  9. 金华杭州计算机学校录取分数线,2017年浙江金华各地中考录取分数线
  10. mysql try catch_C# try catch finally:异常处理