首页 > 代码库 > mapreduce运用
mapreduce运用
测试环境:192.168.1.55
mongo 192.168.1.55:30001
show dbs
use gwgps
测试目标,求出两个班的总数,人数,平均分数等。
可以根据不同的业务需求,定制map,reduce,finalize函数。
1. 数据准备
db.person1.insert({‘class‘:1,‘name‘:‘n1‘,age:5,score:90,nation:‘汉‘})
db.person1.insert({‘class‘:1,‘name‘:‘n2‘,age:6,score:92,nation:‘汉‘})
db.person1.insert({‘class‘:1,‘name‘:‘n3‘,age:5,score:92,nation:‘苗‘})
db.person1.insert({‘class‘:1,‘name‘:‘n4‘,age:8,score:96,nation:‘藏‘})
db.person1.insert({‘class‘:1,‘name‘:‘n5‘,age:8,score:98,nation:‘汉‘})
db.person1.insert({‘class‘:1,‘name‘:‘n6‘,age:9,score:98,nation:‘汉‘})
db.person1.insert({‘class‘:1,‘name‘:‘n7‘,age:4,score:91,nation:‘藏‘})
db.person1.insert({‘class‘:1,‘name‘:‘n8‘,age:8,score:96,nation:‘苗‘})
db.person1.insert({‘class‘:2,‘name‘:‘n9‘,age:9,score:95,nation:‘苗‘})
db.person1.insert({‘class‘:2,‘name‘:‘n10‘,age:9,score:96,nation:‘藏‘})
db.person1.insert({‘class‘:2,‘name‘:‘n11‘,age:9,score:92,nation:‘苗‘})
db.person1.insert({‘class‘:2,‘name‘:‘n12‘,age:8,score:91,nation:‘汉‘})
db.person1.insert({‘class‘:2,‘name‘:‘n13‘,age:7,score:99,nation:‘汉‘})
db.person1.insert({‘class‘:2,‘name‘:‘n14‘,age:7,score:98,nation:‘汉‘})
db.person1.insert({‘class‘:2,‘name‘:‘n15‘,age:2,score:99,nation:‘内蒙‘})
2.编写map函数
map=function(){
var val ={class:this.class,total_score:this.score,count:1,avg_score:0};
emit(this.class,val);
}
3.编写ruduce函数
reduce=function(key,values){
var reducedObject = {class: key,total_score: 0,count:0, avg_score:0};
for (var i = 0; i < values.length; i++) {
reducedObject.total_score += values[i].total_score;
reducedObject.count +=values[i].count;
}
if(reducedObject.count>0)
reducedObject.avg_score =reducedObject.total_score /reducedObject.count;
return reducedObject;
}
4.执行mapReduce函数,生成结果collection.
db.person1.mapReduce(map,reduce,{out: "r2"})
5.查询执行结果
mongos> db.r2.find() { "_id" : 1, "value" : { "class" : 1, "total_score" : 753, "count" : 8, "avg_score" : 94.125 } } { "_id" : 2, "value" : { "class" : 2, "total_score" : 670, "count" : 7, "avg_score" : 95.71428571428571 } }
mapreduce运用