Database Reference
In-Depth Information
icWritable
to wrap the elements in an
ArrayWritable
. Alternatively, you could
write a general
ListWritable
using the ideas from
MapWritable
.
Implementing a Custom Writable
Hadoop comes with a useful set of
Writable
implementations that serve most purposes;
however, on occasion, you may need to write your own custom implementation. With a
custom
Writable
, you have full control over the binary representation and the sort or-
der. Because
Writable
s are at the heart of the MapReduce data path, tuning the binary
representation can have a significant effect on performance. The stock
Writable
imple-
mentations that come with Hadoop are well tuned, but for more elaborate structures, it is
often better to create a new
Writable
type rather than composing the stock types.
TIP
If you are considering writing a custom
Writable
, it may be worth trying another serialization frame-
work, like Avro, that allows you to define custom types declaratively. See
Serialization Frameworks
and
To demonstrate how to create a custom
Writable
, we shall write an implementation
that represents a pair of strings, called
TextPair
. The basic implementation is shown in
Example 5-7. A Writable implementation that stores a pair of Text objects
import
java.io.*
;
import
org.apache.hadoop.io.*
;
public class
TextPair
implements
WritableComparable
<
TextPair
> {
private
Text first
;
private
Text second
;
public
TextPair
() {
set
(
new
Text
(),
new
Text
());
}
public
TextPair
(
String first
,
String second
) {
set
(
new
Text
(
first
),
new
Text
(
second
));
}
public
TextPair
(
Text first
,
Text second
) {