MapReduce API 基本概念

1.序列化

序列化是指将结构化对象转为字节流以便于通过网络进行传输或写入持久存储的过程。反序列化指的是将字节流转为结构化对象的过程。 在 Hadoop MapReduce 中, 序列化的主

作用有两个: 永久存储和进程间通信。为了能够读取或者存储 Java 对象, MapReduce 编程模型要求用户输入和输出数据中的 key 和
value 必须是可序列化的。 在 Hadoop MapReduce 中 , 使一个 Java 对象可序列化的方法是让其对应的类实现
Writable 接口 。 但对于 key 而言,由于它是数据排序的关键字, 因此还需要提供比较两个 key 对象的方法。
为此,key对应类需实现WritableComparable 接口 , 它的类如图:

在package org.apache.hadoop.io 中的WritableComparable.java文件中定义:

public interface WritableComparable<T> extends Writable, Comparable<T> {
}

再来看看Writable接口的定义:

public interface Writable {
      /**
       * Serialize the fields of this object to <code>out</code>.
       *
       * @param out <code>DataOuput</code> to serialize this object into.
       * @throws IOException
       */
      void write(DataOutput out) throws IOException;

      /**
       * Deserialize the fields of this object from <code>in</code>.
       *
       * <p>For efficiency, implementations should attempt to re-use storage in the
       * existing object where possible.</p>
       *
       * @param in <code>DataInput</code> to deseriablize this object from.
       * @throws IOException
       */
      void readFields(DataInput in) throws IOException;
    }

可以很明显的看出,write(DataOutput out)方法的作用是将指定对象的域序列化为out相同的类型;readFields(DataInput in)方法的作用是将in对象中的域反序列化,考虑效率因素,实现接口的时候应该使用已经存在的对象存储。

DataInput接口定义源代码如下:

public
interface DataInput {
   void readFully(byte b[]) throws IOException;

   void readFully(byte b[], int off, int len) throws IOException;

   int skipBytes(int n) throws IOException;

   boolean readBoolean() throws IOException;

   byte readByte() throws IOException;

   int readUnsignedByte() throws IOException;

   short readShort() throws IOException;

   int readUnsignedShort() throws IOException;

   char readChar() throws IOException;

   int readInt() throws IOException;

   long readLong() throws IOException;

   float readFloat() throws IOException;

   double readDouble() throws IOException;

   String readLine() throws IOException;

   String readUTF() throws IOException;
}

每个方法的含义差不多,具体可参见java jdk源码

DataOutput接口定义源代码如下:

public
interface DataOutput {

    void write(int b) throws IOException;

    void write(byte b[]) throws IOException;

    void write(byte b[], int off, int len) throws IOException;

    void writeBoolean(boolean v) throws IOException;

    void writeByte(int v) throws IOException;

    void writeShort(int v) throws IOException;

    void writeChar(int v) throws IOException;

    void writeInt(int v) throws IOException;

    void writeLong(long v) throws IOException;

    void writeFloat(float v) throws IOException;

    void writeDouble(double v) throws IOException;

    void writeBytes(String s) throws IOException;

    void writeChars(String s) throws IOException;

    void writeUTF(String s) throws IOException;
}

WritableComparable可以用来比较,通常通过Comparator . 在hadoop的Map-Reduce框架中任何被用作key的类型都要实现这个接口。

看一个例子:

public class MyWritableComparable implements WritableComparable {
       // Some data
       private int counter;
       private long timestamp;

       public void write(DataOutput out) throws IOException {
         out.writeInt(counter);
         out.writeLong(timestamp);
       }

       public void readFields(DataInput in) throws IOException {
         counter = in.readInt();
         timestamp = in.readLong();
       }

       public int compareTo(MyWritableComparable w) {
         int thisValue = this.value;
         int thatValue = ((IntWritable)o).value;
         return (thisValue &lt; thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
       }
}

2.Reporter 参数

Reporter 是 MapReduce 提供给应用程序的工具。 如图所示,应用程序可使用Reporter 中的方法报告完成进度(progress)、设定状态消息(setStatus 以及更新计数器( incrCounter)。

Reporter 是一个基础参数。 MapReduce 对外提供的大部分组件, 包括 InputFormat、Mapper 和 Reducer 等,均在其主要方法中添加了该参数。

3.回调机制

回调机制是一种常见的设计模式。它将工作流内的某个功能按照约定的接口暴露给外部使用者, 为外部使用者提供数据,或要求外部使用者提供数据。
Hadoop
MapReduce 对外提供的 5 个组件( InputFormat、 Mapper、 Partitioner、 Reducer
和 OutputFormat) 实际上全部属于回调接口 。 当用户按照约定实现这几个接口后, MapReduce运行时环境会自
动调用它们。如图所示,MapReduce 给用户暴露了接口 Mapper, 当用户按照自己的应用程序逻辑实现自己的 MyMapper
后,Hadoop MapReduce 运行时环境会将输入数据解析成 key/value 对, 并调用 map() 函数迭代处理。

时间: 2024-07-28 19:14:28

MapReduce API 基本概念的相关文章

Mapreduce和Yarn概念,参数优化,作用,原理,MapReduce计数器 Counter,MapReduce 多job串联之ControlledJob(来自学习资料)

3.3. MapReduce与YARN 3.3.1 YARN概述 Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,而mapreduce等运算程序则相当于运行于操作系统之上的应用程序 3.3.2 YARN的重要概念 1.  yarn并不清楚用户提交的程序的运行机制 2.  yarn只提供运算资源的调度(用户程序向yarn申请资源,yarn就负责分配资源) 3.  yarn中的主管角色叫ResourceManager 4.  yarn中具体提供运算资源的

MapReduce的基本概念和由来

1.什么是MapReduce MapReduce是面向大数据并行处理的计算模型.框架和平台,它隐含了以下三层含义: 1)MapReduce是一个基于集群的高性能并行计算平台(Cluster Infrastructure).它允许用市场上普通的商用服务器构成一个包含数十.数百至数千个节点的分布和并行计算集群. 2)MapReduce是一个并行计算与运行软件框架(Software Framework).它提供了一个庞大但设计精良的并行计算软件框架,能自动完成计算任务的并行化处理,自动划分计算数据和计

