BigData————MapReduce组件InputFormat

思考:在运行MapReduce程序时,输入的文件格式包括:基于行的日志文件、二进制格式文件、数据库表等。那么,针对不同的数据类型,MapReduce是如何读取这些数据的呢?

input format:输入格式化

FileInputFormat常见的接口实现类包括:

TextInputFormat、KeyValueTextInputFormat、NLineInputFomat、CombineTestInputFormat和自定义InputFormat等。

1. TextInputFormat

TextInputFormat是默认的FileInputFormat实现类。按行读取每条记录。键是存储该行在整个文件中的起始字节偏移量,LongWritable类型。 值是这行的内容,不包括任何行终止符(换行符和回车符),Text类型.

内容:
R1ch lear ning form
Intelligent lear ning engine 
Learning more co nvenient
From the real demand for more close to the enterprise
(k,v):
(0, Rich learning form)
(19, Inte 11 igent learning engine)
(47 ,Learning more convenient)
(72 ,From the real demand for more close to the enter prise)

2. KeyValueTextInputFormat

每一行均为一条记录,被分隔符分割为key,value。可以通过在驱动类中设置conf. set(KeyValueLineRecordReaderKEY_VALUE_SEPERATOR, "\t");来设定分隔符。默认分隔符是tab (t)。

以下是一 个示例,输入是一 个包含4条记录的分片。其中一>表示-个(水平方向的)制表符。
line1-- >Rich learning form
line2--->Intel ligent learning engine
line3--->Learning more convenient
line4--->From the real demand for more close to the enterprise
(k,v):
(line1,Rich learning form)
(line2, Intelligent learning engine)
(line3, Learning more convenient)
(line4, From the real demand for more close to the enterprise)

 

3 . NLinelnputFormat

如果使用NlineInputFormat,代表每个mp进程处理的InputSplit不再按B1ock块去划分,而是按NIinelnpuFormat指定的行数N来划分。即输入文件的总行数N=切片数,如果不整除,切片数=商+1。

R1ch lear ning fo rm
Intelligent lear ning engine 
Learning more co nvenient
From the real demand for more close to the enterprise

例如,如果N是2,则每个输入分片包含两行。开启2个MapTask.
(0, Rich learning form)
(19,ntelligent 1earmning engine)

另一个mapper则收到后两行: 
( 47, Lear ning more convenient)
( 72,From the real demand for more close to the enterprise)
这里的键和值与TextInputFormat生成的一样。

4 . Combine TextInputFormat

框架默认的TextInputFormat切片机制是对任务按文件规划切片,不管文件多少,都会是一个单独的切片,都会交给一个MapTask,这样如果有大量小文件,就会产生大量的MapTask,处理效率极其低下。

1应用场景:

CombineTextInputFormnat用于小文件过多的场景,它可以将多个小文件从逻辑上规划到一个切片中,这样,多个小文件就可以交给一个MapTask处理。

2虚拟存储切片最大值设置

CombineTextInputF ormat.setMaxInputSplitSize(job, 4194304):// 4m

注意:虚拟存储切片最大值设置最好根据实际小文件大小情况来设置具体的值。

3切片机制

生成切片过程包括:虚拟存储过程和切片过程两部分。

BigData————MapReduce组件InputFormat_第1张图片

inpinputformat

BigData————MapReduce组件InputFormat_第2张图片

 

自定义InputFormat介绍

       一个InputFormat是将文件切片----->再转化为对转交给Mapper处理。

  所以我们看到在InputFormat类中只有两个方法,一个负责切片,一个返回能将切片信息转化为相应的键值对的对象。

       我们了解到mapper有输入的数据类型,分别是key/value,key的数据类型是LongWritable类型,value是text类型。那么key/value是怎么来的呢。这里将进行自定义的方式来介绍InputFormat,因为数据原因这里的输出的数据类型是还是LongWritable/text,主要了解这个mapper的取值过程以及以后自定义MyInputFormat。

 

无论HDFS还是MapReduce,在处理小文件时效率都非常低,但又难免面临处理大量小文件的场景,此时,就需要有相应解决方案。可以自定义InputFormat实现小文件的合并。

 

输入数据

                1.txt   2.txt  3.txt

期望输出文件格式

                 part-r-0000

你可能感兴趣的:(mapreduce,hadoop)