org.apache.hadoop.io
Class GenericWritable

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

public abstract class GenericWritable
extends Object
implements Writable

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.

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
Author:
Feng Jiang (Feng.a.Jiang@gmail.com)

Constructor Summary
GenericWritable()
           
 
Method Summary
 Writable get()
          Return the wrapped instance.
protected abstract  Class[] getTypes()
          Return all classes that may be wrapped.
 void readFields(DataInput in)
          Reads the fields of this object from in.
 void set(Writable obj)
          Set the instance that is wrapped.
 void write(DataOutput out)
          Writes the fields of this object to out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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.


readFields

public void readFields(DataInput in)
                throws IOException
Description copied from interface: Writable
Reads 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
Throws:
IOException

write

public void write(DataOutput out)
           throws IOException
Description copied from interface: Writable
Writes the fields of this object to out.

Specified by:
write in interface Writable
Throws:
IOException

getTypes

protected abstract Class[] getTypes()
Return all classes that may be wrapped. Subclasses should implement this to return a constant array of classes.



Copyright © 2006 The Apache Software Foundation