python - MapReduce 结果似乎限制为 100?

标签 python mongodb mapreduce pymongo

我在 MongoDB 和 python 中使用 Map Reduce,但遇到了一个奇怪的限制。我只是想计算“书”记录的数量。它在少于 100 条记录时有效,但当超过 100 条记录时,由于某种原因计数会重置。

这是我的 MR 代码和一些示例输出:

var M = function () {
book = this.book;
emit(book, {count : 1});
}

var R = function (key, values) {
var sum = 0;
values.forEach(function(x) {
sum += 1;
});
var result = {
count : sum 
};
return result;
}

记录数为99时的MR输出:

{u'_id': u'superiors', u'value': {u'count': 99}}

记录数为101时的MR输出:

{u'_id': u'superiors', u'value': {u'count': 2.0}}

有什么想法吗?

最佳答案

您的 reduce 函数应该对 count 值求和,而不仅仅是为每个值添加 1。否则,一个 reduce 的输出不能被正确地用作另一个 reduce 的输入。试试这个:

var R = function (key, values) {
  var sum = 0;
  values.forEach(function(x) {
    sum += x.count;
  });
  var result = {
    count : sum 
  };
  return result;
}

关于python - MapReduce 结果似乎限制为 100?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13318791/

相关文章:

python:如果它只有常量,那么实例化一个类与引用相比有什么缺点吗?

java - 如何在mongodb中获取嵌套文档

ruby-on-rails-3 - MongoDB设置: mongo.日志权限错误?

javascript - Meteorjs : How can I use variables which are defined in template events inside my template helpers?

java - 将Hadoop FS中的所有JARS添加到MapReduce类路径

java - 在mahout频谱聚类中,亲和矩阵中的对角元素值应该是多少

python - 如何从时间序列信号中解卷积阶跃函数

python - 我可以让 Anaconda 导航器查看 pypi 上的包吗?

python - 编程竞赛,如输入验证器

xml - 为什么 YARN 上会有 mapreduce.jobtracker.address 配置?