MapReduce自定义数据类型
2016-02-23 18:14
344 查看
Mapreduce有基本数据类型Text,IntWritable...,也可以由我们自定义数据类型。我们自定义数据类型需要实现接口WritableComparable,基本数据类型也是实现了这个接口。
下面是接口WritableComparable的源码
可以看到接口WritableComparable继承了两个接口,而自己并没有实现任何方法。
下面是Writable接口的源码
可以看到Writable接口中有两个方法,下面看Comparable接口
这个接口只有一个方法,所以继承自WritableComoarable的类需要实现三个方法,参照如下:
下面是接口WritableComparable的源码
public interface WritableComparable<T> extends Writable, Comparable<T> { }
可以看到接口WritableComparable继承了两个接口,而自己并没有实现任何方法。
下面是Writable接口的源码
public interface Writable { void write(DataOutput out) throws IOException; void readFields(DataInput in) throws IOException; }
可以看到Writable接口中有两个方法,下面看Comparable接口
public interface Comparable<T> { public int compareTo(T o); }
这个接口只有一个方法,所以继承自WritableComoarable的类需要实现三个方法,参照如下:
public class MyWritableComparable implements WritableComparable<MyWritableComparable> { * 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 o) { * int thisValue = this.value; * int thatValue = o.value; * return (thisValue < thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); * } * * public int hashCode() { * final int prime = 31; * int result = 1; * result = prime * result + counter; * result = prime * result + (int) (timestamp ^ (timestamp >>> 32)); * return result * } * }
相关文章推荐
- Linux开发cocos2dx程序环境搭建
- angularJS写的简易日历,有待简化
- 子进程和父进程关系以及缓冲区应用
- 运行app时报java.lang.ClassNotFoundException
- respondsToSelector:的使用
- 添加依赖项目失败
- Matlab运行错误捕捉办法
- iOS小明开发笔记(二十一) (缓存机制NSUserDefaults)
- 【bzoj2039】[2009国家集训队]employ人员雇佣 最小割
- ES6新特性概览
- Category的主要知识点
- 决策树
- bootstrap模态框关闭
- 打造高效率产品测试体系--产品测试管理(深圳,2016.3.18~19)
- Android 三大图片缓存原理、特性对比
- .Net网站架构设计(二)Web服务器集群架构
- 全屏背景-Quick-cocos2dx
- 理解javascript this 值
- SQL Server 历史SQL执行记录
- iOS小明开发笔记(二十) (retain、strong、weak、assign区别)