Hadoop中的InputFormat解析
1、InputFormat
InputFormat是Hadoop平台上Mapreduce输入的规范,仅有两个抽象方法。
- List<InputSplit> getSplits(), 获取由输入文件计算出输入分片(InputSplit),解决数据或文件分割成片问题。
- RecordReader<K,V> createRecordReader(),创建RecordReader,从InputSplit中读取数据,解决读取分片中数据问题。
InputFormat主要能完成下列工作:
1、Validate the input-specification of the job. (首先验证作业的输入的正确性)
2、 Split-up the input file(s) into logical InputSplit
s, each of which is then assigned to an individual Mapper
.(将输入的文件划分成一系列逻辑分片
(InputSplit
),一个InputSplit将会被分配给一个独立的MapTask )
3、Provide the RecordReader
implementation to be used to glean input records from the logical InputSplit
for processing by the Mapper
.(提供RecordReader实
现,读取InputSplit中的“K-V对”供Mapper使用)
InputFormat的源代码:
- public abstract class InputFormat<K, V> {
- /**
- * 每个InputSplit的分片被分配到一个独立的Mapper上
- * 注:1、这个分片是逻辑上对输入数据进行分片,而实际上输入文件没有被切割成一个个小块。
- * 每个分片由输入文件的路径,起始位置,偏移量等
- * 2、在InputFormat中创建的RecordReader也要使用InputSplit
- */
- public abstract
- List<InputSplit> getSplits(JobContext context ) throws IOException, InterruptedException;
- /**
- * 为每个分片创建一个record reader
- */
- public abstract
- RecordReader<K,V> createRecordReader(InputSplit split,TaskAttemptContext context)
- throws IOException, InterruptedException;
2.InputSplit
Mapper输入的是一个个分片,称InputSplit。在这个抽象类中,是将每个输入分片(Split)中的内容解析成K-V值。
InputSplit的源代码:
- public abstract class InputSplit {
- /**
- * 得到每个分片的大小,可以按照分片大小排序。
- */
- public abstract long getLength() throws IOException, InterruptedException;
- /**
- * Get the list of nodes by name where the data for the split would be local.
- * The locations do not need to be serialized.
- * 获取存储该分片的数据所在的节点位置
- */
- public abstract String[] getLocations() throws IOException, InterruptedException;
- }
2.1 下面看看InputSplit的一个子类,FileSplit类:
- public FileSplit() {}
- /** Constructs a split with host information
- *
- * @param file the file name
- * @param start the position of the first byte in the file to process
- * @param length the number of bytes in the file to process
- * @param hosts the list of hosts containing the block, possibly null
- */
- public FileSplit(Path file, long start, long length, String[] hosts) {
- this.file = file;
- this.start = start;
- this.length = length;
- this.hosts = hosts;
- }
- /** The file containing this split's data. */
- public Path getPath() { return file; }
- /** The position of the first byte in the file to process. */
- public long getStart() { return start; }
- /** The number of bytes in the file to process. */
- @Override
- public long getLength() { return length; }
- @Override
- public String toString() { return file + ":" + start + "+" + length; }
- ////////////////////////////////////////////
- // Writable methods
- ////////////////////////////////////////////
- @Override
- public void write(DataOutput out) throws IOException {
- Text.writeString(out, file.toString());
- out.writeLong(start);
- out.writeLong(length);
- }
- @Override
- public void readFields(DataInput in) throws IOException {
- file = new Path(Text.readString(in));
- start = in.readLong();
- length = in.readLong();
- hosts = null;
- }
- @Override
- public String[] getLocations() throws IOException {
- if (this.hosts == null) {
- return new String[]{};
- } else {
- return this.hosts;
- }
- }
从源码中可以看出,FileSplit有四个属性:文件路径,分片起始位置,分片长度和存储分片的hosts。用这四项数据,就可以计算出提供给每个Mapper的分片数据。在InputFormat的getSplit()方法中构造分片,分片的四个属性会通过调用FileSplit的Constructor设置。
2.2再看一个InputSplit的子类:CombineFileSplit。源码如下:
为什么介绍该类呢,因为该类对小文件的处理是很有效的,所有深入理解该类,将有助于该节学习。
上面我们介绍的FileSplit对应的是一个输入文件,也就是说,如果用FileSplit对应的FileInputFormat作为输入格式,那么即使文件特别小,也是作为一个单独的InputSplit来处理,而每一个InputSplit将会由一个独立的Mapper Task来处理。在输入数据是由大量小文件组成的情形下,就会有同样大量的InputSplit,从而需要同样大量的Mapper来处理,大量的Mapper Task创建销毁开销将是巨大的,甚至对集群来说,是灾难性的!
CombineFileSplit是针对小文件的分片,它将一系列小文件封装在一个InputSplit内,这样一个Mapper就可以处理多个小文件。可以有效的降低进程开销。与FileSplit类似,CombineFileSplit同样包含文件路径,分片起始位置,分片大小和分片数据所在的host列表四个属性,只不过这些属性不再是一个值,而是一个列表。
需要注意的一点是,CombineFileSplit的getLength()方法,返回的是这一系列数据的数据的总长度。
现在,我们已深入的了解了InputSplit的概念,看了其源码,知道了其属性。我们知道数据分片是在InputFormat中实现的,接下来,我们就深入InputFormat的一个子类,FileInputFormat看看分片是如何进行的。
3 、FileInputFormat
FileInputFormat中,分片方法代码及详细注释如下,就不再详细解释该方法:
Hadoop中的InputFormat解析的更多相关文章
- Hadoop中Partition深度解析
本文地址:http://www.cnblogs.com/archimedes/p/hadoop-partitioner.html,转载请注明源地址. 旧版 API 的 Partitioner 解析 P ...
- hadoop中InputFormat 接口的设计与实现
InputFormat 主要用于描述输入数据的格式, 它提供以下两个功能.❑数据切分:按照某个策略将输入数据切分成若干个 split, 以便确定 Map Task 个数以及对应的 split.❑为 M ...
- Hadoop中常用的InputFormat、OutputFormat(转)
Hadoop中的Map Reduce框架依赖InputFormat提供数据,依赖OutputFormat输出数据,每一个Map Reduce程序都离不开它们.Hadoop提供了一系列InputForm ...
- Hadoop 中疑问解析
Hadoop 中疑问解析 FAQ问题剖析 一.HDFS 文件备份与数据安全性分析1 HDFS 原理分析1.1 Hdfs master/slave模型 hdfs采用的是master/slave模型,一个 ...
- Hadoop中Yarnrunner里面submit Job以及AM生成 至Job处理过程源码解析
参考 http://blog.csdn.net/caodaoxi/article/details/12970993 Hadoop中Yarnrunner里面submit Job以及AM生成 至Job处理 ...
- hadoop中OutputFormat 接口的设计与实现
OutputFormat 主要用于描述输出数据的格式,它能够将用户提供的 key/value 对写入特定格式的文件中. 本文将介绍 Hadoop 如何设计 OutputFormat 接口 , 以及一些 ...
- [转] - hadoop中使用lzo的压缩
在hadoop中使用lzo的压缩算法可以减小数据的大小和数据的磁盘读写时间,不仅如此,lzo是基于block分块的,这样他就允许数据被分解成chunk,并行的被hadoop处理.这样的特点,就可以让l ...
- Hadoop工程包架构解析
Hadoop源码解析 1 --- Hadoop工程包架构解析 1 Hadoop中各工程包依赖简述 Google的核心竞争技术是它的计算平台.Google的大牛们用了下面5篇文章,介绍了它们的计算 ...
- hadoop中MapReduce中压缩的使用及4种压缩格式的特征的比较
在比较四中压缩方法之前,先来点干的,说一下在MapReduce的job中怎么使用压缩. MapReduce的压缩分为map端输出内容的压缩和reduce端输出的压缩,配置很简单,只要在作业的conf中 ...
随机推荐
- [C++]虚函数-同名访问
首先来看一下派生类和基类成员同名事的处理规则: 派生类内定义了一个与基类同名的成员,该现象称为同名覆盖,此时,无论派生类内部成员函数还是派生类的对象访问同名成员,如果未加任何特殊标识,则访问派生类中重 ...
- Ubuntu环境下Nutch+Tomcat 搭建简单的搜索引擎
简易的搜索引擎搭建 我的配置: Nutch:1.2 Tomcat:7.0.57 1 Nutch设置 修改Nutch配置 1.1 修改conf/nutch-site.xml <?xml versi ...
- hdu 1452 Happy 2004
因子和: 的因子是1,2,3,6; 6的因子和是 s(6)=1+2+3+6=12; 的因子是1,2,4,5,10,20; 20的因子和是 s(20)=1+2+4+5+10+20=42; 的因子是1,2 ...
- 深入浅出Java并发包—锁机制(二)
接上文<深入浅出Java并发包—锁机制(一) > 2.Sync.FairSync.TryAcquire(公平锁) 我们直接来看代码 protected final boolean tr ...
- 李洪强漫谈iOS开发[C语言-038]-if else if语句
李洪强漫谈iOS开发[C语言-038]-if else if语句
- javaWEB邮件测试
新建一个工具类: Mail.java 该类的主要关键点是:1.设置系统属性.也就是你是用什么协议来进行邮件发送的,邮件协议有很多在种,比如impt,smpt,prop等协议, 我现在测试用的是smpt ...
- 如何删除ArcSde Service服务
1)打开“控制面板”,“服务”,找到“ArcSde Service(somename)”,这里somename就是你的ArcSde服务的真实的名字,记住这个名字(为叙述方便,以下用somename表示 ...
- Maven那点事儿(Eclipse版)
Maven那点事儿(Eclipse版) 前言: 由于最近工作学习,总是能碰到Maven的源码.虽然平时工作并不使用Maven,但是为了学习一些源码,还是必须要了解下.这篇文章不是一个全面的Mave ...
- Android SDK +Eclipse+ADT+CDT+NDK 开发环境在windows 7下的搭建
Android SDK+Eclipse+ADT+CDT+NDK 开发环境在windows 7下的搭建 这几天一直在研究 Android SDK C/C++平台的搭建,尽管以前有成功在Windows ...
- netty websocket协议开发
websocket的好处我们就不用多说了,就是用于解决长连接.服务推送等需要的一种技术. 以下我们来看一个例子: package com.ming.netty.http.websocket; impo ...