1. 程式人生 > >Hadoop streaming mapreduce多檔案輸入使用方法

Hadoop streaming mapreduce多檔案輸入使用方法

寫Mapreduce程式時,常常會有同時處理多個輸入檔案的的需求,那麼如何在map程式中方便的知道這一條record到底來自哪個檔案呢?如果資料來源是可控的,我們尚可以通過欄位的個數等來判斷,然而這不是一個優雅的方法,其實hadoop已經給了留了解決方法:在map端獲取環境變數map_input_file(0.21.x的版本分支中是mapreduce_map_input_file)即為本次的輸入檔案。 以筆者使用的hadoop streaming + python環境為例: 輸入檔案有兩個: -input "/home/hadoop/file1" -input "/home/hadoop/file2" 在mapper檔案中: if 'file1' in os.getenv('map_input_file'): //檔案1處理邏輯 if 'file2' in os.getenv('map_input_file'): //檔案2處理邏輯