本文主要讲怎么在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()); } } }
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}}