MongoDB MapReduce : Global variables within map function instance?

标签 mongodb mapreduce

我在 MongoDB 中编写了 MapReduce,并希望使用全局变量作为缓存来写入/读取。我知道不可能有全局变量 across 映射函数实例 - 我只想要一个全局变量 within 每个函数实例。这种类型的功能存在于 Hadoop 的 MapReduce 中,所以我期待它存在于 MongoDB 中。但以下似乎不起作用:

var cache = {}; // Does not seem to work!
function () {
  var hashValue = this.varValue1 + this.varValue2;
  if(typeof(cache[hashValue])!= 'undefined') {
    // Do nothing, we've processed at least one input record with this hash
  } else {
    // Process the input record
    // Cache the record
    cache[hashValue] = '1';
  }
}

这在 MongoDB 的 MapReduce 实现中是不允许的,还是我在 JavaScript 中做错了什么(在 JS 中没有经验)?

最佳答案

docs ,我发现以下内容:

db.runCommand(
 { mapreduce : <collection>,
   map : <mapfunction>,
   reduce : <reducefunction>
   [, scope : <object where fields go into javascript global scope >]
 }
);

我认为“范围”变量是您所需要的。

有一个测试/示例on Github使用“范围”变量。

我还是新手,但希望这足以让你开始。

关于MongoDB MapReduce : Global variables within map function instance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2996268/

相关文章:

node.js - 数组中不匹配值的 MongoDB 百分比

json - MongoDB,find() 结果,转换为 json

javascript - 如何嵌套查找MongoDB? JavaScript

javascript - Mongodb 按 dbref 字段分组

hadoop - 当映射器在 EMR 流式处理作业中死亡时会发生什么?

mongodb - 使用 mgo 将上传的文件存储在 MongoDB GridFS 中而不保存到内存

mongodb - 使用迭代动态构建条件 block

hadoop - Mapreduce 洗牌阶段出现内存不足错误

hadoop 对键进行排序并更改键值

hadoop - 从 "reduce input records"到 "reduce input groups"