使用Mongo Shell和Java驱动程序的MongoDB Map Reduce示例

Map Reduce is a data processing technique that condenses large volumes of data into aggregated results. MongoDB mapreduce command is provided to accomplish this task.

Map Reduce是一种数据处理技术,可将大量数据压缩为汇总结果。 提供MongoDB mapreduce命令来完成此任务。

Lets consider the following examples that demonstrates the mapreduce command usage.

让我们考虑以下示例,这些示例演示了mapreduce命令的用法。

Consider the car collection which contains the following documents;

考虑包含以下文件的汽车收藏;

>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:"India",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" : [ ]
})
>

Now let’s write the map reduce function on car collection grouping by speed and categorizing them as overspeed cars.

现在,让我们通过速度并将其归类为超速汽车来对汽车收藏分组编写map reduce功能。

Define the map function as shown below

定义地图功能,如下所示

>var speedmap = function (){
var criteria;
if ( this.speed > 70 ) {
criteria = 'overspeed';
emit(criteria,this.speed);
}
};

This function categorizes the car as overspeed cars based on the speed. Here “this” refers to the current document for which map reducing has to be processed.

此功能根据速度将汽车分类为超速汽车。 这里的“ this”指的是当前文档,必须对其进行地图缩小处理。

Define the reduce function with arguments key and values to caluclate the average speed of the overspeed car as

使用参数键和值定义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;
};
>

Here the speed is summed up for all the cars through iterating the loop and the average speed is calculated as the sum of all the speed by the number of overspeed cars.

在这里,通过迭代循环将所有汽车的速度相加,并且将平均速度计算为所有速度与超速汽车数量的总和。

Invoke the map reduce function by calling the Map and Reduce functions on all the documents present in the car collection as;

通过以以下方式调用car集合中存在的所有文档上的Map and Reduce函数来调用map reduce函数;

>var ret = db.car.mapReduce(speedmap, avgspeed_reducemap, {out: "avgspeed"});

The output is fetched in a collection avgspeed.If this collection does not exist a new collection is created else the new contents are replaced.

输出在avgspeed集合中获取。如果此集合不存在,则会创建一个新集合,否则将替换新内容。

To see the documents invoke db.avgspeed.find()

要查看文档,请调用db.avgspeed.find()

Output:

输出:

{ "_id" : "overspeed", "value" : 85 }

The output states that there average speed of the overspeed cars is 85.

输出显示超速汽车的平均速度为85。

MongoDB Map Reduce Java示例 (MongoDB Map Reduce Java Example)

Below is the java program for above mongo shell example, note that it’s just showcasing the Map Reduce functions working. So make sure data is present in the collection for it to give desired result.

下面是上面mongo shell示例的Java程序,请注意,它只是展示了Map Reduce函数的工作方式。 因此,请确保数据存在于集合中以使其达到期望的结果。

package com.journaldev.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("journaldev");

		// 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());

		}

	}

}

Above java program produces following output.

上面的java程序产生以下输出。

{ "_id" : "overspeed" , "value" : 85.0}

That’s all for a brief overview of Map Reduce functions in the MongoDB database, we will look other MongoDB features in coming posts.

以上就是对MongoDB数据库中Map Reduce函数的简要概述,我们将在以后的文章中介绍MongoDB的其他功能。

翻译自: https://www.journaldev.com/6322/mongodb-map-reduce-example-using-mongo-shell-and-java-driver

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值