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數(shù)據(jù)庫聚合之分組統(tǒng)計$group的用法詳解
在MongoDB中聚合框架允許用戶對數(shù)據(jù)進行處理和分析,以便進行統(tǒng)計計算、匯總以及更復(fù)雜的數(shù)據(jù)轉(zhuǎn)換,這篇文章主要給大家介紹了關(guān)于MongoDB數(shù)據(jù)庫聚合之分組統(tǒng)計$group的用法的相關(guān)資料,需要的朋友可以參考下2024-06-06批量備份還原導入與導出MongoDB數(shù)據(jù)方式
前面我們介紹了mongoDB的安裝與配置,接下來我們講一講如何使用mongo的可視化工具Navicat對mongo數(shù)據(jù)庫進行操作和如何在終端進行mongo數(shù)據(jù)庫對數(shù)據(jù)的批量操作2021-09-09MongoDB固定集合(capped collection)的知識小結(jié)
固定集合指的是事先創(chuàng)建,并且大小固定的集合。下面這篇文章主要給大家總結(jié)介紹了MongoDB固定集合(capped collection)的知識,文中介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起學習學習吧2018-10-10