MapReduce论文中文翻译

原文地址: http://labs.google.com/papers/mapreduce.html 译者: alex 摘要 MapReduce是一个编程模型,也是一个处理和生成超大数据集的算法模型的相关实现.用户首先创建一个Map函数处理一个基于key/value pair的数据集合,输出中间的基于key/value pair的数据集合:然后再创建一个Reduce函数用来合并所有的具有相同中间key值的中间value值.现实世界中有很多满足上述处理模型的例子,本论文将详细描述这个模型. Map

MapReduce:超大机群上的简单数据处理

MapReduce:超大机群上的简单数据处理                                           摘要MapReduce是一个编程模型,和处理,产生大数据集的相关实现.用户指定一个 map函数处理一个key/value对,从而产生中间的key/value对集.然后再指定一个reduce函数合并所有的具有相同中间key的中间 value.下面将列举许多可以用这个模型来表示的现实世界的工作.以这种方式写的程序能自动的在大规模的普通机器上实现并行化.这个运行时系统关心这

Amazon推出基于Hadoop的MapReduce

对于如何将流行的Apache HadoopMapReduce框架运行于AmazonEC2之上,老早之前就已经有教程了.今天Amazon通过Amazon Elastic MapReduce对其提供了官方支持,不断改善自身.从其产品页面可看到: Amazon Elastic MapReduce自动地在Amazon EC2实例上驱动一个MapReduce框架的Hadoop实现,将任务流中的数据分解为更小的块以用于并行处理("map"函数),并最终将处理后的数 据重新组合在一起成为最后结果(&

用于简化MapReduce编程的Java库Apache Crunch简介

Apache Crunch(孵化器项目)是基于Google的FlumeJava库编写的Java库,用于创建MapReduce流水线.与其他用来创建 MapReduce作业的高层工具(如Apache Hive.Apache Pig和Cascading等)类似,Crunch提供了用于实现如连接数据.执行 聚合和排序记录等常见任务的模式库.而与其他工具不同的是,Crunch并不强制所有输入遵循同一数据类型.相反,Crunch 使用了一种定制的类型系统,非常灵活,能够直接处理复杂数据类型,如时间序列.H

MapReduce源码分析之LocatedFileStatusFetcher

        LocatedFileStatusFetcher是MapReduce中一个针对给定输入路径数组,使用配置的线程数目来获取数据块位置的实用类.它的主要作用就是利用多线程技术,每个线程对应一个任务,每个任务针对给定输入路径数组Path[],解析出文件状态列表队列BlockingQueue<List<FileStatus>>.其中,输入数据输入路径只不过是一个Path,而输出数据则是文件状态列表队列BlockingQueue<List<FileStatus&g

在JavaScript SDK里使用SoundCloud API

SoundCloud开发出了一款可被开发者使用的API,这款API能使开发者获得他们想要的几乎任何数据.但是该API的用法有些混乱,特别是对初学者来说,因为此时的SoundCloud API开发文档和文档示例使用的都是SDK(软件开发工具箱)的不同版本. SoundCloud介绍链接地址: http://baike.sogou.com/v128528573.htm SoundCloud API和SoundCloud SDK之间有什么区别呢?从根本上说,SoundCloud API是一个URL的集

云计算中的RESTful API入门

许多人认为 API 是组织访问云计算供应商提供的服务的最佳方法.云使用者使用 API 作为软件接口,以各种方式连接和使用资源,但最优或现代的途径是使用基于 http://www.aliyun.com/zixun/aggregation/14172.html">RESTful 协议的 API.请继续阅读,理解 API 的概念,以及它们在 REST API 和云计算服务中如何使用.本文提供了多个用例来演示如何在真实世界中使用此技术. API 入门 API 是一些软件接口,它们针对数据处理而优化