Class GenericWritable

java.lang.Object
org.apache.hadoop.io.GenericWritable
All Implemented Interfaces:
Configurable, Writable

@Public @Stable public abstract class GenericWritable extends Object implements Writable, Configurable
A wrapper for Writable instances.

When two sequence files, which have same Key type but different Value types, are mapped out to reduce, multiple Value types is not allowed. In this case, this class can help you wrap instances with different types.

Compared with ObjectWritable, this class is much more effective, because ObjectWritable will append the class declaration as a String into the output file in every Key-Value pair.

Generic Writable implements Configurable interface, so that it will be configured by the framework. The configuration is passed to the wrapped objects implementing Configurable interface before deserialization.

how to use it:
1. Write your own class, such as GenericObject, which extends GenericWritable.
2. Implements the abstract method getTypes(), defines the classes which will be wrapped in GenericObject in application. Attention: this classes defined in getTypes() method, must implement Writable interface.

The code looks like this:
 public class GenericObject extends GenericWritable {
 
   private static Class[] CLASSES = {
               ClassType1.class, 
               ClassType2.class,
               ClassType3.class,
               };

   protected Class[] getTypes() {
       return CLASSES;
   }

 }
 
Since:
Nov 8, 2006
  • Constructor Details

    • GenericWritable

      public GenericWritable()
  • Method Details

    • set

      public void set(Writable obj)
      Set the instance that is wrapped.
      Parameters:
      obj - input obj.
    • get

      public Writable get()
      Return the wrapped instance.
      Returns:
      the wrapped instance.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • readFields

      public void readFields(DataInput in) throws IOException
      Description copied from interface: Writable
      Deserialize the fields of this object from in.

      For efficiency, implementations should attempt to re-use storage in the existing object where possible.

      Specified by:
      readFields in interface Writable
      Parameters:
      in - DataInput to deseriablize this object from.
      Throws:
      IOException - any other problem for readFields.
    • write

      public void write(DataOutput out) throws IOException
      Description copied from interface: Writable
      Serialize the fields of this object to out.
      Specified by:
      write in interface Writable
      Parameters:
      out - DataOuput to serialize this object into.
      Throws:
      IOException - any other problem for write.
    • getTypes

      protected abstract Class<? extends Writable>[] getTypes()
      Return all classes that may be wrapped. Subclasses should implement this to return a constant array of classes.
      Returns:
      all classes that may be wrapped.
    • getConf

      public Configuration getConf()
      Description copied from interface: Configurable
      Return the configuration used by this object.
      Specified by:
      getConf in interface Configurable
      Returns:
      Configuration
    • setConf

      public void setConf(Configuration conf)
      Description copied from interface: Configurable
      Set the configuration to be used by this object.
      Specified by:
      setConf in interface Configurable
      Parameters:
      conf - configuration to be used