http://my.oschina.net/huzorro/blog/75210
这篇也写的很好
第八章 MapReduce
MongoDB的MapReduce相当于Mysql中的"group by",所以在MongoDB上使用 Map/Reduce 进行并行"统计"很容易。
使用MapReduce要实现两个函数 Map函数和Reduce函数,Map函数调用emit(key, value),遍历collection中所有的记录,将key与value传递给Reduce函数进行处理。Map函数和Reduce函数可以使用JavaScript来实现,可以通过db.runCommand或mapReduce命令来执行一个MapReduce的操作:
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 >] [, verbose : true] } );
|
参数说明:
l mapreduce: 要操作的目标集合。
l map: 映射函数 (生成键值对序列,作为 reduce 函数参数)。
l reduce: 统计函数。
l query: 目标记录过滤。
l sort: 目标记录排序。
l limit: 限制目标记录数量。
l out: 统计结果存放集合 (不指定则使用临时集合,在客户端断开后自动删除)。
l keeptemp: 是否保留临时集合。
l finalize: 最终处理函数 (对 reduce 返回结果进行最终整理后存入结果集合)。
l scope: 向 map、reduce、finalize 导入外部变量。
l verbose: 显示详细的时间统计信息。
下面我们先准备一些数据:
db.students.insert({classid:1, age:14, name:'Tom'}); db.students.insert({classid:1, age:12, name:'Jacky'}); db.students.insert({classid:2, age:16, name:'Lily'}); db.students.insert({classid:2, age:9, name:'Tony'}); db.students.insert({classid:2, age:19, name:'Harry'}); db.students.insert({classid:2, age:13, name:'Vincent'}); db.students.insert({classid:1, age:14, name:'Bill'}); db.students.insert({classid:2, age:17, name:'Bruce'});
接下来,我们将演示如何统计1班和2班的学生数量
8.1 Map
Map 函数必须调用 emit(key, value) 返回键值对,使用 this 访问当前待处理的 Document。
m = function () { emit(this.classid, 1); }
|
map后的数据就变为:
db.students.insert({classid:1, age:14, name:'Tom'}); =>(1,1) db.students.insert({classid:1, age:12, name:'Jacky'}); =>(1,1) db.students.insert({classid:2, age:16, name:'Lily'}); =>(2,1) db.students.insert({classid:2, age:9, name:'Tony'}); =>(2,1) db.students.insert({classid:2, age:19, name:'Harry'});=>(2,1) db.students.insert({classid:2, age:13, name:'Vincent'});=>(2,1) db.students.insert({classid:1, age:14, name:'Bill'});=>(1,1) db.students.insert({classid:2, age:17, name:'Bruce'});=>(2,1)
value 可以使用 JSON Object 传递 (支持多个属性值)。例如:
emit(this.classid, {count:1})
8.2 Reduce
Reduce函数接收的参数类似 Group 效果,将 Map 返回的键值序列组合成 { key, [value1, value2, value3, value...] } 传递给 reduce。
以上例子结果将变为:
{1,[1,1,1]}
{2,[1,1,1,1]}
r = function (key, values) { var x = 0; values.forEach(function (v) {x += v;}); return x; }
|
Reduce 函数对这些 values 进行 "统计" 操作,返回结果可以使用 JSON Object。
8.3 Result
res = db.runCommand({ mapreduce:"students", map:m, reduce:r, out:"students_res" }); -----------------------输出----------------------- { "result" : "students_res", "timeMillis" : 1587, "counts" : { "input" : 8, "emit" : 8, "output" : 2 }, "ok" : 1 } > db.students_res.find() { "_id" : 1, "value" : 3 } { "_id" : 2, "value" : 5 } >
|
mapReduce() 将结果存储在 "students_res" 表中。
8.4 Finalize
利用 finalize() 我们可以对 reduce() 的结果做进一步处理。
> f = function(key, value) { return {classid:key, count:value}; } function (key, value) { return {classid:key, count:value}; } >
|
我们再重新计算一次,看看返回的结果:
> res = db.runCommand({ mapreduce:"students", map:m, reduce:r, out:"students_res", finalize:f }); { "result" : "students_res", "timeMillis" : 804, "counts" : { "input" : 8, "emit" : 8, "output" : 2 }, "ok" : 1 } > db.students_res.find() { "_id" : 1, "value" : { "classid" : 1, "count" : 3 } } { "_id" : 2, "value" : { "classid" : 2, "count" : 5 } } >
|
列名变与 “classid”和”count”了,这样的列表更容易理解。
8.5 Options
我们还可以添加更多的控制细节。
> res = db.runCommand({ mapreduce:"students", map:m, reduce:r, out:"students_res", finalize:f, query:{age:{$lt:10}} }); { "result" : "students_res", "timeMillis" : 358, "counts" : { "input" : 1, "emit" : 1, "output" : 1 }, "ok" : 1 } > db.students_res.find(); { "_id" : 2, "value" : { "classid" : 2, "count" : 1 } } >
|
可以看到先进行了过滤,只取age<10的数据,然后再进行统计,所以就没有1班的统计数据了。
发表评论
-
详解MongoDB的Compact操作(转)
2012-10-22 16:50 1266转自 http://blog.nosqlfan.com/ ... -
在Windows上安装多个Mysql[主从数据库同步数据]
2012-10-18 10:32 1648主从数据库数据同步,可以使用从数据库来实 ... -
MYsql 超时失效
2012-10-15 16:50 681解决mysql中共8小时空闲后连接超时的问题 我 ... -
MongoDB资料汇总
2012-10-09 10:32 1090MongoDB是一个基于分布式文件存储的数据库。由C ... -
mongodb 总结
2012-10-08 10:48 830生产环境最佳实践 1.linux 系统: 1】关闭文件系 ... -
Mongo Database 性能优化
2012-10-08 09:05 1036SQL Server有工具进行数据库的优化,Mongo Dat ... -
MongoDB 修改器
2012-09-29 10:22 794命令: $inc 用来增加 ... -
MongoDB Windows 下安装部署
2012-09-28 16:52 619下面主要是我在Windows上(Win7)安装、运行、安装Wi ... -
MySQL和Java时间毫秒之间的转换问题的总结
2012-09-28 11:48 3338最近几天在做关于依时间查询用户认证的业务,因为在MySQL中, ... -
MySQL 获取当前AUTO_INCREMENT的ID
2012-09-27 14:38 1782获得新生成的AUTO_INCREMENT属性的值也有多种方法 ... -
PowerDesigner 技巧小结
2012-09-19 17:07 4567分类: Database 2012-03-23 12:49 7 ... -
嵌入式DERBY数据库试用
2012-09-19 11:31 1096import java.sql.Connection; ... -
MYSQL SQL书写优化建议,数据库
2012-09-19 11:23 7601、选取最适用的字段属性 Mysql可以很好的支持大数据 ... -
derby 空间压缩回收
2012-09-14 18:09 923CALL SYSCS_UTIL.SYSCS_COMPRESS_ ... -
derby的IJ 连接
2012-09-14 17:18 802connect 'jdbc:derby:E:\svn_sour ... -
derby分页
2012-09-13 17:24 982SELECT * FROM YOUR_TABLE OF ...
相关推荐
MapReduce应该算是MongoDB操作中比较复杂的了,下面这篇文章主要给大家介绍了关于MongoDB中MapReduce使用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起看看吧。
mongo group by mapreduce操作指导!
java操作mongo的小例子
mongo spring boot Aggregate 例子
spring MVC morphia mongo 整合的例子 网上下载的例子 自己调试保证绝对能运行
和博客对应的spring_mongo的例子。
Mongodb是针对大数据量环境下诞生的用于保存大数据量的非关系型数据库,针对大量的数据。接下来通过本文给大家介绍Mongodb中MapReduce实现数据聚合方法详解,感兴趣的朋友一起学习吧
Hadoop集群配置及MapReduce开发手册
阿里mongo同步工具mongo-shake
mongoVUE.1.6.9.破解文件,解压后覆盖安装目录中的MongoVUE.exe文件 安装文件下载地址:http://www.mongovue.com/downloads/
1.分篇章进行学习,内容控制30分钟内 2.1个月疗程,不要放弃治疗哦 3.图文并茂,有问题请发到邮箱
Mongo数据库连接工具,带破解批处理文件.3T支持的mongo功能很多
一个简单的增删改查询的简单例子,数据库用的是Mongo.适合初学者,代码仅供借鉴。
NULL 博文链接:https://tompig.iteye.com/blog/1101488
mongo客户端mongo客户端mongo客户端mongo客户端mongo客户端
安装后将mongovue.exe替换系统安装的mongovue.exe就能免费使用mongovue所有功能。
MongoVUE1.6.9软件及其破解方法,本人亲测,可用
MongoVUE
mongo免安装mongo免安装
solr-mongo-importer-1.1.0.jar solr-mongo-importer-1.1.0.jar solr-mongo-importer-1.1.0.jar