MapReduce去重

來源:互聯網
上載者:User

標籤:

一:背景

很多資料來源中的資料都是含有大量重複的,為此我們需要將重複的資料去掉,這也稱為資料的清洗,MapReduce從Map端到Reduce端的Shuffle過程天生就有去重的功能,但是這是對輸出的Key作為參照進行去重的。所以我們可以將Map端讀入Value作為Key輸出,就可以很方便的實現去重了。

 

二:技術實現

#需求 有兩個檔案file0和file1。將兩個檔案中的內容合并去重。

#file0的內容如下:

 

[java] view plain copy 
  1. 1  
  2. 1  
  3. 2  
  4. 2  
  5. 3  
  6. 3  
  7. 4  
  8. 4  
  9. 5  
  10. 5  
  11. 6  
  12. 6  
  13. 7  
  14. 8  
  15. 9  

file1的內容如下:

 

 

[java] view plain copy 
  1. 1  
  2. 9  
  3. 9  
  4. 8  
  5. 8  
  6. 7  
  7. 7  
  8. 6  
  9. 6  
  10. 5  
  11. 5  
  12. 4  
  13. 4  
  14. 2  
  15. 1  
  16. 2  


代碼實現:

 

 

[java] view plain copy 
  1. public class DistinctTest {  
  2.         // 定義輸入路徑  
  3.         private static final String INPUT_PATH = "hdfs://liaozhongmin:9000/distinct_file/*";  
  4.         // 定義輸出路徑  
  5.         private static final String OUT_PATH = "hdfs://liaozhongmin:9000/out";  
  6.   
  7.         public static void main(String[] args) {  
  8.   
  9.             try {  
  10.                 // 建立配置資訊  
  11.                 Configuration conf = new Configuration();  
  12.                   
  13.   
  14.                 // 建立檔案系統  
  15.                 FileSystem fileSystem = FileSystem.get(new URI(OUT_PATH), conf);  
  16.                 // 如果輸出目錄存在,我們就刪除  
  17.                 if (fileSystem.exists(new Path(OUT_PATH))) {  
  18.                     fileSystem.delete(new Path(OUT_PATH), true);  
  19.                 }  
  20.   
  21.                 // 建立任務  
  22.                 Job job = new Job(conf, DistinctTest.class.getName());  
  23.   
  24.                 //1.1   設定輸入目錄和設定輸入資料格式化的類  
  25.                 FileInputFormat.setInputPaths(job, INPUT_PATH);  
  26.                 job.setInputFormatClass(TextInputFormat.class);  
  27.   
  28.                 //1.2   設定自訂Mapper類和設定map函數輸出資料的key和value的類型  
  29.                 job.setMapperClass(DistinctMapper.class);  
  30.                 job.setMapOutputKeyClass(Text.class);  
  31.                 job.setMapOutputValueClass(Text.class);  
  32.   
  33.                 //1.3   設定分區和reduce數量(reduce的數量,和分區的數量對應,因為分區為一個,所以reduce的數量也是一個)  
  34.                 job.setPartitionerClass(HashPartitioner.class);  
  35.                 job.setNumReduceTasks(1);  
  36.   
  37.                 //1.4   排序  
  38.                 //1.5   歸約  
  39.                 job.setCombinerClass(DistinctReducer.class);  
  40.                 //2.1   Shuffle把資料從Map端拷貝到Reduce端。  
  41.                 //2.2   指定Reducer類和輸出key和value的類型  
  42.                 job.setReducerClass(DistinctReducer.class);  
  43.                 job.setOutputKeyClass(Text.class);  
  44.                 job.setOutputValueClass(Text.class);  
  45.   
  46.                 //2.3   指定輸出的路徑和設定輸出的格式化類  
  47.                 FileOutputFormat.setOutputPath(job, new Path(OUT_PATH));  
  48.                 job.setOutputFormatClass(TextOutputFormat.class);  
  49.   
  50.   
  51.                 // 提交作業 退出  
  52.                 System.exit(job.waitForCompletion(true) ? 0 : 1);  
  53.               
  54.             } catch (Exception e) {  
  55.                 e.printStackTrace();  
  56.             }  
  57.         }  
  58.   
  59.     public static class DistinctMapper extends Mapper<LongWritable, Text, Text, Text>{  
  60.         //定義寫出去的key和value  
  61.         private Text outKey = new Text();  
  62.         private Text outValue = new Text("");  
  63.         @Override  
  64.         protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {  
  65.             //把輸入的key作為value輸出(因為)  
  66.             outKey = value;  
  67.               
  68.             //把結果寫出去  
  69.             context.write(outKey, outValue);  
  70.         }  
  71.     }  
  72.       
  73.     public static class DistinctReducer extends Reducer<Text, Text, Text, Text>{  
  74.         @Override  
  75.         protected void reduce(Text key, Iterable<Text> value, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {  
  76.             //直接把key寫出去  
  77.             context.write(key, new Text(""));  
  78.         }  
  79.     }  
  80. }  

程式啟動並執行結果:

 

MapReduce去重

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.