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 继承知识点检测
笔试题 Java 继承知识点检测 Question 1 Output of following Java Program? class Base { public void show() { Syst ...
- cpp笔记(长期更新)
c++0x:c++11标准成为正式标准之前的草案临时名字 多态:接口的多种不同的实现方式即为多态,即子类类型的指针赋值给父类类型的指针 动态内存(堆)的管理是通过一对运算符来完成的: new:在动态内 ...
- C++ 四种显示转换
转自:http://www.jellythink.com/archives/205 (果冻想) 前言 这篇文章总结的是C++中的类型转换,这些小的知识点,有的时候,自己不是很注意,但是在实际开发中 ...
- 【BZOJ 3997】 3997: [TJOI2015]组合数学 (DP| 最小链覆盖=最大点独立集)
3997: [TJOI2015]组合数学 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 919 Solved: 664 Description 给出 ...
- [BZOJ4817][SDOI2017]树点涂色(LCT+DFS序线段树)
4817: [Sdoi2017]树点涂色 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 692 Solved: 408[Submit][Status ...
- bzoj 1098
对于关系,看其是否是“等价关系”,即满足:自反,传递,对称. 如果是可以用并查集来连接等价类. 这道题是求原图补集的联通快个数,考虑原图度最少的点(由鸽巢原理,最多为2*e/n个). 先将未与其连边的 ...
- [转]Android Service完全解析,关于服务你所需知道的一切
目录(?)[+] Android Service完全解析,关于服务你所需知道的一切(上) 分类: Android疑难解析2013-10-31 08:10 6451人阅读 评论(39) 收藏 举报 ...
- HDU 5298 Solid Geometry Homework 暴力
Solid Geometry Homework 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5298 Description Yellowstar ...
- 对于GTPv2协议头部的解析
参考3GPP TS 29.060 GTP的头部是可变的,GTP-C(control)和GTP-U(user)共同使用一个头部. GTP Header头部: -Version 用来标识GTP协议的版本, ...
- 人脸检测:微软 VS Face++
近些年国内的人脸技术已经得到了很大的发展,今天网上无意看到了微软和face++两家公司的人脸检测,Face++号称国际顶尖的技术,也用过他们的接口感觉确实很不错,而微软则不再话下了.于是想对比微软和F ...