欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MongoDB中MapReduce编程模型使用实例

程序员文章站 2022-07-06 10:17:59
注:作者使用的mongodb为2.4.7版本。单词计数示例: 插入用于单词计数的数据:复制代码 代码如下:db.data.insert({sentence:'consid...

注:作者使用的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);
        }
    }
}

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" : [
                {
                        "_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任务平滑过渡就更好了。