快速例子学习mongodb的mapreduce

快速例子学习mongodb的mapreduce

博客分类: 数据库相关/oracle
mongodbjavamongomapreduce
map和reduce是十分有用的操作,特别是在NOSQL中.本文简单小结下 

在mongodb中对mapreduce的操作,以及在JAVA中如何操作.

1启动mongodb

mongo启动即可

2建立db

usetest

3加点记录

>book1={name:"UnderstandingJAVA",pages:100}

>book2={name:"UnderstandingJSON",pages:200}

>db.books.save(book1)

>db.books.save(book2)

继续加

>book={name:"UnderstandingXML",pages:300}

>db.books.save(book)

>book={name:"UnderstandingWebServices",pages:400}

>db.books.save(book)

>book={name:"UnderstandingAxis2",pages:150}

>db.books.save(book)

4先来做MAP,这里是先归类,按页数去划分分类,如下:

  
> var map = function() {  
var category;  
if ( this.pages >= 250 )   
category = 'Big Books';  
else   
category = "Small Books";  
emit(category, {name: this.name});  
};  

5然后再按reduce来统计个数

 
> var reduce = function(key, values) {  
var sum = 0;  
values.forEach(function(doc) {  
sum += 1;  
});  
return {books: sum};  
};  

6然后再查看下,结果显示为:

>varcount=db.books.mapReduce(map,reduce,{out:"book_results"});

>db[count.result].find()

{"_id":"BigBooks","value":{"books":2}}

{"_id":"SmallBooks","value":{"books":3}}

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

import com.mongodb.BasicDBObject;  
import com.mongodb.DB;  
import com.mongodb.DBCollection;  
import com.mongodb.DBObject;  
import com.mongodb.MapReduceCommand;  
import com.mongodb.MapReduceOutput;  
import com.mongodb.Mongo;  
  
public class MongoClient {  
  
 /** 
  * @param args 
  */  
 public static void main(String[] args) {  
  
  Mongo mongo;  
    
  try {  
   mongo = new Mongo("localhost", 27017);  
   DB db = mongo.getDB("library");  
  
   DBCollection books = db.getCollection("books");  
  
   BasicDBObject book = new BasicDBObject();  
   book.put("name", "Understanding JAVA");  
   book.put("pages", 100);  
   books.insert(book);  
     
   book = new BasicDBObject();    
   book.put("name", "Understanding JSON");  
   book.put("pages", 200);  
   books.insert(book);  
     
   book = new BasicDBObject();  
   book.put("name", "Understanding XML");  
   book.put("pages", 300);  
   books.insert(book);  
     
   book = new BasicDBObject();  
   book.put("name", "Understanding Web Services");  
   book.put("pages", 400);  
   books.insert(book);  
   
   book = new BasicDBObject();  
   book.put("name", "Understanding Axis2");  
   book.put("pages", 150);  
   books.insert(book);  
     
   String map = "function() { "+   
             "var category; " +    
             "if ( this.pages >= 250 ) "+    
             "category = 'Big Books'; " +  
             "else " +  
             "category = 'Small Books'; "+    
             "emit(category, {name: this.name});}";  
     
   String reduce = "function(key, values) { " +  
                            "var sum = 0; " +  
                            "values.forEach(function(doc) { " +  
                            "sum += 1; "+  
                            "}); " +  
                            "return {books: sum};} ";  
     
   MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,  
     null, MapReduceCommand.OutputType.INLINE, null);  
  
   MapReduceOutput out = books.mapReduce(cmd);  
  
   for (DBObject o : out.results()) {  
    System.out.println(o.toString());  
   }  
  } catch (Exception e) {  
   // TODO Auto-generated catch block  
   e.printStackTrace();  
  }  
 }  
}  

相关推荐