Mongodb MapReduce

Java代码  

  1. package com.iminido.nosql;  
  2.   
  3. import com.iminido.ssdb.HMap;  
  4. import com.mongodb.BasicDBObject;  
  5. import com.mongodb.DB;  
  6. import com.mongodb.DBCollection;  
  7. import com.mongodb.DBObject;  
  8. import com.mongodb.MapReduceCommand;  
  9. import com.mongodb.MapReduceOutput;  
  10. import com.mongodb.WriteConcern;  
  11. import java.net.UnknownHostException;  
  12.   
  13. /** 
  14.  * 
  15.  * @author JadeLuo 
  16.  */  
  17. public class NoSqlDb  extends Mdb {  
  18.       static DB db = null;  
  19.     public static void main(String[] args)  {  
  20. //        NoSqlDb.insert(“collectionName”, “{\”_id\”:\”id1\”,\”key\”:\”value\”}”);  
  21. //        NoSqlDb.save(“collectionName”, HMap.init().add(“_id”, “550cd6d7f21296bf9dc51bb9”).add(“key”, “222”));  
  22. //        NoSqlDb.save(“collectionName”, HMap.init().add(“_id”, “550cd6d7f21296bf9dc51bb9”).add(“key”, “222”));  
  23.         System.out.print(NoSqlDb.query(“collectionName”));  
  24.         NoSqlDb.remove(“collectionName”);  
  25.         System.out.print(NoSqlDb.query(“collectionName”));  
  26.           
  27.           
  28. //        initCollection2(“msg”, “temp2”).insert(new BasicDBObject().append(“k”, “vv”), WriteConcern.FSYNCED);  
  29. //        System.out.print( initCollection2(“msg”, “temp2”).find().toArray());   
  30.     }  
  31.       public static void mapReduce() throws UnknownHostException{    
  32. //        Mongo mongo = new Mongo(“localhost”, 27017);    
  33. //        DB db = mongo.getDB(“zhongsou_ad”);    
  34.          
  35.         /***  
  36.          *  book1 = {name : “Understanding JAVA”, pages : 100}  
  37.          *  book2 = {name : “Understanding JSON”, pages : 200}  
  38.          *  db.books.save(book1)  
  39.          *  db.books.save(book2)  
  40.          *  book = {name : “Understanding XML”, pages : 300}  
  41.          *  db.books.save(book)  
  42.          *  book = {name : “Understanding Web Services”, pages : 400}  
  43.          *  db.books.save(book)  
  44.          *  book = {name : “Understanding Axis2”, pages : 150}  
  45.          *  db.books.save(book)    
  46.          *    
  47.         var map = function() {  
  48.             var category;  
  49.             if ( this.pages >= 250 )  
  50.                 category = ‘Big Books’;  
  51.             else  
  52.                 category = “Small Books”;  
  53.             emit(category, {name: this.name});  
  54.         };  
  55.         var reduce = function(key, values) {  
  56.             var sum = 0;  
  57.             values.forEach(function(doc) {  
  58.                 sum += 1;  
  59.             });  
  60.             return {books: sum};  
  61.         };         
  62.         var count  = db.books.mapReduce(map, reduce, {out: “book_results”});  
  63.          */    
  64.         try {    
  65.     
  66.             DBCollection books = db.getCollection(“books”);    
  67.     
  68.             BasicDBObject book = new BasicDBObject();    
  69.             book.put(“name”“Understanding JAVA”);    
  70.             book.put(“pages”100);    
  71.             books.insert(book);    
  72.                 
  73.             book = new BasicDBObject();      
  74.             book.put(“name”“Understanding JSON”);    
  75.             book.put(“pages”200);    
  76.             books.insert(book);    
  77.                 
  78.             book = new BasicDBObject();    
  79.             book.put(“name”“Understanding XML”);    
  80.             book.put(“pages”300);    
  81.             books.insert(book);    
  82.                 
  83.             book = new BasicDBObject();    
  84.             book.put(“name”“Understanding Web Services”);    
  85.             book.put(“pages”400);    
  86.             books.insert(book);    
  87.               
  88.             book = new BasicDBObject();    
  89.             book.put(“name”“Understanding Axis2”);    
  90.             book.put(“pages”150);    
  91.             books.insert(book);    
  92.                 
  93.             String map = “function() { “+     
  94.                       “var category; “ +      
  95.                       “if ( this.pages >= 250 ) “+      
  96.                       “category = ‘Big Books’; “ +    
  97.                       “else “ +    
  98.                       “category = ‘Small Books’; “+      
  99.                       “emit(category, {name: this.name});}”;    
  100.                 
  101.             String reduce = “function(key, values) { “ +    
  102.                                      “var sum = 0; “ +    
  103.                                      “values.forEach(function(doc) { “ +    
  104.                                      “sum += 1; “+    
  105.                                      “}); “ +    
  106.                                      “return {books: sum};} “;    
  107.                 
  108.             MapReduceCommand cmd = new MapReduceCommand(books, map, reduce,    
  109.               null, MapReduceCommand.OutputType.INLINE, null);    
  110.     
  111.             MapReduceOutput out = books.mapReduce(cmd);    
  112.     
  113.             for (DBObject o : out.results()) {    
  114.              System.out.println(o.toString());    
  115.             }    
  116.            } catch (Exception e) {    
  117.              e.printStackTrace();    
  118.            }    
  119.     }    
  120.         
  121.         
  122. //public class NoSqlDb extends Squ{  
  123.       
  124. //    public DBCollection getDBCollection(String db, String collect) {  
  125. //        String host = Const.MONGODB_HOST;// 主机名  
  126. //        int port = Const.MONGODB_PORT;// 端口  
  127. //        MongoClient mg = null;  
  128. //        try {  
  129. //            mg = new MongoClient(host, port);  
  130. //        } catch (UnknownHostException ex) {  
  131. //            Logger.getLogger(NoSqlDb.class.getName()).log(Level.SEVERE, null, ex);  
  132. //        }  
  133. //        DBCollection clg = mg.getDB(db).getCollection(collect);  
  134. //        return clg;  
  135. //    }  
  136. }  
    原文作者:MapReduce
    原文地址: https://yq.aliyun.com/articles/46998
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