This output should be largely self-explanatory. We create various Writable wrapper objects and show their general usage. There are several key points:
As mentioned, there is no type-safety beyond Writable itself. So it is possible to have an array or map that holds multiple types, as shown previously.
We can use autounboxing, for example, by supplying an Integer object to methods on IntWritable that expect an int variable.
The inner class demonstrates what is needed if an ArrayWritable class is to be used as an input to a reduce function; a subclass with such a default constructor must be defined.
Other wrapper classes
CompressedWritable: This is a base class to allow for large objects that should remain compressed until their attributes are explicitly accessed
ObjectWritable: This is a general-purpose generic object wrapper
NullWritable: This is a singleton object representation of a null value
VersionedWritable: This is a base implementation to allow writable classes to track versions over time
Have a go hero – playing with Writables
Write a class that exercises the NullWritable and ObjectWritable classes in the same way as it does in the previous examples.
Making your own
As you have seen from the Writable and Comparable interfaces, the required methods are pretty straightforward; don't be afraid of adding this functionality if you want to use your own custom classes as keys or values within a MapReduce job.