天天看點

Hadoop的InputFormat和OutputFormat

一 InputFormat概述:

(1)InputFormat類:InputFormat用來描述和控制MapReduce Job的資料輸入。

(2)InputSplit(輸入分片):代表配置設定給單個map任務的資料。InputSplit存儲的并非資料本身,而是一個分片長度和一個記錄資料位置的數組,生成InputSplit的方法可通過InputFormat來設定。InputFormat的getSplits方法可以生成InputSplit相關資訊,包括兩部分:InputSplit中繼資料資訊和原始InputSplit資訊。InputSplit中繼資料資訊将被JobTracker使用,用以生成Task本地性相關資料結構;原始InputSplit資訊将被Map Task初始化時使用,用以擷取自己要處理的資料。

(3)map任務處理的資料是由InputFormat分解過的資料,InputFormat将資料集分割為輸入分片(InputSplit)。map會将分片傳送給InputFormat,InputFormat調用getRecordReader方法生成RecordReader,RecordReader再通過createKey、createValue方法建立可供Map處理的<key,value>。

(4)Hadoop預定義了多種方法将不同類型輸入資料轉化為Map能處理的<key,value>對(也可自定義),它們都繼承自InputFormat,分别是:

*DBInputFormat

*DelegatingInputFormat

*FIleInputFormat:CombineFileInputFormat, KeyValueTextInputFormat, NLineInputFormat,SequenceFileInputFormat, TextInputFormat。

二 OutputFormat概述:

(1)OutputFormat類:OutputFormat類描述和控制MapReduce Job的資料輸出。

(2)MapReduce架構需要OutputFormat做的工作:

*Validate the output-specification of the job. For e.g. check that the output directory doesn't already exist.

*Provide the RecordWriter implementation to be used to write out the output files of the job. Output files are stored in a FileSystem.

繼續閱讀