使用Mongo Shell和Java驱动程序的MongoDB Map Reduce示例
时间:2020-02-23 14:40:51 来源:igfitidea点击:
Map Reduce是一种数据处理技术,可将大量数据压缩为汇总结果。
提供MongoDB mapreduce命令可以完成此任务。
让我们考虑以下示例,这些示例演示了mapreduce命令的用法。
考虑包含以下文件的汽车集合;
>db.car.insert( [ {car_id:"c1",name:"Audi",color:"Black",cno:"H110",mfdcountry:"Germany",speed:72,price:11.25}, {car_id:"c2",name:"Polo",color:"White",cno:"H111",mfdcountry:"Japan",speed:65,price:8.5}, {car_id:"c3",name:"Alto",color:"Silver",cno:"H112",mfdcountry:"San Franceco",speed:53,price:4.5}, {car_id:"c4",name:"Santro",color:"Grey",cno:"H113",mfdcountry:"Sweden",speed:89,price:3.5} , {car_id:"c5",name:"Zen",color:"Blue",cno:"H114",mfdcountry:"Denmark",speed:94,price:6.5} ] ) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 5, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) >
现在,让我们通过对速度进行分类并将其归类为超速汽车,从而在汽车收藏分组中编写map reduce功能。
定义地图功能,如下所示
>var speedmap = function (){ var criteria; if ( this.speed > 70 ) { criteria = 'overspeed'; emit(criteria,this.speed); } };
此功能根据速度将汽车分类为超速汽车。
这里的" this"指的是当前文档,必须对其进行地图缩小处理。
使用参数键和值定义reduce函数,以计算超速汽车的平均速度为
>var avgspeed_reducemap = function(key, speed) { var total =0; for (var i = 0; i < speed.length; i++) { total = total+speed[i]; } return total/speed.length; }; >
其中通过迭代循环将所有汽车的速度相加,并且将平均速度计算为所有速度与超速汽车数量的总和。
通过以以下方式调用car集合中存在的所有文档上的Map and Reduce函数来调用map reduce函数:
>var ret = db.car.mapReduce(speedmap, avgspeed_reducemap, {out: "avgspeed"});
输出在avgspeed集合中获取。
如果此集合不存在,则会创建一个新集合,否则将替换新内容。
要查看文档,请调用db.avgspeed.find()。
输出:
{ "_id" : "overspeed", "value" : 85 }
输出显示超速汽车的平均速度为85。
MongoDB Map Reduce Java示例
下面是上面的mongo shell示例的Java程序,请注意,它只是展示了Map Reduce函数的工作原理。
因此,请确保数据存在于集合中以使其达到期望的结果。
package com.theitroad.mongodb; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.MapReduceCommand; import com.mongodb.MapReduceOutput; import com.mongodb.MongoClient; public class MongoDBMapReduce { public static void main(String[] args) throws UnknownHostException { //create an instance of client and establish the connection MongoClient m1 = new MongoClient(); //get the test db,use your own DB db = m1.getDB("theitroad"); //get the car collection DBCollection coll = db.getCollection("car"); //map function to categorize overspeed cars String carMap = "function (){" + "var criteria;" + "if ( this.speed > 70 ) {" + "criteria = 'overspeed';" + "emit(criteria,this.speed);" + "}" + "};"; //reduce function to add all the speed and calculate the average speed String carReduce = "function(key, speed) {" + "var total =0;" + "for (var i = 0; i < speed.length; i++) {" + "total = total+speed[i];" + "}" + "return total/speed.length;" + "};"; //create the mapreduce command by calling map and reduce functions MapReduceCommand mapcmd = new MapReduceCommand(coll, carMap, carReduce, null, MapReduceCommand.OutputType.INLINE, null); //invoke the mapreduce command MapReduceOutput cars = coll.mapReduce(mapcmd); //print the average speed of cars for (DBObject o : cars.results()) { System.out.println(o.toString()); } } }
上面的java程序产生以下输出。
{ "_id" : "overspeed" , "value" : 85.0}