shuaiwei588 发表于 2015-7-10 07:51:17

MongoDB数据库的MapReduce简单操作(转)

  MongoDB也简单的实现了MapReduce的功能来提供分布式的数据查询服务,MapReduce的分布是功能主要用在Shard上


db.runCommand(
{ mapreduce : ,
    map : ,
    reduce :
    [, query : ]
    [, sort : ]
    [, limit : ]
    [, out : ]
    [, keeptemp: ]
    [, finalize : ]
    [, scope : ]
    [, verbose : true]
}
);  下面是对MapReduce的简单测试
此例子来源于:http://www.mongodb.org/display/DOCS/MapReduce


> db.things.insert({_id:1,tags:['dog','cat']});                                                                                    
> db.things.insert({_id:2,tags:['cat']});      
> db.things.insert({_id:3,tags:['mouse','cat','dog']});
> db.things.insert({_id:4,tags:[]});                  
> m = function(){
... this.tags.forEach(
...   function(z){      
...             emit(z,{count:1});
...   }
...);
};
function () {
   this.tags.forEach(function (z) {emit(z, {count:1});});
}
> r=function(key,values){
... var total = 0;
... for(var i=0;i res=db.things.mapReduce(m,r);
{
         "result" : "tmp.mr.mapreduce_1268577545_1",
         "timeMillis" : 25,
         "counts" : {
               "input" : 4,
               "emit" : 6,
               "output" : 3
         },
         "ok" : 1,
         "ok" : 1,
}
> res
{
         "result" : "tmp.mr.mapreduce_1268577545_1",
         "timeMillis" : 25,
         "counts" : {
               "input" : 4,
               "emit" : 6,
               "output" : 3
         },
         "ok" : 1,
         "ok" : 1,
}

> db.find()
{ "_id" : "cat", "value" : { "count" : 3 } }
{ "_id" : "dog", "value" : { "count" : 2 } }
{ "_id" : "mouse", "value" : { "count" : 1 } }
> db.drop()
true
> db.find()
>  以下有几个MapReduce的参考例子:
http://www.mongodb.org/display/DOCS/MapReduce
http://github.com/mongodb/mongo/ ... sts/mr_bigobject.js
http://github.com/mongodb/mongo/blob/master/jstests/mr5.js
http://github.com/mongodb/mongo/blob/master/jstests/mr4.js
http://github.com/mongodb/mongo/blob/master/jstests/mr3.js
http://github.com/mongodb/mongo/blob/master/jstests/mr2.js
http://github.com/mongodb/mongo/blob/master/jstests/mr1.js
页: [1]
查看完整版本: MongoDB数据库的MapReduce简单操作(转)