本文主要讲怎么在mongodb中使用mapreduce,而不涉及Spring与Mongodb整合等内容,如想了解这些,请参考我的其它文章。
这个例子主要目的是:统计每个名字有多少人重名。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MapReduceOutput;
import com.zolcorp.mahout.model.Student;
import com.zolcorp.mahout.service.StudentService;
/**
* @author hadoop
*
*/
public class MapReduceMain {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext(
new String[] { "classpath:applicationContext-bean.xml",
"classpath:applicationContext-datasource.xml" });
StudentService studentService = context.getBean("studentService", StudentService.class);
studentService.add(new Student("ming", 25));
studentService.add(new Student("gang", 24));
studentService.add(new Student("ming", 24));
MongoTemplate mongoTemplate = context.getBean("mongoTemplate",
MongoTemplate.class);
DBCollection coll = mongoTemplate.getCollection("student");
String map = "function() { emit(this.name, {count:1});}";
String reduce = "function(key, values) {var total = 0;for(var i=0;i<values.length;i++){total += values[i].count;}return {count:total};}";
String result = "resultCollection";
MapReduceOutput mapReduceOutput = coll.mapReduce(map,
reduce.toString(), result, null);
DBCollection resultColl = mapReduceOutput.getOutputCollection();
DBCursor cursor = resultColl.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
}
我们在student这个集合中,加入了三条数据。Student.java如下:
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author hadoop
*
*/
@Document(collection="student")
public class Student {
private String id;
private String name;
private int age;
public Student()
{
this("", 0);
}
public Student(String name, int age)
{
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
插入数据后,库中的内容如下:
程序运行结果如下:
{ "_id" : "gang" , "value" : { "count" : 1.0}}
{ "_id" : "ming" , "value" : { "count" : 2.0}}
mapreduce的函数都是用javasript编写,还有如果想了解mapreduce的话,请参考其它的文档,这篇文档只是告诉大家,在mongodb中使用mapreduce的方法。如果读者用过hadoop的话的,应该能发现mangodb的mapreduce有很多优点,先不做阐述,以后再补充吧。