MapReduce编程模型之InputFormat分析(-)

MapReduce因其简单编程模型而流行,适用于可拆分为独立子问题的分布式计算。本文聚焦InputFormat,它负责数据切片和键值对生成。InputFormat包含两个主要职责:按策略切分输入数据为InputSplit,并提供RecordReader解析键值对。文章还提及新版API的InputFormat类图及Hadoop自带的实现,并推荐相关Hadoop技术书籍。
摘要由CSDN通过智能技术生成

      MapReduce编程模型之所以流行是因为其编程模型的简单性,MapReduce编程模型是由一些高度抽象化的编程组件组成,我们只要实现了这些组件,并且在作业配置中设定这些组件,框架自动会调用这些组件完成我们所设定的功能.

 1.概述

          适用场景:MapReduce是一个分布式计算框架,其适用于问题是,其问题可以被分解为多个互相独立的子问题,这些子问题可以被并行的解决,等这些子问题被解决了,问题自然就被解决了.

             处理流程:首先数据会被分为若干分片,我们以分片作为数据输入,对分片进行迭代,迭代将会生成K/V键值对,这些键值对会被传入map(进行映射),映射过程,会被映射为另外一些键值对.这些键值对,会被进行分组,这些键值对依据键进行分组,这些组将会被进行reduce(进行归约),从而又生成另外一些键值对.这些最终结果的键值对将会被保存到文件系统中.

MapReduce编程接口体系结构

   整个编程接口体系结构共分为5个层次,最低层就是MapReduce运行时环境,上一层是MapReduce编程模型的最基本的5个API,其中我们在写MapReduceJob只要实现Map,Reduce就可以了,其他部分基本上不用涉及.而在更上一层,则提供了更高一级的功能组件:

JobControl(DAG):有时候我们的问题无法由一个MapReduce任务进行解决,而这个工具可以让我们更好的编写需要多个MapReduce完成的作业,我们只需要设定MapReduce作业间的依赖关系,工具会自动帮助我们处理这些依赖关系.

ChainMapper/ChainReducer:这可以帮助我们编写链式作业,在Map和Reduce阶段,存在多个Mapper.

Hadoop Streaming:方便用户使用非Java编写MapReduce作业

Hadoop Pipes:使用C++编写MapReduce作业

2.InputFormat接口的设计与实现

InputFormat接口就是用于描述输入数据的格式,主要具有两大功能

1.依据某种策略,将输入数据切分为多个分片.确定Map数量和其对应的InputSplit

InputSplit定义了单个map任务的大小和其理想的数据本地性

<span style="font-size:14px;">public abstract class InputSplit {
  /**
   * Get the size of the split, so that the input splits can be sorted by size.
   * @return the number of bytes in the split
   * @throws IOException
   * @throws InterruptedException
   */
  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.
   * @return a new array of the node nodes.
   * @throws IOException
   * @throws InterruptedException
   */
  public abstract 
    String[] getLocations() throws IOException, InterruptedException;
}
</span>

2.提供RecordReader,用于将分片中的数据解析为K/V键值对,用于Map调用

<span style="font-size:14px;">public abstract class RecordReader<KEYIN, VALUEIN> implements Closeable {

  /**
   * Called once at initialization.
   * @param split the split that defines the range of records to read
   * @param context the information about the task
   * @throws IOException
   * @throws InterruptedException
   */
  public abstract void initialize(InputSplit split,
                                  TaskAttemptContext context
                                  ) throws IOException, InterruptedException;

  /**
   * Read the next key, value pair.
   * @return true if a key/value pair was read
   * @throws IOException
   * @throws InterruptedException
   */
  public abstract 
  boolean nextKeyValue() throws IOException, InterruptedException;

  /**
   * Get the current key
   * @return the current key or null if there is no current key
   * @throws IOException
   * @throws InterruptedException
   */
  public abstract
  KEYIN getCurrentKey() throws IOException, InterruptedException;
  
  /**
   * Get the current value.
   * @return the object that was read
   * @throws IOException
   * @throws InterruptedException
   */
  public abstract 
  VALUEIN getCurrentValue() throws IOException, InterruptedException;
  
  /**
   * The current progress of the record reader through its data.
   * @return a number between 0.0 and 1.0 that is the fraction of the data read
   * @throws IOException
   * @throws InterruptedException
   */
  public abstract float getProgress() throws IOException, InterruptedException;
  
  /**
   * Close the record reader.
   */
  public abstract void close() throws IOException;
}</span>

新版API中的InputFormat类图

Hadoop MapReduce自带的InputFormat实现的类图


参考书籍:

Hadoop技术内幕 深入理解MapReduce架构设计与实现原理

Hadoop高级编程---构建与实现大数据解决方案

Hadoop权威指南 第2版

Hadoop实战


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值