MongoDB中MapReduce编程模型使用实例

注:作者使用的MongoDB为2.4.7版本。

单词计数示例:


插入用于单词计数的数据:


db.data.insert({sentence:’Consider the following map-reduce operations on a collection orders that contains documents of the following prototype’})
db.data.insert({sentence:’I get the following error when I follow the code found in this link’})


图个简洁,数据中没有包含标点符号。 在mongo shell写入以下内容:

        if (word.length != 0) {
            emit(word, 1);
        }
    }
}


var reduce = function(key, values) {
    return Array.sum(values);
}


db.data.mapReduce(
    map,
    reduce,
    {out:{inline:1}}
)



db.data.mapReduce的第一和第二个参数分别指定map和reduce,map的输入是集合中的每个文档,通过emit()生成键值对;而reduce则处理键的多个值。


mapReduce的第三个参数指明在内存中进行mapreduce并返回结果,运行结果如下:

}



results的值是MapReduce的处理结果,timeMillis指明花费的时间;counts中input指明了输入的文档数,emit指明了在map中调用emit的次数,reduce指明了reduce的次数(本例中如果单次次数为1则不需要reduce),output指明了输出的文档数目。


可以看到,键_id不再是自动生成,而是被reduce中的key取代。当然,也可以将结果输入到一个新的collection中,例如:

db.mr_result.find()


也可以使用db.runCommand执行mapreduce任务,这种方法为开发者提供了更多的选项,具体请见资料[1]。资料[2][3][4]提供了关于mapreduce更全面的内容。资料[5]给出了优化mapreduce任务的方法,资料[6]是资料[5]的一篇中文翻译。


应该注意的是,资料[5]中提到使用ScopedThread()创建线程,笔者在GUI工具Robomongo的shell中运行 new ScopedThread()时候报错: ReferenceError: ScopedThread is not defined (shell):1


不过在mongo shell中可以正常运行:


> new ScopedThread()
Sat Mar 22 21:32:36.062 Error: need at least one argument at src/mongo/shell/utils.js:101


如果使用其他编程语言管理MongoDB,要用到线程时,应该使用该编程语言内置的线程。


关于mongodb实现的mapreduce,个人觉得如果支持多个MR任务平滑过渡就更好了。


数据运维技术 » MongoDB中MapReduce编程模型使用实例