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;
       }
     }
相關文章
相關標籤/搜索