快速例子学习mongodb的mapreduce
快速例子学习mongodb的mapreduce
mongodbjavamongomapreduce.
map和reduce是十分有用的操作,特别是在NOSQL中.本文简单小结下
在mongodb中对mapreduce的操作,以及在JAVA中如何操作.
1 启动mongodb
mongo启动即可
2 建立db
use test
3 加点记录 > book1 = {name : "Understanding JAVA", pages : 100}
> book2 = {name : "Understanding JSON", pages : 200}
> db.books.save(book1)
> db.books.save(book2)
继续加
> book = {name : "Understanding XML", pages : 300}
> db.books.save(book)
> book = {name : "Understanding Web Services", pages : 400}
> db.books.save(book)
> book = {name : "Understanding Axis2", pages : 150}
> db.books.save(book) 4先来做MAP,这里是先归类,按页数去划分分类,如下:
Java代码1.> var map = function() {
2.var category;
3.if ( this.pages >= 250 )
4.category = 'Big Books';
5.else
6.category = "Small Books";
7.emit(category, {name: this.name});
8.};
5 然后再按reduce来统计个数
Java代码1.> var reduce = function(key, values) {
2.var sum = 0;
3.values.forEach(function(doc) {
4.sum += 1;
5.});
6.return {books: sum};
7.};6 然后再查看下,结果显示为:
> var count= db.books.mapReduce(map, reduce, {out: "book_results"});
> db.find()
{ "_id" : "Big Books", "value" : { "books" : 2 } }
{ "_id" : "Small Books", "value" : { "books" : 3 } }
7 换用JAVA去实现之,注意下载mongodb的驱动,代码如下:
Java代码1.import com.mongodb.BasicDBObject;
2.import com.mongodb.DB;
3.import com.mongodb.DBCollection;
4.import com.mongodb.DBObject;
5.import com.mongodb.MapReduceCommand;
6.import com.mongodb.MapReduceOutput;
7.import com.mongodb.Mongo;
8.
9.public class MongoClient {
10.
11. /**
12.* @param args
13.*/
14. public static void main(String[] args) {
15.
16.Mongo mongo;
17.
18.try {
19. mongo = new Mongo("localhost", 27017);
20. DB db = mongo.getDB("library");
21.
22. DBCollection books = db.getCollection("books");
23.
24. BasicDBObject book = new BasicDBObject();
25. book.put("name", "Understanding JAVA");
26. book.put("pages", 100);
27. books.insert(book);
28.
29. book = new BasicDBObject();
30. book.put("name", "Understanding JSON");
31. book.put("pages", 200);
32. books.insert(book);
33.
34. book = new BasicDBObject();
35. book.put("name", "Understanding XML");
36. book.put("pages", 300);
37. books.insert(book);
38.
39. book = new BasicDBObject();
40. book.put("name", "Understanding Web Services");
41. book.put("pages", 400);
42. books.insert(book);
43.
44. book = new BasicDBObject();
45. book.put("name", "Understanding Axis2");
46. book.put("pages", 150);
47. books.insert(book);
48.
49. String map = "function() { "+
50. "var category; " +
51. "if ( this.pages >= 250 ) "+
52. "category = 'Big Books'; " +
53. "else " +
54. "category = 'Small Books'; "+
55. "emit(category, {name: this.name});}";
56.
57. String reduce = "function(key, values) { " +
58. "var sum = 0; " +
59. "values.forEach(function(doc) { " +
60. "sum += 1; "+
61. "}); " +
62. "return {books: sum};} ";
63.
64. MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,
65. null, MapReduceCommand.OutputType.INLINE, null);
66.
67. MapReduceOutput out = books.mapReduce(cmd);
68.
69. for (DBObject o : out.results()) {
70. System.out.println(o.toString());
71. }
72.} catch (Exception e) {
73. // TODO Auto-generated catch block
74. e.printStackTrace();
75.}
76. }
77.} 有爱的楼主 希望与楼至多多交流
页:
[1]