快速學習mongodb的mapreduce例子

fmms 12年前發布 | 51K 次閱讀 MongoDB NoSQL數據庫

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,這里是先歸類,按頁數去劃分分類,如下:

> 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 然后再查看下,結果顯示為:
  > 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的驅動,代碼如下:
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(); } } }</pre>

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!