In Hadoop Secondary sort the code in Composite has the following method to compare values, the Composite key class implements WritableComparable
:-
@Override
public int compareTo(CustomKey o) {
int result = firstName.compareTo(o.getFirstName());
log.debug("value is " + result);
if (result == 0) {
return lastName.compareTo(o.getLastName());
}
return result;
}
In the custom sorter that we create to perform secondary sort which extends WritableComparator
and the code goes like this :-
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
CustomKey key1 = (CustomKey) w1;
CustomKey key2 = (CustomKey) w2;
int value = key1.getFirstName().compareTo(key2.getFirstName());
if (value == 0) {
return -key1.getLastName().compareTo(key2.getLastName());
}
return value;
}
I want to know why we are comparing values twice for sorting once in CustomKey
class by implementing WritableComparable
and then we create one CustomSorter
class again to sort the value by extending WritableComparator
.