Hadoop 要使一个类能序例化, 要实现Writable接口, Writable 调用DataInput和DataOutput实现序例化。

DataOutput是JDK中IO包下的一个类, 提供了writeBoolean, writeByte, writeShort。等方法了。

这样让用户决定哪一个字段序例化, 怎么反序例化。

在org.apache.hadoop.io包下包含了大量的可序列化的组件,它们都实现了Writable接口,Writable接口提供了两个方法,write和readFields,分别用来序列化和反序列化。

  1. /**  
  2.  * Licensed to the Apache Software Foundation (ASF) under one  
  3.  * or more contributor license agreements.  See the NOTICE file  
  4.  * distributed with this work for additional information  
  5.  * regarding copyright ownership.  The ASF licenses this file  
  6.  * to you under the Apache License, Version 2.0 (the  
  7.  * "License"); you may not use this file except in compliance  
  8.  * with the License.  You may obtain a copy of the License at  
  9.  *  
  10.  *     http://www.apache.org/licenses/LICENSE-2.0  
  11.  *  
  12.  * Unless required by applicable law or agreed to in writing, software  
  13.  * distributed under the License is distributed on an "AS IS" BASIS,  
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15.  * See the License for the specific language governing permissions and  
  16.  * limitations under the License.  
  17.  */  
  18.   
  19. package org.apache.hadoop.io;   
  20.   
  21. import java.io.DataOutput;   
  22. import java.io.DataInput;   
  23. import java.io.IOException;   
  24.   
  25. import org.apache.hadoop.classification.InterfaceAudience;   
  26. import org.apache.hadoop.classification.InterfaceStability;   
  27.   
  28. /**  
  29.  * A serializable object which implements a simple, efficient, serialization   
  30.  * protocol, based on {@link DataInput} and {@link DataOutput}.  
  31.  *  
  32.  * <p>Any <code>key</code> or <code>value</code> type in the Hadoop Map-Reduce  
  33.  * framework implements this interface.</p>  
  34.  *   
  35.  * <p>Implementations typically implement a static <code>read(DataInput)</code>  
  36.  * method which constructs a new instance, calls {@link #readFields(DataInput)}   
  37.  * and returns the instance.</p>  
  38.  *   
  39.  * <p>Example:</p>  
  40.  * <p><blockquote><pre>  
  41.  *     public class MyWritable implements Writable {  
  42.  *       // Some data       
  43.  *       private int counter;  
  44.  *       private long timestamp;  
  45.  *         
  46.  *       public void write(DataOutput out) throws IOException {  
  47.  *         out.writeInt(counter);  
  48.  *         out.writeLong(timestamp);  
  49.  *       }  
  50.  *         
  51.  *       public void readFields(DataInput in) throws IOException {  
  52.  *         counter = in.readInt();  
  53.  *         timestamp = in.readLong();  
  54.  *       }  
  55.  *         
  56.  *       public static MyWritable read(DataInput in) throws IOException {  
  57.  *         MyWritable w = new MyWritable();  
  58.  *         w.readFields(in);  
  59.  *         return w;  
  60.  *       }  
  61.  *     }  
  62.  * </pre></blockquote></p>  
  63.  */  
  64. @InterfaceAudience.Public   
  65. @InterfaceStability.Stable   
  66. public interface Writable {   
  67.   /**   
  68.    * Serialize the fields of this object to <code>out</code>.  
  69.    *   
  70.    * @param out <code>DataOuput</code> to serialize this object into.  
  71.    * @throws IOException  
  72.    */  
  73.   void write(DataOutput out) throws IOException;   
  74.   
  75.   /**   
  76.    * Deserialize the fields of this object from <code>in</code>.    
  77.    *   
  78.    * <p>For efficiency, implementations should attempt to re-use storage in the   
  79.    * existing object where possible.</p>  
  80.    *   
  81.    * @param in <code>DataInput</code> to deseriablize this object from.  
  82.    * @throws IOException  
  83.    */  
  84.   void readFields(DataInput in) throws IOException;   
  85. }