mapreduce自定义排序

来源:互联网 发布:招商网站留言数据提取 编辑:程序博客网 时间:2024/06/06 00:30

我们进入mapreduce中的对象中可以看到类中有一个compareTo的方法,用于比较作为key值的前后两个对象
自定义排序即自己定义新的对象,重写compareTo方法
例如:

`private static class MyNewKey implements WritableComparable {
long firstNum;
long secondNum;

    public MyNewKey() {    }    public MyNewKey(long first, long second) {        firstNum = first;        secondNum = second;    }    @Override    public void write(DataOutput out) throws IOException {        out.writeLong(firstNum);        out.writeLong(secondNum);    }    @Override    public void readFields(DataInput in) throws IOException {        firstNum = in.readLong();        secondNum = in.readLong();    }    /*     * 当key进行排序时会调用以下这个compreTo方法     */    @Override    public int compareTo(MyNewKey anotherKey) {        long min = firstNum - anotherKey.firstNum;        if (min != 0) {            // 说明第一列不相等,则返回两数之间小的数            return (int) min;        } else {            return (int) (secondNum - anotherKey.secondNum);        }    }}`