码迷,mamicode.com
首页 > 其他好文 > 详细

踏着前人的脚印学Hadoop——序列化,Writerable

时间:2014-12-31 22:46:58      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

package org.apache.hadoop.io;

import java.io.DataOutput;
import java.io.DataInput;
import java.io.IOException;

/**
* A serializable object which implements a simple, efficient,

一个序列化的对象,这个家伙实现了一个简单、高效、序列化的协议,它是基于DataInput和DataOutput这两个IO对象的
* protocol, based on {@link DataInput} and {@link DataOutput}.
*
* <p>Any <code>key</code> or <code>value</code> type in the Hadoop Map-Reduce

在hadoop Map-Reduce框架中任何基于key-value类型的的数据都是实现的这个接口
* framework implements this interface.</p>
*
* <p>Implementations typically implement a static <code>read(DataInput)</code>
* method which constructs a new instance, calls {@link #readFields(DataInput)}
* and returns the instance.</p>
*
* <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>
*/
public interface Writable {
  /**
   * Serialize the fields of this object to <code>out</code>.
   *
   * @param out <code>DataOuput</code> to serialize this object into.
   * @throws IOException
   */
  void write(DataOutput out) throws IOException;

  /**
   * Deserialize the fields of this object from <code>in</code>. 
   *
   * <p>For efficiency, implementations should attempt to re-use storage in the
   * existing object where possible.</p>
   *
   * @param in <code>DataInput</code> to deseriablize this object from.
   * @throws IOException
   */
  void readFields(DataInput in) throws IOException;
}

 

-----------------------------------------------------------------------------------------------------------------------------------

int
readInt()
          读取四个输入字节并返回一个 int 值。

long
readLong()
          读取八个输入字节并返回一个 long 值。

 

-----------------------------------------------------------------------------------------------------------------------------------

 

void
writeInt(int v)
          将一个 int 值写入输出流,该值由四个字节组成。

void
writeLong(long v)
          将一个 long 值写入输出流,该值由八个字节组成。

 

-----------------------------------------------------------------------------------------------------------------------------------------


/** A polymorphic Writable that writes an instance with it‘s class name.
* Handles arrays, strings and primitive types without a Writable wrapper.
*/
public class ObjectWritable implements Writable, Configurable {

  private Class declaredClass;
  private Object instance;
  private Configuration conf;

 

-------------------------------------------------------------------------------------------------------------------------------------------

Writable writable = WritableFactories.newInstance(instanceClass, conf);
      writable.readFields(in);
      instance = writable;

踏着前人的脚印学Hadoop——序列化,Writerable

标签:

原文地址:http://www.cnblogs.com/shunliu-java/p/4196706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!