使用RawComparator加速Hadoop程序

在前面两篇文章[1][2]中我们介绍了Hadoop序列化的相关知识,包括Writable接口与Writable对象以及如何编写定制的Writable类,深入的分析了Writable类序列化之后占用的字节空间以及字节序列的构成。我们指出Hadoop序列化是Hadoop的核心部分之一,了解和分析Writable类的相关知识有助于我们理解Hadoop序列化的工作方式以及选择合适的Writable类作为MapReduce的键和值,以达到高效利用磁盘空间以及快速读写对象。因为在数据密集型计算中,在网络数据的传输是影响计算效率的一个重要因素,选择合适的Writable对象不但减小了磁盘空间,而且更重要的是其减小了需要在网络中传输的数据量,从而加快了程序的速度。

在本文中我们介绍另外一种方法加快程序的速度,这就是使用RawComparator加速Hadoop程序。我们知道作为键(Key)的Writable类必须实现WritableComparable接口,以实现对键进行排序的功能。Writable类进行比较时,Hadoop的默认方式是先将序列化后的对象字节流反序列化为对象,然后再进行比较(compareTo方法),比较过程需要一个反序列化的步骤。RawComparator的做法是不进行反序列化,而是在字节流层面进行比较,这样就省下了反序列化过程,从而加速程序的运行。Hadoop自身提供的IntWritable、LongWritabe等类已经实现了这种优化,使这些Writable类作为键进行比较时,直接使用序列化的字节数组进行比较大小,而不用进行反序列化。

RawComparator的实现

