(5-2)Writable源码分析
来源:互联网 发布:视频录制软件 编辑:程序博客网 时间:2024/06/05 17:14
//Writable是一个接口,里面有两个方法write()和ReadFields()。源码中给出了一个自定义序列化的例子代码。
package org.apache.hadoop.io;/** * <p>Example:</p> * <p><blockquote><pre> * public class MyWritable implements Writable { * // 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 static MyWritable read(DataInput in) throws IOException { * MyWritable w = new MyWritable(); * w.readFields(in); * return w; * } * } * </pre></blockquote></p> */@InterfaceAudience.Public@InterfaceStability.Stablepublic interface Writable { void write(DataOutput out) throws IOException; void readFields(DataInput in) throws IOException;}
//hadoop序列化还用到了Java的这个接口
package java.lang;import java.util.*;public interface Comparable<T> { public int compareTo(T o);}
//对于Key而言,是数据排序的关键字,因此还需要提供比较两个Key对象的关键字,Key对应类需实现WritableComparable接口,
//源码中也给出了自定义WritableComparable的实例。
package org.apache.hadoop.io * <p>Example:</p> * <p><blockquote><pre> * public class MyWritableComparable implements WritableComparable<MyWritableComparable> { * // 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 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 * } * } * </pre></blockquote></p> */@InterfaceAudience.Public@InterfaceStability.Stablepublic interface WritableComparable<T> extends Writable, Comparable<T> {}
//随便看一个hadoop已经封装好的数据类型LongWritable,该类实现了WritableCpmparable接口。
//Writable里的write()和ReadFields()方法和Comparable里的compareTo()方法都得到了具体实现。
package org.apache.hadoop.io;/** A WritableComparable for longs. */@InterfaceAudience.Public@InterfaceStability.Stablepublic class LongWritable implements WritableComparable<LongWritable> { private long value; public LongWritable() {} public LongWritable(long value) { set(value); } /** Set the value of this LongWritable. */ public void set(long value) { this.value = value; } /** Return the value of this LongWritable. */ public long get() { return value; } @Override public void readFields(DataInput in) throws IOException { value = in.readLong(); } @Override public void write(DataOutput out) throws IOException { out.writeLong(value); } /** Compares two LongWritables. */ @Override public int compareTo(LongWritable o) { long thisValue = this.value; long thatValue = o.value; return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); }}
0 0
- (5-2)Writable源码分析
- Hadoop序列化与Writable源码分析
- Hadoop源码分析:Writable接口和WritableComparable接口
- [hadoop2.7.1]I/O之Writable源码及相关注解
- JQuery 1.10.2源码分析学习5
- nginx源码分析5-方法(2)
- libhdfs源码分析(5)
- Tomcat 5源码分析
- Leveldb源码分析--5
- x264 源码分析 (5)
- Tomcat 5源码分析
- Tomcat 5源码分析
- openfire源码分析---5
- Leveldb源码分析--5
- malloc源码分析---5
- jQuery源码分析5
- red5源码分析---5
- lucene源码分析---5
- POJ 1201 (差分约束)
- C++字符串拷贝与缓冲区溢出
- JAX-RS开发(二):hello world
- 第六周阅读程序
- 使用VideoView播放视频
- (5-2)Writable源码分析
- Android开源项目 分类 值得学习
- Android4.4制作自有SDK探析
- [LeetCode]House Robber
- 为什么构造函数不能声明为虚函数,析构函数可以
- 第六届南桥杯A组Java第二题 星系炸弹
- android常用控件属性(中文)
- 如何更加巧妙地解决所面对的问题
- Q4: N元一次方程组(矩阵)