各位好,在阅读本文请务必先阅读上一篇文章《来,我给你们看一段神奇的mongodb的mapreduce操作!》,链接:http://gong1208.iteye.com/blog/1830576
因为此文是上一篇文章的解释。
我在上篇博客中指出的mongodb进行mapreduce时出现的奇怪的错误,其实是我个人的错误,原因在于mongodb进行mapreduce时,reduce函数有一段说明:
Requirements for the reduce Function
The reduce function has the following prototype:
function(key, values) {
...
return result;
}
The reduce function exhibits the following behaviors:
- The reduce function should not access the database, even to perform read operations.
- The reduce function should not affect the outside system.
- MongoDB will not call the reduce function for a key that has only a single value.
- The reduce function can access the variables defined in the scope parameter.
Because it is possible to invoke the reduce function more than once for the same key, the following properties need to be true
- he type of the return object must be identical to the type of the value emitted by the map function to ensure that the following operations is true:
· reduce(key, [ C, reduce(key, [ A, B ]) ] ) == reduce( key, [ C, A, B ] )
· reduce( key, [ reduce(key, valuesArray) ] ) == reduce( key, valuesArray )
- the order of the elements in the valuesArray should not affect the output of the reduce function, so that the following statement is true:
reduce( key, [ A, B ] ) == reduce( key, [ B, A ] )
参考地址: http://docs.mongodb.org/manual/reference/method/db.collection.mapReduce/#db.collection.mapReduce
这段话的意思是,reduce函数有可能在执行一个任务是可能会被调用多次,而不是我们理解的传统的方法中,一次任务只调用一次,所以,reduce函数必须是幂等的。简单来说,就是reduce函数中接收的value参数的形式,必须和reduce函数返回的结果value的形式一致。
仍然拿我上个例子说明:
起初我是这么写的:
2. printjson("job start"); 3. var map = function() { 4. emit(this.ip, {value: 1}); 5. } 6. 7. var reduce = function(key, values) { 8. var count = 0; 9. values.forEach(function(v) { 10. count += v['value']; 11. }); 12. return {count: count }; 13. 14. } 15. 16. var res = db.runCommand({mapreduce:"RegistRecord",map:map, reduce:reduce, out:"log_results"}); 17. printjson("job end")
可以看出emit函数的第二个参数形式为:{value:number},所以reduce函数的values值的形式为:{value:number},所以,reduce函数的返回值形式也必须应当是{value:number},因为reduce函数会将自己的返回值再次作为下一次reduce的输入值使用。
改为如下就正确了:
var reduce = function(key, values) { var count = {value:0}; values.forEach(function(v) { count.value += v['value']; }); return count; }
ps:在此特别感谢mongodb社区的Kay.Kim<kay.kim@10gen.com>,我曾发了封邮件向mongodb社区请教此问题,没想到居然收到了社区的热心答复,并为我解答了此问题。
相关推荐
MongoDB MapReduce MapReduce是一种计算模型,简单的说就是将大批量的工作(数据)分解(MAP)执行,然后再将结果合并成最终结果(REDUCE)。这样做的好处是可以在任务被分解后,可以通过大量机器进行并行计算,减少...
mongodb mapreduce 实例,该例子主要用来做订单统计的。具体问题请到博客提问。
MapReduce应该算是MongoDB操作中比较复杂的了,下面这篇文章主要给大家介绍了关于MongoDB中MapReduce使用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起看看吧。
NULL 博文链接:https://superhuo.iteye.com/blog/1193485
MongoDB的MapReduce.pdf 学习资料 复习资料 教学资源
mongo group by mapreduce操作指导!
Mongodb是针对大数据量环境下诞生的用于保存大数据量的非关系型数据库,针对大量的数据。接下来通过本文给大家介绍Mongodb中MapReduce实现数据聚合方法详解,感兴趣的朋友一起学习吧
1.分篇章进行学习,内容控制30分钟内 2.1个月疗程,不要放弃治疗哦 3.图文并茂,有问题请发到邮箱
mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce ...
一个简单的Mongodb mapreduce的例子
MapReduce发明人关于MapReduce的介绍
MapReduce 是 Google 在 2004 年发布的一个软件框架,用于支持大规模数据的分布式计算。 MongoDB 是一个开源的面向文档的 NoSQL 数据库系统,使用 C++ 编写。
Java操作Hadoop Mapreduce基本实践源码.
MapReduce操作实例-数据去重.pdf 学习资料 复习资料 教学资源
MapReduce操作实例-倒排索引.pdf 学习资料 复习资料 教学资源
4 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后查看 MapReduce Web 界面。 5. 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后练习 MapReduce Shell 常用命令。 。。
大数据mapreduce案例介绍,包括代码解释,详解MRS工作流程
Mapreduce实验报告 前言和简介 MapReduce是Google提出的一种编程模型,在这个模型的支持下可以实现大规模并行化计 算。在Mapreduce框架下一个计算机群通过统一的任务调度将一个巨型任务分成许多部分 ,分别解决然后...