org.apache.hadoop.io
abstract public class: GenericWritable [javadoc |
source]
java.lang.Object
org.apache.hadoop.io.GenericWritable
All Implemented Interfaces:
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;
}
}
| Method from org.apache.hadoop.io.GenericWritable Detail: |
public Writable get() {
return instance;
}
Return the wrapped instance. |
abstract protected Class[] getTypes()
Return all classes that may be wrapped. Subclasses should implement this
to return a constant array of classes. |
public void readFields(DataInput in) throws IOException {
type = in.readByte();
Class< ? extends Writable > clazz = getTypes()[type & 0xff];
try {
instance = clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
throw new IOException("Cannot initialize the class: " + clazz);
}
instance.readFields(in);
}
|
public void set(Writable obj) {
instance = obj;
Class[] clazzes = getTypes();
for (int i = 0; i < clazzes.length; i++) {
Class clazz = clazzes[i];
if (clazz.isInstance(instance)) {
type = (byte) i;
return;
}
}
throw new RuntimeException("The type of instance is: "
+ instance.getClass() + ", which is NOT registered.");
}
Set the instance that is wrapped. |
public void write(DataOutput out) throws IOException {
if (type == NOT_SET || instance == null)
throw new IOException("The GenericWritable has NOT been set correctly. type="
+ type + ", instance=" + instance);
out.writeByte(type);
instance.write(out);
}
|