Hadoop Writable interface

Hadoop 利用继承 Writable 接口的类来进行 MapReduce 的运算 从输入到输出要用到,因此对于 MapReduce 的性能是有影响的。实现该接口的类应该覆盖 read write ,分别用来进行反序列化和序列化操做。

下面是Hadoop API javadoc给出的例子 java

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;
       }
     }
相关文章
相关标签/搜索