Nosql Mongodb之旅(12)—MongoDB MapReduce

  MongDB的MapReduce相当于MySQL中的“group by”,所以在MongoDB上使用Map/Reduce进行并行“统计”很容易。

    使用MapReduce要实现两个函数Map函数和Reduce函数,Map函数调用emit(key,value),遍历collection中的所有记录,将key和value传递给Reduce函数进行处理。Map函数和Reduce函数可以使用JS来实现,可以通过db.runCommand或mapReduce命令来执行一个MapReduce操作。

    示例shell

[plain] view plaincopy
  1. db.runCommand(  
  2. { mapreduce : <collection>,  
  3. map : <mapfunction>,  
  4. reduce : <reducefunction>  
  5. [, query : <query filter object>]  
  6. [, sort : <sorts the input objects using this key. Useful for optimization, like sorting by the  
  7. emit key for fewer reduces>]  
  8. [, limit : <number of objects to return from collection>]  
  9. [, out : <see output options below>]  
  10. [, keeptemp: <true|false>]  
  11. [, finalize : <finalizefunction>]  
  12. [, scope : <object where fields go into javascript global scope >]  
  13. [, verbose : true]  
  14. }  
  15. );  

     参数说明:
     mapreduce: 要操作的目标集合。
     map: 映射函数 (生成键值对序列,作为 reduce 函数参数)。
     reduce: 统计函数。
     query: 目标记录过滤。
     sort: 目标记录排序。
     limit: 限制目标记录数量。
     out: 统计结果存放集合 (不指定则使用临时集合,在客户端断开后自动删除)。
     keeptemp: 是否保留临时集合。
     finalize: 最终处理函数 (对 reduce 返回结果进行最终整理后存入结果集合)。
     scope: 向 map、reduce、finalize 导入外部变量。
     verbose: 显示详细的时间统计信息。

     下面我们准备数据以备后面示例所需

[plain] view plaincopy
  1. > db.students.insert({classid:1, age:14, name:'Tom'})  
  2. > db.students.insert({classid:1, age:12, name:'Jacky'})  
  3. > db.students.insert({classid:2, age:16, name:'Lily'})  
  4. > db.students.insert({classid:2, age:9, name:'Tony'})  
  5. > db.students.insert({classid:2, age:19, name:'Harry'})  
  6. > db.students.insert({classid:2, age:13, name:'Vincent'})  
  7. > db.students.insert({classid:1, age:14, name:'Bill'})  
  8. > db.students.insert({classid:2, age:17, name:'Bruce'})  
  9. >  

    现在我们演示如何统计1班和2班的学生数量

    Map 函数必须调用 emit(key, value) 返回键值对,使用 this 访问当前待处理的 Document。

    这里this一定不能忘了!!! 

[plain] view plaincopy
  1. > m = function() { emit(this.classid, 1) }  
  2. function () {  
  3. emit(this.classid, 1);  
  4. }  
  5. >  

    value 可以使用 JSON Object 传递 (支持多个属性值)。例如:
    emit(this.classid, {count:1})
    Reduce 函数接收的参数类似 Group 效果,将 Map 返回的键值序列组合成 { key, [value1,value2, value3, value...] } 传递给 reduce。

[plain] view plaincopy
  1. > r = function(key, values) {  
  2. ... var x = 0;  
  3. ... values.forEach(function(v) { x += v });  
  4. ... return x;  
  5. ... }  
  6. function (key, values) {  
  7. var x = 0;  
  8. values.forEach(function (v) {x += v;});  
  9. return x;  
  10. }  
  11. >  

    Reduce 函数对这些 values 进行 "统计" 操作,返回结果可以使用 JSON Object。

    结果如下: 

[plain] view plaincopy
  1. > res = db.runCommand({  
  2. ... mapreduce:"students",  
  3. ... map:m,  
  4. ... reduce:r,  
  5. ... out:"students_res"  
  6. ... });  
  7. {  
  8. "result" : "students_res",  
  9. "timeMillis" : 1587,  
  10. "counts" : {  
  11. "input" : 8,  
  12. "emit" : 8,  
  13. "output" : 2  
  14. },  
  15. "ok" : 1  
  16. }  
  17. > db.students_res.find()  
  18. { "_id" : 1, "value" : 3 }  
  19. { "_id" : 2, "value" : 5 }  
  20. >  

    mapReduce() 将结果存储在 "students_res" 表中。

    利用 finalize() 我们可以对 reduce() 的结果做进一步处理。

[plain] view plaincopy
  1. > f = function(key, value) { return {classid:key, count:value}; }  
  2. function (key, value) {  
  3. return {classid:key, count:value};  
  4. }  
  5. >  

    我们再重新计算一次,看看返回的结果:

[plain] view plaincopy
  1. > res = db.runCommand({  
  2. ... mapreduce:"students",  
  3. ... map:m,  
  4. ... reduce:r,  
  5. ... out:"students_res",  
  6. ... finalize:f  
  7. ... });  
  8. {  
  9. "result" : "students_res",  
  10. "timeMillis" : 804,  
  11. "counts" : {  
  12. "input" : 8,  
  13. "emit" : 8,  
  14. "output" : 2  
  15. },  
  16. "ok" : 1  
  17. }  
  18. > db.students_res.find()  
  19. { "_id" : 1, "value" : { "classid" : 1, "count" : 3 } }  
  20. { "_id" : 2, "value" : { "classid" : 2, "count" : 5 } }  
  21. >  

    列名变与 “classid”和”count”了,这样的列表更容易理解。

    

    我们还可以添加更多的控制细节。

[plain] view plaincopy
  1. > res = db.runCommand({  
  2. ... mapreduce:"students",  
  3. ... map:m,  
  4. ... reduce:r,  
  5. ... out:"students_res",  
  6. ... finalize:f,  
  7. ... query:{age:{$lt:10}}  
  8. ... });  
  9. {  
  10. "result" : "students_res",  
  11. "timeMillis" : 358,  
  12. "counts" : {  
  13. "input" : 1,  
  14. "emit" : 1,  
  15. "output" : 1  
  16. },  
  17. "ok" : 1  
  18. }  
  19. > db.students_res.find();  
  20. { "_id" : 2, "value" : { "classid" : 2, "count" : 1 } }  
  21. >  

    可以看到先进行了过滤,只取age<10 的数据,然后再进行统计,所以就没有1 班的统计数据了。

你可能感兴趣的:(mongodb,数据库,NoSQL,nosql数据库)