快速例子学习mongodb的mapreduce

感谢有奉献精神的人

转自:http://jackyrong.iteye.com/blog/1408548

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[count.result].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. }  


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值