未调用hadoop groupingcomparatorclass.compare方法

qpgpyjmq  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(286)

我创建了一个hadoop作业 job.setGroupingComparatorClass 使用比较器类( PathDateComparator )我创造的。当运行作业时,我可以看到 PathDateComparator 正在被调用,但比较函数未被调用。
这是你的名字 PathDateComparator 班级:

public class PathDateComparator extends WritableComparator {

    protected PathDateComparator() {
        super(Text.class, true);
        System.out.println("PathDateComparator");
    }

    private String getPath(String s) {
        System.out.println("s:" + s);
        String[] temp = s.split(IOUtil.tab);
        return temp[0];
    }

    @Override
    public int compare(Object o1, Object o2) {
        String k1 = ((Text) o1).toString();
        System.out.println("k1:" + k1);
        String path1 = getPath(k1);
        System.out.println("path1:" + path1);

        String k2 = ((Text) o2).toString();
        System.out.println("k2:" + k2);
        String path2 = getPath(k2);
        System.out.println("path2:" + path2);

        return path1.compareTo(path2);
    }

    @Override
    public int compare(WritableComparable o1, WritableComparable o2) {
        String k1 = ((Text) o1).toString();
        System.out.println("k1:" + k1);
        String path1 = getPath(k1);
        System.out.println("path1:" + path1);

        String k2 = ((Text) o2).toString();
        System.out.println("k2:" + k2);
        String path2 = getPath(k2);
        System.out.println("path2:" + path2);

        return path1.compareTo(path2);
    }

    @Override
    public int compare(byte[] bArray1, int offset1, int length1,
            byte[] bArray2, int offset2, int length2) {

        String k1 = new String(Arrays.copyOfRange(bArray1, offset1, offset1 + length1));
        System.out.println("k1:" + k1);
        String path1 = getPath(k1);
        System.out.println("path1:" + path1);

        String k2 = new String(Arrays.copyOfRange(bArray2, offset2, offset2 + length2));
        System.out.println("k2:" + k2);
        String path2 = getPath(k2);
        System.out.println("path2:" + path2);

        return path1.compareTo(path2);
    }
}
yx2lnoni

yx2lnoni1#

您引用的类(这里的pathdatecomparator)需要是rawcomparator的实现者。不过,compare方法看起来是正确的。其思想是该类仅用于比较编码为字节数组的键值。

相关问题