Hadoop WritableComparable接口
2015-09-14 17:37
453 查看
hadoop WritableComparable接口的作用,
两个作用 A 实现了一个序列化协议
B 可以比较的实现,实现方法compareTo()方法,
MapReduce中所有的key值类型都必须实现这个接口,避免重复的key啊
===============================================================分割线=====================================
WritableComparable接口
Writable接口大家可能都知道,它是一个实现了序列化协议的序列化对象。在Hadoop中定义一个结构化对象都要实现Writable接口,使得该结构化对象可以序列化为字节流,字节流也可以反序列化为结构化对象。那WritableComparable接口是可序列化并且可比较的接口。MapReduce中所有的key值类型都必须实现这个接口,既然是可序列化的那就必须得实现readFiels()和write()这两个序列化和反序列化函数,既然也是可比较的那就必须得实现compareTo()函数,该函数即是比较和排序规则的实现。这样MR中的key值就既能可序列化又是可比较的。下面几符图是API中对WritableComparable接口的解释及其方法,还有一个实现了该接口的对象的列子:
A
Example:
两个作用 A 实现了一个序列化协议
B 可以比较的实现,实现方法compareTo()方法,
MapReduce中所有的key值类型都必须实现这个接口,避免重复的key啊
===============================================================分割线=====================================
WritableComparable接口
Writable接口大家可能都知道,它是一个实现了序列化协议的序列化对象。在Hadoop中定义一个结构化对象都要实现Writable接口,使得该结构化对象可以序列化为字节流,字节流也可以反序列化为结构化对象。那WritableComparable接口是可序列化并且可比较的接口。MapReduce中所有的key值类型都必须实现这个接口,既然是可序列化的那就必须得实现readFiels()和write()这两个序列化和反序列化函数,既然也是可比较的那就必须得实现compareTo()函数,该函数即是比较和排序规则的实现。这样MR中的key值就既能可序列化又是可比较的。下面几符图是API中对WritableComparable接口的解释及其方法,还有一个实现了该接口的对象的列子:
public interface WritableComparable<T> extends Writable, Comparable<T>
A
Writablewhich is also
Comparable.
WritableComparables can be compared to each other, typically via
Comparators. Any type which is to be used as a
keyin the Hadoop Map-Reduce framework should implement this interface.
Example:
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)); } }
转自:http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228579.html 感谢作者
相关文章推荐
- 【转】使用 vim + ctags + cscope + taglist 阅读源码
- linux学习二
- Opencv 完美配置攻略 2014 (Win8.1 + Opencv 2.4.8 + VS 2013)
- linux常用命令(25):linux文件属性详解
- Linux学习之八——利用变量
- nginx基本配置和虚拟主机配置
- 25 个 Linux 性能监控工具
- ARMv8 與 Linux的新手筆記
- 查看linux版本的三种常用方法
- 2015年9月13日课程作业--关于SHELL的问题
- hadoop 自学指南四之常见MapReduce案例
- Linux命令初识二
- Shell编程入门
- 利用crop剪裁方式训练图片的一些事项
- openwrt无法生成sysupgrade.bin
- linux 安装 mysql-5.6.26
- Install Htop (Linux Process Monitoring) for RHEL, CentOS & Fedora
- 如何解决网站大规模并发访问带来的性能下降问题
- centos6.5设备mysql5.6
- Hadoop之—— CentOS Warning: $HADOOP_HOME is deprecated解