Mongodb MapReduce

package com.iminido.nosql;

import com.iminido.ssdb.HMap;
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.WriteConcern;
import java.net.UnknownHostException;

/**
 *
 * @author JadeLuo
 */
public class NoSqlDb  extends Mdb {
      static DB db = null;
    public static void main(String[] args)  {
//        NoSqlDb.insert("collectionName", "{\"_id\":\"id1\",\"key\":\"value\"}");
//        NoSqlDb.save("collectionName", HMap.init().add("_id", "550cd6d7f21296bf9dc51bb9").add("key", "222"));
//        NoSqlDb.save("collectionName", HMap.init().add("_id", "550cd6d7f21296bf9dc51bb9").add("key", "222"));
        System.out.print(NoSqlDb.query("collectionName"));
        NoSqlDb.remove("collectionName");
        System.out.print(NoSqlDb.query("collectionName"));
        
        
//        initCollection2("msg", "temp2").insert(new BasicDBObject().append("k", "vv"), WriteConcern.FSYNCED);
//        System.out.print( initCollection2("msg", "temp2").find().toArray()); 
    }
      public static void mapReduce() throws UnknownHostException{  
//        Mongo mongo = new Mongo("localhost", 27017);  
//        DB db = mongo.getDB("zhongsou_ad");  
       
        /*** 
         *  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)   
         *   
        var map = function() { 
            var category; 
            if ( this.pages >= 250 ) 
                category = 'Big Books'; 
            else 
                category = "Small Books"; 
            emit(category, {name: this.name}); 
        }; 
        var reduce = function(key, values) { 
            var sum = 0; 
            values.forEach(function(doc) { 
                sum += 1; 
            }); 
            return {books: sum}; 
        };        
        var count  = db.books.mapReduce(map, reduce, {out: "book_results"}); 
         */  
        try {  
  
            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) {  
             e.printStackTrace();  
           }  
    }  
      
      
//public class NoSqlDb extends Squ{
    
//    public DBCollection getDBCollection(String db, String collect) {
//        String host = Const.MONGODB_HOST;// 主机名
//        int port = Const.MONGODB_PORT;// 端口
//        MongoClient mg = null;
//        try {
//            mg = new MongoClient(host, port);
//        } catch (UnknownHostException ex) {
//            Logger.getLogger(NoSqlDb.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        DBCollection clg = mg.getDB(db).getCollection(collect);
//        return clg;
//    }
}

 

你可能感兴趣的:(mapreduce)