MapReduce API 基本概念
在正式分析新旧 API 之前, 先要介绍几个基本概念。 这些概念贯穿于所有 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 < 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() 函数迭代处理。
参考资料
《Hadoop技术内幕 深入理解MapReduce架构设计与实现原理》
MapReduce API 基本概念的更多相关文章
- 新增的Java MapReduce API
http://book.51cto.com/art/201106/269647.htm Hadoop的版本0.20.0包含有一个新的 Java MapReduce API,有时也称为"上下文 ...
- MapReduce编程job概念原理
在Hadoop中,每个MapReduce任务都被初始化为一个job,每个job又可分为两个阶段:map阶段和reduce阶段.这两个阶段分别用两个函数来表示.Map函数接收一个<key,valu ...
- Compute API 关键概念 详解
Compute API 是 RESTful HTTP 服务,提供管理虚机的能力. 虚机可能有不同的内存大小,CPU数量,硬盘大小,能够在几分钟之内创建出来.和虚机的交互,可以通过Compute API ...
- 【翻译】Flink Table Api & SQL —— 概念与通用API
本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/common.html Flink Tabl ...
- Vulkan API基本概念
设备初始化 Instance --> GPU --> Device Instance表示具体的Vulkan应用.在一个应用程序中可以创建多个实例,这些实例之间相互独立,互不干扰. 当调用A ...
- web API的概念
11月20日 纷乱的术语 接口:从接口测试说起,接口是某个对象和外界交互的部分,应用程序可能有很多接口. 用户界面UI(user interface) 消息交互接口,外界是其他程序:diameter, ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- SDK,API概念
什么是SDK什么是API? SDK 就是 Software Development Kit 的缩写,就是"软件开发工具包". 这是一个覆盖面相当广泛的名词,可以这么说:辅助开发某一 ...
- Hadoop案例(十一)MapReduce的API使用
一学生成绩---增强版 数据信息 computer,huangxiaoming,,,,,,, computer,xuzheng,,,,, computer,huangbo,,,, english,zh ...
随机推荐
- Java-redis分布式锁 抢购秒杀系统 实现
一.使用分布式锁要满足的几个条件: 系统是一个分布式系统(关键是分布式,单机的可以使用ReentrantLock或者synchronized代码块来实现) 共享资源(各个系统访问同一个资源,资源的载体 ...
- Cordova - 禁用整个应用页面的上下拖动效果(防止拖动出现黑边)
可在 config.xml 中进行如下设置: <preference name="WebViewBounce" value="false" /> ...
- matlab下mex 调用opencv库
1. 首先写好待编译的.cpp文件,使用混合编程,以人脸检测为例 #include "mex.h" // Required for the use of MEX files // ...
- elementUI 学习入门之 input 输入框
基础用法 <el-input v-model="input1" palcehoder="请输入"></el-input> var Mai ...
- PHP获取客户端请求头信息
获取HTTP请求头信息 Apache 如果web服务器用的是apache,可以直接用php的库函数getallheaders() Nginx 如果web服务器用的是nginx,则无法直接使用getal ...
- Git in Powershell saying 'Could not find ssh-agent'
加入系统环境变量D:\Program Files\Git\usr\bin
- Xamarin.Forms教程开发的Xcode的下载安装
Xamarin.Forms教程开发的Xcode的下载安装 Xamarin.Forms教程开发的Xcode的下载安装,Xcode是开发iOS应用程序的图形化开发工具.本节将讲解Xamarin.Forms ...
- 【BZOJ 3229】 3229: [Sdoi2008]石子合并 (GarsiaWachs算法)
3229: [Sdoi2008]石子合并 Description 在一个操场上摆放着一排N堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石子数记为该次合 ...
- 「Luogu4321」随机游走
「Luogu4321」随机游走 题目描述 有一张 \(n\) 个点 \(m\) 条边的无向图,\(Q\) 组询问,每次询问给出一个出发点和一个点集 \(S\) ,求从出发点出发随机游走走遍这个点集的期 ...
- Java创建线程的三种主要方式
Java创建线程的主要方式 一.继承Thread类创建 通过继承Thread并且重写其run(),run方法中即线程执行任务.创建后的子类通过调用 start() 方法即可执行线程方法. 通过继承Th ...