(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
原创粉丝点击