Package org.apache.hadoop.io
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.
1. Write your own class, such as GenericObject, which extends GenericWritable.
2. Implements the abstract method
The code looks like this:
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.
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionget()Return the wrapped instance.getConf()Return the configuration used by this object.getTypes()Return all classes that may be wrapped.voidreadFields(DataInput in) Deserialize the fields of this object fromin.voidSet the instance that is wrapped.voidsetConf(Configuration conf) Set the configuration to be used by this object.toString()voidwrite(DataOutput out) Serialize the fields of this object toout.
-
Constructor Details
-
GenericWritable
public GenericWritable()
-
-
Method Details
-
set
Set the instance that is wrapped.- Parameters:
obj- input obj.
-
get
Return the wrapped instance.- Returns:
- the wrapped instance.
-
toString
-
readFields
Description copied from interface:WritableDeserialize the fields of this object fromin.For efficiency, implementations should attempt to re-use storage in the existing object where possible.
- Specified by:
readFieldsin interfaceWritable- Parameters:
in-DataInputto deseriablize this object from.- Throws:
IOException- any other problem for readFields.
-
write
Description copied from interface:WritableSerialize the fields of this object toout.- Specified by:
writein interfaceWritable- Parameters:
out-DataOuputto serialize this object into.- Throws:
IOException- any other problem for write.
-
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
Description copied from interface:ConfigurableReturn the configuration used by this object.- Specified by:
getConfin interfaceConfigurable- Returns:
- Configuration
-
setConf
Description copied from interface:ConfigurableSet the configuration to be used by this object.- Specified by:
setConfin interfaceConfigurable- Parameters:
conf- configuration to be used
-