WritableComparable interface is just a subinterface of the Writable and java.lang.Comparable interfaces. For implementing a WritableComparable we must have compareTo method apart from readFields and write methods, as shown below:
public interface WritableComparable extends Writable, ComparableComparison of types is crucial for MapReduce, where there is a sorting phase during which keys are compared with one another.
{
void readFields(DataInput in);
void write(DataOutput out);
int compareTo(WritableComparable o)
}
The code for IntPair class which is used in In-mapper Combiner Program to Calculate Average post is given below:
As you can see in compareTo(IntPair tp) of above class that IntPair needs to be deserialized for comparison to take place, we can implement a RawComparator which can compare two keys by just checking their serialized representation. More on RawComparator is available in Hadoop: The Definitive Guide.
How to add a RowComparator Or Writable Comparator to this Example ?
ReplyDeleteCould you please illustrate implementation of WritableComparator implementation on a bit complex custom data type, let say, the data type has 4 fields, Intwritable, Text, MapWritable, Text ?