Hadoop MapReduce 學習筆記(一) 序言和準備

終於踏入了Hadoop的世界,先學習了Sqoop,然後MapReduce.這裏結合MapReduce實現類似SQL的各種功能.如:max,min,order by,inner/left/right join group by等.但這裏只是我的一個學習的過程,還有很多不足和錯誤.但我會步步深入不斷改進,希望也能幫助到大家.同時今後也會不斷跟進,比如讀PIG/Hive的源碼,看他們如何組織,如何寫MapReduce.以及工作過程中一些實踐經驗和心得.畢竟這塊資料還是比較少,尤其是系統性的.

       這裏我先貼上幾個準備類,用於生成測試數據.以及答個測試框架.

     首先貼上測試父類,具體請看註釋:

 
Java代碼  收藏代碼

    package com.guoyun.hadoop.mapreduce.study;  
      
    import org.slf4j.Logger;  
    import org.slf4j.LoggerFactory;  
    /**
     * MapReduce 測試父類
     */  
    public abstract class MyMapReduceTest {  
        
      public static final Logger log=LoggerFactory.getLogger(MyMapReduceTest.class);  
      public static final String DEFAULT_INPUT_PATH="testDatas/mapreduce/MRInput";  
      public static final String DEFAULT_OUTPUT_PATH="testDatas/mapreduce/MROutput";  
      public static final String NEW_LINE="\r";  
      public static final int DEFAULT_LENGTH=1000;  
      protected String inputPath=DEFAULT_INPUT_PATH;    // hadoop input  
      protected String outputPath=DEFAULT_OUTPUT_PATH;  // hadoop output  
      protected boolean isGenerateDatas=false;          // 是否生成測試數據  
      protected long maxValue=Long.MIN_VALUE;           // 生成數的最大值,以便跟結果比較  
      protected long minValue=Long.MAX_VALUE;           // 生成數的最小值,以便跟結果比較  
        
        
      public MyMapReduceTest(long dataLength) throws Exception {  
        this(dataLength,DEFAULT_INPUT_PATH,DEFAULT_OUTPUT_PATH);  
      }  
        
      /**
       * 該構造方法不會自動生成數據
       * @param outputPath
       */  
      public MyMapReduceTest(String outputPath) {  
        this.outputPath=outputPath;  
      }  
        
      /**
       * 該構造方法不會自動生成數據,同時會重用input的輸入數據
       * @param outputPath
       */  
      public MyMapReduceTest(String inputPath,String outputPath) {  
        this.inputPath=inputPath;  
        this.outputPath=outputPath;  
      }  
        
        
      public MyMapReduceTest(long dataLength,String inputPath, String outputPath) throws Exception {  
        this.inputPath = inputPath;  
        this.outputPath = outputPath;  
        isGenerateDatas=true;  
        init(dataLength);  
      }  
      
      public String getInputPath() {  
        return inputPath;  
      }  
      
      public void setInputPath(String inputPath) {  
        this.inputPath = inputPath;  
      }  
      
      public String getOutputPath() {  
        return outputPath;  
      }  
      
      public void setOutputPath(String outputPath) {  
        this.outputPath = outputPath;  
      }  
      
      public long getMaxValue() {  
        return maxValue;  
      }  
      
      public void setMaxValue(long maxValue) {  
        this.maxValue = maxValue;  
      }  
        
      public long getMinValue() {  
        return minValue;  
      }  
      
      public void setMinValue(long minValue) {  
        this.minValue = minValue;  
      }  
      
      public boolean isGenerateDatas() {  
        return isGenerateDatas;  
      }  
        
      /**
       * 初始化,根據設置,會自動生成測試數據
       *  
       * @param length
       * @throws Exception
       */  
      private void init(long length) throws Exception{  
        if(isGenerateDatas){  
          generateDatas(length);  
        }  
      }  
        
      /**
       * 生成測試數據,寫入inputPath.
       * 根據不同的測試需要,由子類完成
       *  
       * @param length
       * @throws Exception
       */  
      protected abstract void generateDatas(long length) throws Exception;  
       
    } 

轉載請註明出處:http://guoyunsky.iteye.com/blog/1233707


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章