FileInputFormat常用实现类

版权声明:www.hsboy.cn [email protected] https://blog.csdn.net/oHongShu1/article/details/88946836

切片数决定MapTask并行数,妥善选择切片方式,提高效率。

TextInputFormat(默认实现类)

默认按块大小进行切片,本地运行时默认的切片大小是32M。剩余大小小于切片1.1倍的时候,切成一片。更改默认切片大小时,若要小于当前块大小,更改最大值(maxsize);若要大于当前块大小,更改最小值(minsize)。
<key, value> <LongWritable, Text>

CombineTextInputFormat

解决小文件的方式之一,由于是虚拟切片,对物理块没有影响,所以没能从根本上解决小文件问题,每个文件依然占用NN150byte的内存空间。
切片方式(假设切片设置为4M):大于4M小于2*4M的,将其切为两片,每片各为一半。这样切分完肯定都是小于等于4M的,然后进行合并,若小于4M,将其与下一片合并。所以也是存在大于切片设置值的切片。

// 如果不设置InputFormat,它默认用的是TextInputFormat.class
job.setInputFormatClass(CombineTextInputFormat.class);
//虚拟存储切片最大值设置20m
CombineTextInputFormat.setMaxInputSplitSize(job, 20971520)

KeyValueTextInputFormat

每一行为一条记录,被分隔符分割为key、value

	Configuration conf = new Configuration();
		// 设置切割符
	conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR, " ");

<key, value> <Text, Text>

NLineInputFormat

不再按照block划分,而使用行数N进行切片

        // 设置每个切片InputSplit中划分三条记录
        NLineInputFormat.setNumLinesPerSplit(job, 3);  
        // 使用NLineInputFormat处理记录数  
        job.setInputFormatClass(NLineInputFormat.class);  

键和值与TextInputFormat一样

自定义InputFormat

以SequenceFile为例

1.自定义一个类继承FileInputFormat
(1)重写isSplitable()方法,返回false不可切割
(2)重写createRecordReader(),创建自定义的RecordReader对象,并初始化
2、改写RecordReader,实现一次读取一个完整文件封装为KV
(1)采用IO流一次读取一个文件输出到value中,因为设置了不可切片,最终把所有文件都封装到了value中
(2)获取文件路径信息+名称,并设置key
3、设置Driver

// (1)设置输入的inputFormat
job.setInputFormatClass(WholeFileInputformat.class);

// (2)设置输出的outputFormat
job.setOutputFormatClass(SequenceFileOutputFormat.class);

猜你喜欢

转载自blog.csdn.net/oHongShu1/article/details/88946836