我创建了一个hadoop作业,其中我用我创建的比较器类(PathDateComparator
)job.setGroupingComparatorClass
。当运行作业时,我可以看到PathDateComparator
的构造函数被调用,但compare函数没有。
这是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);
}
}
您引用的类(此处为PathDateComparator)需要是RawComparator的实现者。不过,compare方法看起来不错。其思想是,该类仅用于比较编码为字节数组的键值。