免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3596 | 回复: 1
打印 上一主题 下一主题

[MongoDB] 快速例子学习mongodb的mapreduce [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-19 00:22 |只看该作者 |倒序浏览

快速例子学习mongodb的mapreduce


mongodbjavamongomapreduce.

map和reduce是十分有用的操作,特别是在NOSQL中.本文简单小结下
在mongodb中对mapreduce的操作,以及在JAVA中如何操作.

1 启动mongodb
   mongo启动即可

2 建立db
   use test

3 加点记录
  1.    > book1 = {name : "Understanding JAVA", pages : 100}
  2. > book2 = {name : "Understanding JSON", pages : 200}
  3.    > db.books.save(book1)
  4. > db.books.save(book2)
  5.   继续加
  6.   > book = {name : "Understanding XML", pages : 300}
  7. > db.books.save(book)
  8. > book = {name : "Understanding Web Services", pages : 400}
  9. > db.books.save(book)
  10. > book = {name : "Understanding Axis2", pages : 150}
  11. > db.books.save(book)
复制代码
4  先来做MAP,这里是先归类,按页数去划分分类,如下:
   


Java代码
  1. 1.> var map = function() {  
  2. 2.var category;  
  3. 3.if ( this.pages >= 250 )   
  4. 4.category = 'Big Books';  
  5. 5.else   
  6. 6.category = "Small Books";  
  7. 7.emit(category, {name: this.name});  
  8. 8.};  
复制代码
5 然后再按reduce来统计个数
  


Java代码
  1. 1.> var reduce = function(key, values) {  
  2. 2.var sum = 0;  
  3. 3.values.forEach(function(doc) {  
  4. 4.sum += 1;  
  5. 5.});  
  6. 6.return {books: sum};  
  7. 7.};  
复制代码
6 然后再查看下,结果显示为:
  > var count  = db.books.mapReduce(map, reduce, {out: "book_results"});
> db[count.result].find()

{ "_id" : "Big Books", "value" : { "books" : 2 } }
{ "_id" : "Small Books", "value" : { "books" : 3 } }

7 换用JAVA去实现之,注意下载mongodb的驱动,代码如下:



Java代码
  1. 1.import com.mongodb.BasicDBObject;  
  2. 2.import com.mongodb.DB;  
  3. 3.import com.mongodb.DBCollection;  
  4. 4.import com.mongodb.DBObject;  
  5. 5.import com.mongodb.MapReduceCommand;  
  6. 6.import com.mongodb.MapReduceOutput;  
  7. 7.import com.mongodb.Mongo;  
  8. 8.  
  9. 9.public class MongoClient {  
  10. 10.  
  11. 11. /**
  12. 12.  * @param args
  13. 13.  */  
  14. 14. public static void main(String[] args) {  
  15. 15.  
  16. 16.  Mongo mongo;  
  17. 17.   
  18. 18.  try {  
  19. 19.   mongo = new Mongo("localhost", 27017);  
  20. 20.   DB db = mongo.getDB("library");  
  21. 21.  
  22. 22.   DBCollection books = db.getCollection("books");  
  23. 23.  
  24. 24.   BasicDBObject book = new BasicDBObject();  
  25. 25.   book.put("name", "Understanding JAVA");  
  26. 26.   book.put("pages", 100);  
  27. 27.   books.insert(book);  
  28. 28.     
  29. 29.   book = new BasicDBObject();   
  30. 30.   book.put("name", "Understanding JSON");  
  31. 31.   book.put("pages", 200);  
  32. 32.   books.insert(book);  
  33. 33.     
  34. 34.   book = new BasicDBObject();  
  35. 35.   book.put("name", "Understanding XML");  
  36. 36.   book.put("pages", 300);  
  37. 37.   books.insert(book);  
  38. 38.     
  39. 39.   book = new BasicDBObject();  
  40. 40.   book.put("name", "Understanding Web Services");  
  41. 41.   book.put("pages", 400);  
  42. 42.   books.insert(book);  
  43. 43.   
  44. 44.   book = new BasicDBObject();  
  45. 45.   book.put("name", "Understanding Axis2");  
  46. 46.   book.put("pages", 150);  
  47. 47.   books.insert(book);  
  48. 48.     
  49. 49.   String map = "function() { "+   
  50. 50.             "var category; " +   
  51. 51.             "if ( this.pages >= 250 ) "+   
  52. 52.             "category = 'Big Books'; " +  
  53. 53.             "else " +  
  54. 54.             "category = 'Small Books'; "+   
  55. 55.             "emit(category, {name: this.name});}";  
  56. 56.     
  57. 57.   String reduce = "function(key, values) { " +  
  58. 58.                            "var sum = 0; " +  
  59. 59.                            "values.forEach(function(doc) { " +  
  60. 60.                            "sum += 1; "+  
  61. 61.                            "}); " +  
  62. 62.                            "return {books: sum};} ";  
  63. 63.     
  64. 64.   MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,  
  65. 65.     null, MapReduceCommand.OutputType.INLINE, null);  
  66. 66.  
  67. 67.   MapReduceOutput out = books.mapReduce(cmd);  
  68. 68.  
  69. 69.   for (DBObject o : out.results()) {  
  70. 70.    System.out.println(o.toString());  
  71. 71.   }  
  72. 72.  } catch (Exception e) {  
  73. 73.   // TODO Auto-generated catch block  
  74. 74.   e.printStackTrace();  
  75. 75.  }  
  76. 76. }  
  77. 77.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-02-19 15:53 |只看该作者
有爱的楼主 希望与楼至多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP