Map-Reduce是一種計算模型,簡單的說就是將大批量的工作(數(shù)據(jù))分解(MAP)執(zhí)行,然后再將結(jié)果合并成最終結(jié)果(REDUCE)。
MongoDB提供的Map-Reduce非常靈活,對于大規(guī)模數(shù)據(jù)分析也相當實用。
MapReduce 命令
以下是MapReduce的基本語法:
>db.collection.mapReduce( function() {emit(key,value);}, //map 函數(shù) function(key,values) {return reduceFunction}, //reduce 函數(shù) { out: collection, query: document, sort: document, limit: number } )
使用 MapReduce 要實現(xiàn)兩個函數(shù) Map 函數(shù)和 Reduce 函數(shù),Map 函數(shù)調(diào)用 emit(key, value), 遍歷 collection 中所有的記錄, 將key 與 value 傳遞給 Reduce 函數(shù)進行處理。
Map 函數(shù)必須調(diào)用 emit(key, value) 返回鍵值對。
參數(shù)說明:
map :映射函數(shù) (生成鍵值對序列,作為 reduce 函數(shù)參數(shù))。
reduce 統(tǒng)計函數(shù),reduce函數(shù)的任務(wù)就是將key-values變成key-value,也就是把values數(shù)組變成一個單一的值value。。
out 統(tǒng)計結(jié)果存放集合 (不指定則使用臨時集合,在客戶端斷開后自動刪除)。
query 一個篩選條件,只有滿足條件的文檔才會調(diào)用map函數(shù)。(query。limit,sort可以隨意組合)
sort 和limit結(jié)合的sort排序參數(shù)(也是在發(fā)往map函數(shù)前給文檔排序),可以優(yōu)化分組機制
limit 發(fā)往map函數(shù)的文檔數(shù)量的上限(要是沒有l(wèi)imit,單獨使用sort的用處不大)
使用 MapReduce
考慮以下文檔結(jié)構(gòu)存儲用戶的文章,文檔存儲了用戶的 user_name 和文章的 status 字段:
>db.posts.insert({ "post_text": "php中文網(wǎng),最全的技術(shù)文檔。", "user_name": "mark", "status":"active" }) WriteResult({ "nInserted" : 1 }) >db.posts.insert({ "post_text": "php中文網(wǎng),最全的技術(shù)文檔。", "user_name": "mark", "status":"active" }) WriteResult({ "nInserted" : 1 }) >db.posts.insert({ "post_text": "php中文網(wǎng),最全的技術(shù)文檔。", "user_name": "mark", "status":"active" }) WriteResult({ "nInserted" : 1 }) >db.posts.insert({ "post_text": "php中文網(wǎng),最全的技術(shù)文檔。", "user_name": "mark", "status":"active" }) WriteResult({ "nInserted" : 1 }) >db.posts.insert({ "post_text": "php中文網(wǎng),最全的技術(shù)文檔。", "user_name": "mark", "status":"disabled" }) WriteResult({ "nInserted" : 1 }) >db.posts.insert({ "post_text": "php中文網(wǎng),最全的技術(shù)文檔。", "user_name": "php", "status":"disabled" }) WriteResult({ "nInserted" : 1 }) >db.posts.insert({ "post_text": "php中文網(wǎng),最全的技術(shù)文檔。", "user_name": "php", "status":"disabled" }) WriteResult({ "nInserted" : 1 }) >db.posts.insert({ "post_text": "php中文網(wǎng),最全的技術(shù)文檔。", "user_name": "php", "status":"active" }) WriteResult({ "nInserted" : 1 })
現(xiàn)在,我們將在 posts 集合中使用 mapReduce 函數(shù)來選取已發(fā)布的文章(status:"active"),并通過user_name分組,計算每個用戶的文章數(shù):
>db.posts.mapReduce( function() { emit(this.user_name,1); }, function(key, values) {return Array.sum(values)}, { query:{status:"active"}, out:"post_total" } )
以上 mapReduce 輸出結(jié)果為:
{ "result" : "post_total", "timeMillis" : 23, "counts" : { "input" : 5, "emit" : 5, "reduce" : 1, "output" : 2 }, "ok" : 1 }
結(jié)果表明,共有4個符合查詢條件(status:"active")的文檔, 在map函數(shù)中生成了4個鍵值對文檔,最后使用reduce函數(shù)將相同的鍵值分為兩組。
具體參數(shù)說明:
result:儲存結(jié)果的collection的名字,這是個臨時集合,MapReduce的連接關(guān)閉后自動就被刪除了。
timeMillis:執(zhí)行花費的時間,毫秒為單位
input:滿足條件被發(fā)送到map函數(shù)的文檔個數(shù)
emit:在map函數(shù)中emit被調(diào)用的次數(shù),也就是所有集合中的數(shù)據(jù)總量
ouput:結(jié)果集合中的文檔個數(shù)(count對調(diào)試非常有幫助)
ok:是否成功,成功為1
err:如果失敗,這里可以有失敗原因,不過從經(jīng)驗上來看,原因比較模糊,作用不大
使用 find 操作符來查看 mapReduce 的查詢結(jié)果:
>db.posts.mapReduce( function() { emit(this.user_name,1); }, function(key, values) {return Array.sum(values)}, { query:{status:"active"}, out:"post_total" } ).find()
以上查詢顯示如下結(jié)果,兩個用戶 tom 和 mark 有兩個發(fā)布的文章:
{ "_id" : "mark", "value" : 4 } { "_id" : "php", "value" : 1 }
用類似的方式,MapReduce可以被用來構(gòu)建大型復(fù)雜的聚合查詢。
Map函數(shù)和Reduce函數(shù)可以使用 JavaScript 來實現(xiàn),使得MapReduce的使用非常靈活和強大。