【发布时间】:2015-06-28 11:50:09
【问题描述】:
我已经实现了 hadoop 排序比较器类来对我的键进行排序。我知道它用来比较每个键。但是,我不知道它如何详细工作?如果用来比较的话,是这样吗?
谢谢大家....
【问题讨论】:
标签: sorting hadoop mapreduce compare comparator
我已经实现了 hadoop 排序比较器类来对我的键进行排序。我知道它用来比较每个键。但是,我不知道它如何详细工作?如果用来比较的话,是这样吗?
谢谢大家....
【问题讨论】:
标签: sorting hadoop mapreduce compare comparator
说,你的密钥是(Attribute1, Attribute2)。现在您可以使用排序比较器,先按 Attribute1 排序,然后按 Attribute2。
例如,
Key = (2008,32) // year, temperature
现在,如果您想按年份然后按温度排序,您可以使用排序比较器,如下所示:
public static class KeyComparator extends WritableComparator {
protected KeyComparator() {
super(CompositeKey.class, true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
CompositeKey ip1 = (CompositeKey) w1;
CompositeKey ip2 = (CompositeKey) w2;
int result = CompositeKey.compare(ip1.getYear(), ip2.getYear());
if (result != 0) {
return result;
}
return CompositeKey.compare(ip1.getTemperature(), ip2.getTemperature());
}
}
【讨论】: