MongoDB mapreduce 使用
MapReduce介紹
MapReduce是一種編程模型,用於大規模數據集(大於1TB)的並行運算。
它極大地方便了編程人員在不會分佈式並行編程的情況下,將自己的程序運行在分佈式系統上。
指定併發的Reduce(歸約)函數,用來保證所有映射的鍵值對中的每一個共享相同的鍵組。
MongoDB 中的MapReduce 使用JavaScript來作爲查詢語言,因此能表達任意的邏輯,
但是它運行非常慢,不應該用在實時的數據分析中 。
MongoDB MapReduce語法
db.runCommand("mapreduce":“集合名稱”,"map":Map函數名稱,"reduce":Reduce函數名稱,"out":"集合名稱")
1:Map函數使用emit函數來返回要處理的值 。this 表示整個文檔
2:reduce函數需要處理Map階段或者是前一個reduce的數據,因此reduce返回的文檔必須要能作爲reduce的第二個參數的一個元素,
問題一:計算每個鍵出現的次數
db.userdatas.find()
{ "_id" : ObjectId("59789a56bc629e73c4f09e1c"), "name" : "wang wu", "age" : 45 }
{ "_id" : ObjectId("59789a74bc629e73c4f09e1e"), "name" : "wang wu", "age" : 8 }
{ "_id" : ObjectId("59789ac0bc629e73c4f09e20"), "name" : "wang wu", "age" : 33 }
{ "_id" : ObjectId("597f357a09c84cf58880e40e"), "name" : "u1", "age" : 37 }
{ "_id" : ObjectId("597f357a09c84cf58880e40f"), "name" : "u1", "age" : 37 }
{ "_id" : ObjectId("597f357a09c84cf58880e410"), "name" : "u5", "age" : 78 }
{ "_id" : ObjectId("597f357a09c84cf58880e412"), "name" : "u3", "age" : 32 }
{ "_id" : ObjectId("597f357a09c84cf58880e411"), "name" : "u4", "age" : 30, "score" : [ 7, 4, 2, 0 ] }
{ "_id" : ObjectId("597fcc0f411f2b2fd30d0b3f"), "age" : 20, "score" : [ 7, 4, 2, 0, 10, 9, 8, 7 ], "name" : "lihao" }
{ "_id" : ObjectId("597f357a09c84cf58880e413"), "name" : "u2", "age" : 33, "wendang" : { "yw" : 80, "xw" : 90 } }
{ "_id" : ObjectId("5983f5c88eec53fbcd56a7ca"), "date" : ISODate("2017-08-04T04:19:20.693Z") }
var map = function(){
for(var key in this){
emit(key,{count:1});
}
}
var reduce = function(key,emits){
var total = 0;
for(var i in emits){
total += emits[i].count;
}
return {"count":total};
};
db.runCommand({"mapreduce":"userdatas","map":map,"reduce":reduce,"out":"result"});
{
"result" : "result",
"timeMillis" : 42,
"counts" : {
"input" : 11,
"emit" : 11,
"reduce" : 2,
"output" : 9
},
"ok" : 1
}
db.result.find()
{ "_id" : "_id", "value" : { "count" : 11 } }
{ "_id" : "age", "value" : { "count" : 10 } }
{ "_id" : "date", "value" : { "count" : 1 } }
{ "_id" : "name", "value" : { "count" : 10 } }
{ "_id" : "score", "value" : { "count" : 2 } }
{ "_id" : "wendang", "value" : { "count" : 1 } }
MongoDB MapReduce 可選參數
1:finalize:function :可以將reduce的結果發送到finalize,這是整個處理的最後一步
2:keeptemp:boolean :是否在連接關閉的時候,保存臨時結果集合
3:query:document :在發送給map前對文檔進行過濾
4:sort:document :在發送給map前對文檔進行排序
5:limit:integer :發往map函數的文檔數量上限
6:scope:document :可以在javascript中使用的變量
7:verbose:boolean :是否記錄詳細的服務器日誌
問題一 : 計算出文檔中含有age字段的總和
var map = function() {
emit("age",this.age)
}
var reduce = function(key,emits) {
var total = 0;
for(var i in emits) {
total += emits[i];
}
return total
}
var query = {"age":{"$exists":1}}
// var sort = {"age":1};
// var finalize = function(key,value){
// return {"mykey":key,"myV":value};
// };
db.runCommand({"mapreduce":"userdatas","map":map,"reduce":reduce,"out":"result","query":query})
{
"result" : "result",
"timeMillis" : 17,
"counts" : {
"input" : 10,
"emit" : 10,
"reduce" : 1,
"output" : 1
},
"ok" : 1
}
db.result.find()
{ "_id" : "age", "value" : 353 }