org.apache.hadoop.io
Class GenericWritable

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

@InterfaceAudience.Public
@InterfaceStability.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 Summary
GenericWritable()
           
 
Method Summary
 Writable get()
          Return the wrapped instance.
 Configuration getConf()
          Return the configuration used by this object.
protected abstract  Class<? extends Writable>[] getTypes()
          Return all classes that may be wrapped.
 void readFields(DataInput in)
          Deserialize the fields of this object from in.
 void set(Writable obj)
          Set the instance that is wrapped.
 void setConf(Configuration conf)
          Set the configuration to be used by this object.
 String toString()
           
 void write(DataOutput out)
          Serialize the fields of this object to out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GenericWritable

public GenericWritable()
Method Detail

set

public void set(Writable obj)
Set the instance that is wrapped.

Parameters:
obj -

get

public Writable get()
Return 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

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

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.


getConf

public Configuration getConf()
Description copied from interface: Configurable
Return the configuration used by this object.

Specified by:
getConf in interface Configurable

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


Copyright © 2014 Apache Software Foundation. All Rights Reserved.