Reverse Sorting Reducer Keys

偶尔善良 提交于 2019-12-03 08:38:13

In Hadoop 1.X, you can specify a custom comparator class for your outputs using JobConf.setOutputKeyComparatorClass.

Your comparator must implement the RawComparator interface.

With Hadoop 2.X, this is done by using Job.setSortComparatorClass, still with an implementation of RawComparator.

Sample, simple code

class MyKeyComparator extends WritableComparator {
    protected DescendingKeyComparator() {
        super(Text.class, true);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public int compare(WritableComparable w1, WritableComparable w2) {
        Text key1 = (Text) w1;
        Text key2 = (Text) w2;          
        return -1 * key1.compareTo(key2);
    }
}

Then add it it to the job

job.setSortComparatorClass(MyKeyComparator.class);

you can change the below text type as per ur use.

Text key1 = (Text) w1;
Text key2 = (Text) w2; 

You can multiply your key by -1 before emitting it from your mapper. This will cause the framework to sort it in ascending order but negative values -5,-4,-3,-2,-1 and then in the reducer multiply it again by -1 resulting in 5,4,3,2,1. This will cause the framework to sort in sudo-descending order. In a more complex sort it is best to write a custom class for comparing and then set it in your driver class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!