在Hadoop中编写Writable的RawComparator一般不直接继承RawComparator类,而是继承RawComparator的子类WritableComparator,因为WritableComparator类为我们提供了一些有用的工具方法,比如从字节数组中读取int、long和vlong等值。下面是上两篇文章中我们定制的MyWritable类的RawComparator实现,定制的MyWritable由两个VLongWritable对组成,为了添加RawComparator功能,Writable类必须实现WritableComparable接口,这里不再展示实现了WritableComparable接口的MyWritableComparable类的全部内容,而只是MyWritableComparable类中Comparator的实现,完整的代码可以在github中找到。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
...//omitted for conciseness
/**
* A RawComparator that compares serialized VlongWritable Pair
* compare method decode long value from serialized byte array one by one
*
* @author yoyzhou
*
* */
public static class Comparator extends WritableComparator { public Comparator() {
super(MyWritableComparable.class);
} public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { int cmp = 1;
//determine how many bytes the first VLong takes
int n1 = WritableUtils.decodeVIntSize(b1[s1]);
int n2 = WritableUtils.decodeVIntSize(b2[s2]); try {
//read value from VLongWritable byte array
long l11 = readVLong(b1, s1);
long l21 = readVLong(b2, s2); cmp = l11 > l21 ? 1 : (l11 == l21 ? 0 : -1);
if (cmp != 0) { return cmp; } else { long l12 = readVLong(b1, s1 + n1);
long l22 = readVLong(b2, s2 + n2);
return cmp = l12 > l22 ? 1 : (l12 == l22 ? 0 : -1);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} static { // register this comparator
WritableComparator.define(MyWritableComparable.class, new Comparator());
} ...

通过上面的代码我们可以看到要实现Writable的RawComparator我们只需要重载WritableComparator的public intcompare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)方法。在我们的例子中,通过从VLongWritable对序列化后字节数组中一个一个的读取VLongWritable的值,再进行比较。

当然编写完compare方法之后,不要忘了为Writable类注册编写的RawComparator类。

总结

为Writable类编写RawComparator必须对Writable本身序列化之后的字节数组有清晰的了解,知道如何从字节数组中读取Writable对象的值,而这正是我们前两篇关于Hadoop序列化和Writable接口的文章所要阐述的内容。

通过以上的三篇文章,我们了解了Hadoop Writable接口,如何编写自己的Writable类,Writable类的字节序列长度与其构成,以及如何为Writable类编写RawComparator来为Hadoop提速。

参考资料

Tom White, Hadoop: The Definitive Guide, 3rd Edition

Hadoop序列化与Writable接口(一)

Hadoop序列化与Writable接口(二)

--EOF--

使用RawComparator加速Hadoop程序的更多相关文章

  1. IntelliJ IDEA + Maven环境编写第一个hadoop程序

    1. 新建IntelliJ下的maven项目 点击File->New->Project,在弹出的对话框中选择Maven,JDK选择你自己安装的版本,点击Next 2. 填写Maven的Gr ...

  2. [转载]高效使用matlab之四:一个加速matlab程序的例子

    原文地址:http://www.bfcat.com/index.php/2012/11/speed-up-app/ 这篇文章原文是matlab网站上的,我把它翻译过来同时自己也学习一下.原文见这里 这 ...

  3. 深入剖析HADOOP程序日志

    深入剖析HADOOP程序日志 前提 本文来自于 博客园 逖靖寒的世界 http://gpcuster.cnblogs.com 了解log4j的使用. 正文 本文来自于 博客园 逖靖寒的世界 http: ...

  4. eclipse运行hadoop程序报错:Connection refused: no further information

    eclipse运行hadoop程序报错:Connection refused: no further information log4j:WARN No appenders could be foun ...

  5. WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path

    之前在mac上调试hadoop程序(mac之前配置过hadoop环境)一直都是正常的.因为工作需要,需要在windows上先调试该程序,然后再转到linux下.程序运行的过程中,报Failed to ...

  6. 运行第一个Hadoop程序,WordCount

    系统: Ubuntu14.04 Hadoop版本: 2.7.2 参照http://www.cnblogs.com/taichu/p/5264185.html中的分享,来学习运行第一个hadoop程序. ...

  7. 使用ToolRunner运行Hadoop程序基本原理分析

    为了简化命令行方式运行作业,Hadoop自带了一些辅助类.GenericOptionsParser是一个类,用来解释常用的Hadoop命令行选项,并根据需要,为Configuration对象设置相应的 ...

  8. 第一个Hadoop程序——Hello Hadoop

    本人原创,转载请注明出处:http://blog.csdn.net/panjunbiao/article/details/12773163 下载Hadoop程序包,下载地址:http://hadoop ...

  9. 用Cython加速Python程序以及包装C程序简单测试

    用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...

随机推荐

  1. HDU 4734 F(x) ★(数位DP)

    题意 一个整数 (AnAn-1An-2 ... A2A1), 定义 F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1,求[0..B]内有多少 ...

  2. Windows、Ubuntu双系统正确卸载Ubuntu系统

    先判断启动方式,以管理员身份打开cmd或者power shell,输入bcdedit,找到path那一行,如果是winload.efi就是uefi引导,若为exe就是legacy引导 或者win+r输 ...

  3. nmcli 使用记录---fatt

    安装nmcli工具 yum install NetworkManager 使用语法 Usage: nmcli [OPTIONS] OBJECT { COMMAND | help } OBJECT g[ ...

  4. ActiveMQ教程(简介与安装)

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久 ...

  5. SQL基础整理(事务)

    事务==流程控制 确保流程只能成功或者失败,若出现错误会自动回到原点 具体的实现代码如下: begin tran insert into student values(') goto tranroll ...

  6. LeetCode OJ:Jump Game II(跳跃游戏2)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. PHP实现登录功能DEMO

    PHP实现登录的原理是什么呢?就是利用Session实现的,用户访问网站,系统会自动在服务器生成一个Session文件,这个Session可以用来存储用户的登录信息.好了,这是基本储备,我们下面来实现 ...

  8. MoreEffectiveC++Item35 条款27: 要求或禁止对象产生于heap中

    一 要求对象产生在heap中 阻止对象产生产生在non-heap中最简单的方法是将其构造或析构函数声明在private下,用一个public的函数去调用起构造和析构函数 class UPNumber ...

  9. L161

    The robot arm made for gentle undersea explorationA soft robotic arm which will allow underwater sea ...

  10. c# DataTable 数据集处理DataTableHandler

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...