MongoDB中MapReduce編程模型使用實例
注:作者使用的MongoDB為2.4.7版本。
單詞計數(shù)示例:
插入用于單詞計數(shù)的數(shù)據(jù):
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'})
圖個簡潔,數(shù)據(jù)中沒有包含標點符號。 在mongo shell寫入以下內(nèi)容:
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的第一和第二個參數(shù)分別指定map和reduce,map的輸入是集合中的每個文檔,通過emit()生成鍵值對;而reduce則處理鍵的多個值。
mapReduce的第三個參數(shù)指明在內(nèi)存中進行mapreduce并返回結(jié)果,運行結(jié)果如下:
{
"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指明花費的時間;counts中input指明了輸入的文檔數(shù),emit指明了在map中調(diào)用emit的次數(shù),reduce指明了reduce的次數(shù)(本例中如果單次次數(shù)為1則不需要reduce),output指明了輸出的文檔數(shù)目。
可以看到,鍵_id不再是自動生成,而是被reduce中的key取代。當然,也可以將結(jié)果輸入到一個新的collection中,例如:
之后查看mr_result集合中的內(nèi)容即可:
也可以使用db.runCommand執(zhí)行mapreduce任務(wù),這種方法為開發(fā)者提供了更多的選項,具體請見資料[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中運行 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,要用到線程時,應(yīng)該使用該編程語言內(nèi)置的線程。
關(guān)于mongodb實現(xiàn)的mapreduce,個人覺得如果支持多個MR任務(wù)平滑過渡就更好了。
相關(guān)文章
MongoDB增刪查改操作示例【基于JavaScript Shell】
這篇文章主要介紹了MongoDB增刪查改操作,結(jié)合實例形式分析了MongoDB數(shù)據(jù)庫基于JavaScript Shell的基本增刪查改操作技巧與使用注意事項,需要的朋友可以參考下2019-07-07
如何對 MongoDB 進行性能優(yōu)化(五個簡單步驟)
MongoDB一直是最流行的NoSQL,而根據(jù)DB-Engines Ranking最新的排行,時下MongoDB已經(jīng)擊敗PostgreSQL躍居數(shù)據(jù)庫總排行的第四位,僅次于Oracle、MySQL和Microsoft SQL Server。本文給大家介紹MongoDB性能優(yōu)化的簡單總結(jié)。2015-10-10
基于MongoDB數(shù)據(jù)庫索引構(gòu)建情況全面分析
下面小編就為大家?guī)硪黄贛ongoDB數(shù)據(jù)庫索引構(gòu)建情況全面分析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
教你使用mongoose實現(xiàn)多集合關(guān)聯(lián)查詢
這篇文章主要給大家介紹了關(guān)于如何使用mongoose實現(xiàn)多集合關(guān)聯(lián)查詢的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-02-02
解決MongoDB6.0報錯:"mongo"不是內(nèi)部或外部命令,也不是可運行的程序或批處理文件
這篇文章主要給大家介紹了關(guān)于解決MongoDB6.0報錯:"mongo"不是內(nèi)部或外部命令,也不是可運行的程序或批處理文件的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-05-05
MongoDB系列教程(六):java操作mongodb實例
這篇文章主要介紹了MongoDB系列教程(六):java操作mongodb實例,本文講解了java中操作mongodb數(shù)據(jù)增加、刪除、修改、查詢數(shù)據(jù)等代碼實例,需要的朋友可以參考下2015-05-05

