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写入以下内容:

代码如下:

var map = function() {
    split_result = this.sentence.split(" ");
    for (var i in split_result) {
        var word = split_result[i].replace(/(^\s*)|(\s*$)/g,"").toLowerCase(); //去除了单词两边可能的空格,并将单词转换为小写
        if (word.length != 0) {
            emit(word, 1);
        }
    }
}
<p>var reduce = function(key, values) {
    return Array.sum(values);
}</p>
<p>db.data.mapReduce(
    map,
    reduce,
    {out:{inline:1}}
)</p>


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

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

代码如下:

{
        "results" : [
                {
                        "_id" : "a",
                        "value" : 1
                },
                {
                        "_id" : "code",
                        "value" : 1
                },
                {
                        "_id" : "collection",
                        "value" : 1
                },
                {
                        "_id" : "consider",
                        "value" : 1
                },
                {
                        "_id" : "contains",
                        "value" : 1
                },
                {
                        "_id" : "documents",
                        "value" : 1
                },
                {
                        "_id" : "error",
                        "value" : 1
                },
                {
                        "_id" : "follow",
                        "value" : 1
                },
                {
                        "_id" : "following",
                        "value" : 3
                },
                {
                        "_id" : "found",
                        "value" : 1
                },
                {
                        "_id" : "get",
                        "value" : 1
                },
                {
                        "_id" : "i",
                        "value" : 2
                },
                {
                        "_id" : "in",
                        "value" : 1
                },
                {
                        "_id" : "link",
                        "value" : 1
                },
                {
                        "_id" : "map-reduce",
                        "value" : 1
                },
                {
                        "_id" : "of",
                        "value" : 1
                },
                {
                        "_id" : "on",
                        "value" : 1
                },
                {
                        "_id" : "operations",
                        "value" : 1
                },
                {
                        "_id" : "orders",
                        "value" : 1
                },
                {
                        "_id" : "prototype",
                        "value" : 1
                },
                {
                        "_id" : "that",
                        "value" : 1
                },
                {
                        "_id" : "the",
                        "value" : 4
                },
                {
                        "_id" : "this",
                        "value" : 1
                },
                {
                        "_id" : "when",
                        "value" : 1
                }
        ],
        "timeMillis" : 1,
        "counts" : {
                "input" : 2,
                "emit" : 30,
                "reduce" : 3,
                "output" : 24
        },
        "ok" : 1,
}


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

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

代码如下:

db.data.mapReduce( map, reduce, {out:"mr_result"} )

之后查看mr_result集合中的内容即可:

代码如下:

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任务平滑过渡就更好了。

相关推荐