Mongodb MapReduce

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:
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. }  
相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
分布式计算 NoSQL JavaScript
初识Mongodb之MapReduce操作篇
初识Mongodb之MapReduce操作篇
293 0
初识Mongodb之MapReduce操作篇
|
分布式计算 JavaScript NoSQL
|
分布式计算 JavaScript NoSQL
|
分布式计算 NoSQL Hadoop
|
分布式计算 NoSQL 测试技术