濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > MongoDB中MapReduce編程模型使用實(shí)例

MongoDB中MapReduce編程模型使用實(shí)例

熱門標(biāo)簽:高德地圖標(biāo)注商戶位置 徐州ai電銷機(jī)器人原理 企業(yè)智能外呼系統(tǒng)價(jià)格多少 沈陽營(yíng)銷電銷機(jī)器人招商 兗州電話外呼營(yíng)銷系統(tǒng) 智能電銷機(jī)器人銷售話術(shù) 機(jī)器人外呼系統(tǒng)軟件存在問題 福州電銷機(jī)器人源代碼 南京400電話怎樣辦理

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

單詞計(jì)數(shù)示例:

插入用于單詞計(jì)數(shù)的數(shù)據(jù):

復(fù)制代碼 代碼如下:

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'})

圖個(gè)簡(jiǎn)潔,數(shù)據(jù)中沒有包含標(biāo)點(diǎn)符號(hào)。 在mongo shell寫入以下內(nèi)容:

復(fù)制代碼 代碼如下:

var map = function() {
    split_result = this.sentence.split(" ");
    for (var i in split_result) {
        var word = split_result[i].replace(/(^\s*)|(\s*$)/g,"").toLowerCase(); //去除了單詞兩邊可能的空格,并將單詞轉(zhuǎn)換為小寫
        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的第一和第二個(gè)參數(shù)分別指定map和reduce,map的輸入是集合中的每個(gè)文檔,通過emit()生成鍵值對(duì);而reduce則處理鍵的多個(gè)值。

mapReduce的第三個(gè)參數(shù)指明在內(nèi)存中進(jìn)行mapreduce并返回結(jié)果,運(yùn)行結(jié)果如下:

復(fù)制代碼 代碼如下:

{
        "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的處理結(jié)果,timeMillis指明花費(fèi)的時(shí)間;counts中input指明了輸入的文檔數(shù),emit指明了在map中調(diào)用emit的次數(shù),reduce指明了reduce的次數(shù)(本例中如果單次次數(shù)為1則不需要reduce),output指明了輸出的文檔數(shù)目。

可以看到,鍵_id不再是自動(dòng)生成,而是被reduce中的key取代。當(dāng)然,也可以將結(jié)果輸入到一個(gè)新的collection中,例如:

復(fù)制代碼 代碼如下:
db.data.mapReduce( map, reduce, {out:"mr_result"} )

之后查看mr_result集合中的內(nèi)容即可:
復(fù)制代碼 代碼如下:
db.mr_result.find()

也可以使用db.runCommand執(zhí)行mapreduce任務(wù),這種方法為開發(fā)者提供了更多的選項(xiàng),具體請(qǐng)見資料[1]。資料[2][3][4]提供了關(guān)于mapreduce更全面的內(nèi)容。資料[5]給出了優(yōu)化mapreduce任務(wù)的方法,資料[6]是資料[5]的一篇中文翻譯。

應(yīng)該注意的是,資料[5]中提到使用ScopedThread()創(chuàng)建線程,筆者在GUI工具Robomongo的shell中運(yùn)行 new ScopedThread()時(shí)候報(bào)錯(cuò): ReferenceError: ScopedThread is not defined (shell):1

不過在mongo shell中可以正常運(yùn)行:

復(fù)制代碼 代碼如下:

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

如果使用其他編程語言管理MongoDB,要用到線程時(shí),應(yīng)該使用該編程語言內(nèi)置的線程。

關(guān)于mongodb實(shí)現(xiàn)的mapreduce,個(gè)人覺得如果支持多個(gè)MR任務(wù)平滑過渡就更好了。

您可能感興趣的文章:
  • Spring 注解編程模型相關(guān)知識(shí)詳解
  • python多線程抽象編程模型詳解
  • 編程用到的windows系統(tǒng)目錄變量簡(jiǎn)寫
  • 用PHP編程開發(fā)“虛擬域名”系統(tǒng)
  • 詳解為什么現(xiàn)代系統(tǒng)需要一個(gè)新的編程模型

標(biāo)簽:邯鄲 吉安 大理 景德鎮(zhèn) 鶴崗 丹東 昭通 本溪

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB中MapReduce編程模型使用實(shí)例》,本文關(guān)鍵詞  MongoDB,中,MapReduce,編程,模型,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MongoDB中MapReduce編程模型使用實(shí)例》相關(guān)的同類信息!
  • 本頁收集關(guān)于MongoDB中MapReduce編程模型使用實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    新干县| 灵山县| 贺州市| 泽库县| 郁南县| 东莞市| 剑河县| 盐源县| 安远县| 苏尼特右旗| 内丘县| 芦山县| 密云县| 乐安县| 武汉市| 宜宾市| 石林| 阳曲县| 佛坪县| 巧家县| 福泉市| 田东县| 邓州市| 莱芜市| 光泽县| 肇庆市| 安达市| 阿克陶县| 丰宁| 施秉县| 水富县| 孟津县| 双辽市| 崇州市| 金坛市| 夏河县| 正阳县| 喀什市| 沽源县| 柳林县| 东港市|