Hadoop的InputFormat和OutputFormat

来源:互联网 发布:企业域名实名认证 编辑:程序博客网 时间:2024/05/19 17:57

一 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.