Hadoop GroupingComparatorClass.compare方法未被调用

时间:2013-08-13 22:10:19

标签: java hadoop mapreduce

我创建了一个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);
    }
}

1 个答案:

答案 0 :(得分:0)

您引用的类(PathDateComparator,此处)需要是RawComparator的实现者。但是,比较方法看起来是正确的。这个想法是该类仅用于比较编码为字节数组的键值。