原始比较器与可写可比




如果我们谈论排序键,compare()compareTo()协同工作,但我只想知道,在高度配置的机器时代,是否需要考虑何时使用compare()以及何时使用compareTo()

如果需要考虑compare(byte b1[],int s1,int l1, byte b2[],int s2,int l2)compareTo(object key1,Object key2)具有优势的任何场景,那么请建议我们真正需要决定使用哪一个的字段或用例或问题类型?

谢谢!!

RawComparator 的使用:

如果您仍然想优化Map Reduce Job所花费的时间,则必须使用RawComparator。

中间键值对已从映射器传递到化简器。

在这些值从映射器到达化简器之前,将执行随机和排序步骤。

排序得到了改进,因为 RawComparator 将按字节比较键。如果我们不使用 RawComparator,则必须完全反序列化中间密钥才能进行比较。

例:

public class IndexPairComparator extends WritableComparator {
protected IndexPairComparator() {
    super(IndexPair.class);
}
@Override
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
    int i1 = readInt(b1, s1);
    int i2 = readInt(b2, s2);
    int comp = (i1 < i2) ? -1 : (i1 == i2) ? 0 : 1;
    if(0 != comp)
        return comp;
    int j1 = readInt(b1, s1+4);
    int j2 = readInt(b2, s2+4);
    comp = (j1 < j2) ? -1 : (j1 == j2) ? 0 : 1;
    return comp;
}

}

在上面的例子中,我们没有直接实现RawComparator。相反,我们扩展了WritableComparator,它在内部实现了RawComparator。

看看这篇文章 Jee Vang

可写比较器中实现 RawComparator((:只需比较键

public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
     try {
      buffer.reset(b1, s1, l1);                   // parse key1
      key1.readFields(buffer);
      buffer.reset(b2, s2, l2);                   // parse key2
      key2.readFields(buffer);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return compare(key1, key2);                   // compare them
}

查看来源

最新更新