MongoDB之Hadoop驅動介紹

來源:互聯網
上載者:User

1. 一些概念Hadoop是一套Apache開源的分散式運算架構,其中包括了Distributed File System與分散式運算模型MapReduce,而MongoDB是一個面向文檔的分散式資料庫,它是NoSql的一種,而這裡所要介紹的就是一個MongoDB的Hadoop驅動,這裡就是把MongoDB作為MapReduce的輸入源,充分利用MapReduce的優勢來對MongoDB的資料進行處理與計算。
2. MongoDB的Hadoop驅動目前這個版本的Hadoop驅動還是測試版本,還不能應用到實際的生產環境中去。
你可以從下面網址https://github.com/mongodb/mongo-hadoop下載到最新的驅動包,下面是它的一些依賴說明:

  • 目前推薦用最新的Hadoop 0.20.203版本,或者是用Cloudera CHD3還做
  • MongoDB的版本最好是用1.8+
  • 還有是MongoDB的java驅動必須是2.5.3+
它的一些特點:
  • 提供了一個Hadoop的Input和Output適配層,讀於對資料的讀入與寫出
  • 提供了大部分參數的可配置化,這些參數都可有XML設定檔來進行配置,你可以在設定檔中定義要查詢的欄位,查詢條件,排序策略等
目前還不支援的功能:
  • 目前還不支援多Sharding的來源資料讀取
  • 還不支援資料的split操作

3. 程式碼分析
運行其examples中的WordCount.java代碼

 
  1. // 事先在MongoDB的test資料庫的in表中加入的測試樣本,使用如下方法    
  2.    /** 
  3. * test.in db.in.insert( { x : "eliot was here" } ) db.in.insert( { x : 
  4. * "eliot is here" } ) db.in.insert( { x : "who is here" } ) = 
  5. */  
  6. ublic class WordCount {  
  7.   
  8.   
  9.    private static final Log log = LogFactory.getLog( WordCount.class );  
  10.   
  11.   
  12. // 這是一個Map操作   
  13.    public static class TokenizerMapper extends Mapper<Object, BSONObject, Text, IntWritable> {  
  14.   
  15.   
  16.        private final static IntWritable one = new IntWritable( 1 );  
  17.        private final Text word = new Text();  
  18.   
  19.   
  20.        public void map( Object key , BSONObject value , Context context ) throws IOException, InterruptedException{  
  21.   
  22.   
  23.            System.out.println( "key: " + key );  
  24.            System.out.println( "value: " + value );  
  25.   
  26.   
  27.         // 對詞進行按空格切分   
  28.            final StringTokenizer itr = new StringTokenizer( value.get( "x" ).toString() );  
  29.            while ( itr.hasMoreTokens() ) {  
  30.                word.set( itr.nextToken() );  
  31.                context.write( word, one ); // 這裡的key為詞,而value為1   
  32.            }  
  33.        }  
  34.    }  
  35.   
  36.   
  37. // 這是Reduce操作,用於計算詞出現的頻率   
  38.    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {  
  39.   
  40.   
  41.        private final IntWritable result = new IntWritable();  
  42.   
  43.   
  44.        public void reduce( Text key , Iterable<IntWritable> values , Context context ) throws IOException, InterruptedException{  
  45.   
  46.   
  47.         // 計算詞出現的頻率,把相同詞的value相加   
  48.            int sum = 0;  
  49.            for ( final IntWritable val : values ) {  
  50.                sum += val.get();  
  51.            }  
  52.            result.set( sum );  
  53.            context.write( key, result ); // key為單個詞,value為這個詞所對應的詞頻率   
  54.        }  
  55.    }  
  56.   
  57.   
  58.    public static void main( String[] args ) throws Exception{  
  59.   
  60.   
  61.        final Configuration conf = new Configuration();  
  62.     // 定義MongoDB資料庫的輸入與輸出表名,這裡是調用本地的MongoDB,預設連接埠號碼為27017   
  63.        MongoConfigUtil.setInputURI( conf, "mongodb://localhost/test.in" );  
  64.        MongoConfigUtil.setOutputURI( conf, "mongodb://localhost/test.out" );  
  65.        System.out.println( "Conf: " + conf );  
  66.   
  67.   
  68.        final Job job = new Job( conf , "word count" );  
  69.   
  70.   
  71.        job.setJarByClass( WordCount.class );  
  72.   
  73.   
  74.     // 定義Mapper,Reduce與Combiner類   
  75.        job.setMapperClass( TokenizerMapper.class );  
  76.   
  77.   
  78.        job.setCombinerClass( IntSumReducer.class );  
  79.        job.setReducerClass( IntSumReducer.class );  
  80.   
  81.   
  82.     // 定義Mapper與Reduce的輸出key/value的類型   
  83.        job.setOutputKeyClass( Text.class );  
  84.        job.setOutputValueClass( IntWritable.class );  
  85.   
  86.   
  87.     // 定義InputFormat與OutputFormat的類型   
  88.        job.setInputFormatClass( MongoInputFormat.class );  
  89.        job.setOutputFormatClass( MongoOutputFormat.class );  
  90.   
  91.   
  92.        System.exit( job.waitForCompletion( true ) ? 0 : 1 );  
  93.    }  
  94.       
4. 分塊機制的簡單介紹

這裡沒有實現對不同shard的split操作,也就是說,對於分布在不同shard上的資料,只會產生一個Map操作。
這裡本人提供了一個分區的思路,有興趣的可以討論一下。

我們知道,對於Collection分塊後,會產生一個Config資料庫,在這個資料庫下有一個叫做chunks的表,其中每個chunk記錄了start_row與end_row,而這些chunk可以分布在不同的shard上,我們可以通過分析這個Collection來得到每個shard上的chunk資訊,從而把每個shard上的chunk資訊組合成一個InputSplit,這就是這裡的MongoInputSplit,這樣的話,只要去修改MongoInputFormat這個類的getSplits這個方法,加入對chunks表的分析,得到shard的資訊,這樣就可以實現多split的Map操作,對於不同的Shard,每個Map都會調用本地的Mongos代理服務,這樣就實現了移動計算而不是移動資料的目的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.