以IntWritable为例介绍,定制writable的步骤

//继承 WritableComparable接口(继承了writable接口和comparable接口)
public class IntWritable implements WritableComparable<IntWritable> { //定义普通java类型的成员变量
private int value; //成员变量的set方法
public void set(int value) { this.value = value; }
//成员变量的get方法
public int get() { return value; } //无参构造函数,为MR框架反射机制所调用
public IntWritable() {}
//有参构造函数
public IntWritable(int value) { set(value); } //反序列化方法
public void readFields(DataInput in) throws IOException {
value = in.readInt();
}
//序列化方法
public void write(DataOutput out) throws IOException {
out.writeInt(value);
} //覆写equals()方法
public boolean equals(Object o) {
if (!(o instanceof IntWritable))
return false;
IntWritable other = (IntWritable)o;
return this.value == other.value;
} //覆写hashCode()方法
public int hashCode() {
return value;
} //覆写toString()方法
public String toString() {
return Integer.toString(value);
} //覆写 comparable接口 中的compareTo()方法【默认升序】
public int compareTo(IntWritable o) {
int thisValue = this.value;
int thatValue = o.value;
return (thisValue<thatValue ? - : (thisValue==thatValue ? : ));
} //1. 定义内部类Comparator【比较器】继承自WritableComparator类
public static class Comparator extends WritableComparator { //2. 不可缺少的无参构造函数,反射机制调用
public Comparator() {
super(IntWritable.class);
} //3. 覆写 字节流层面的比较排序
public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) {
//返回 字符数组b1 的编码值
int thisValue = readInt(b1, s1);
int thatValue = readInt(b2, s2);
return (thisValue<thatValue ? - : (thisValue==thatValue ? : ));
}
}
//4. 向WritableComparator类注册定制的writable类【Haoop自动调用上述的比较器】
static {
WritableComparator.define(IntWritable.class, new Comparator());
}
}

注意点:

  1. 在定制Writable类中实现字节流层面的比较时,一般不直接继承RawComparator类,而是继承其子类WritableComparator,因为子类为我们提供了一些有用的工具方法,比如从字节数组中读取int、long和vlong等值。并覆写 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) 方法。
  2. 当然编写完compare()方法之后,不要忘了为定制的Writable类注册编写的RawComparator类。
  3. 对于代码中的 readInt()工具方法的具体实现:
/** Parse an integer from a byte array. */
public static int readInt(byte[] bytes, int start) {
return (((bytes[start ] & 0xff) << ) +
((bytes[start+] & 0xff) << ) +
((bytes[start+] & 0xff) << ) +
((bytes[start+] & 0xff)));
}

定制Writable类的更多相关文章

  1. hadoop中实现定制Writable类

    Hadoop中有一套Writable实现可以满足大部分需求,但是在有些情况下,我们需要根据自己的需要构造一个新的实现,有了定制的Writable,我们就可以完全控制二进制表示和排序顺序. 为了演示如何 ...

  2. Hadoop中Writable类之四

    1.定制Writable类型 Hadoop中有一套Writable实现,例如:IntWritable.Text等,但是,有时候可能并不能满足自己的需求,这个时候,就需要自己定制Writable类型. ...

  3. Java Gradle入门指南之内建与定制任务类(buildSrc、Groovy等)

        上一篇随笔介绍了Gradle的安装与任务管理,这篇着重介绍Gradle的内建任务(in-built tasks)与自定义任务(custom tasks),借助Gradle提供的众多内建任务类型 ...

  4. python 元类与定制元类

    1:元类 元类:类的创建与管理者 所有类的元类是type class a: pass print(type(a)) 结果:<class 'type'> 2:定制元类 类的实例化过程:(可看 ...

  5. Hadoop中Writable类之二

    1.ASCII.Unicode.UFT-8 在看Text类型的时候,里面出现了上面三种编码,先看看这三种编码: ASCII是基于拉丁字母的一套电脑编码系统.它主要用于显示现代英语和其他西欧语言.它是现 ...

  6. hadoop中典型Writable类详解

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-writable.html,转载请注明源地址. Hadoop将很多Writable类归入org.apac ...

  7. hadoop中的序列化与Writable类

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-writable-class.html,转载请注明源地址. hadoop中自带的org.apache.h ...

  8. Hadoop中Writable类

    1.Writable简单介绍 在前面的博客中,经常出现IntWritable,ByteWritable.....光从字面上,就可以看出,给人的感觉是基本数据类型 和 序列化!在Hadoop中自带的or ...

  9. Hadoop中Writable类之三

    1.BytesWritable <1>定义 ByteWritable是对二进制数据组的封装.它的序列化格式为一个用于指定后面数据字节数的整数域(4个字节),后跟字节本身. 举个例子,假如有 ...

随机推荐

  1. HDU 5667 Sequence(矩阵快速幂)

    Problem Description Holion August will eat every thing he has found. Now there are many foods,but he ...

  2. HUD2647 Reward_反向建图拓扑排序

    HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...

  3. 移动端click时间、touch事件、tap事件

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...

  4. Reference counted objects

    Reference counted objects · netty/netty Wiki https://github.com/netty/netty/wiki/Reference-counted-o ...

  5. mysql创建索引时报错1170

    MySQL只能将BLOB/TEXT类型字段设置索引为BLOB/TEXT数据的前N个字符. 索引指定下col2的长度就可以了 :alter table foo add index col_2 (col2 ...

  6. php中var_dump、var_export和print_r的用法区别

    void var_dump ( mixed $expression [, mixed $... ] )此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值.数组将递归展开值,通过缩进显示其结 ...

  7. Ponds----hdu5438(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438   题意:有n个池塘和m个管道:每个池塘的价值是v, 现在由于资金问题要删除池塘:但是删除的池塘 ...

  8. 对Numpy数组按axis运算的理解

    Python的Numpy数组运算中,有时会出现按axis进行运算的情况,如 >>> x = np.array([[1, 1], [2, 2]]) >>> x arr ...

  9. 007-aven-assembly-plugin和maven-jar-plugin打包,java启动命令

    一.需求 打一个zip包,包含如下: bin为程序脚本,启动和停止 lib为依赖包 根目录下为配置文件和项目jar包 二.知识储备 2.1.插件了解 plugin function maven-jar ...

  10. node.js---sails项目开发

    http://sailsdoc.swift.ren/ 这里有 sails中文文档 node.js---sails项目开发(1)安装,启动sails node.js---sails项目开发(2)安装测试 ...