hadoop排序比较器类的用途是什么?

z9ju0rcb  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(406)

我已经实现了hadoop排序比较类来排序我的密钥。我知道它用来比较每把钥匙。但是,我不知道它是怎么运作的?如果用它来比较,这是真的吗?
谢谢大家。。。。

wbgh16ku

wbgh16ku1#

你的钥匙是 (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());
    }
}

相关问题