程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MongoDB數據庫 >> MongoDB綜合知識 >> MongoDB中MapReduce編程模型使用實例

MongoDB中MapReduce編程模型使用實例

編輯:MongoDB綜合知識

注:作者使用的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任務平滑過渡就更好了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved