MongoDB中MapReduce編程模型使用執行個體_MongoDB

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.