Source Comparator vs. WritableComparable


compare()and compareTo()work synonymously when it comes to sorting keys, but I just want to know that in the era of highly configured machines, there will be a need to think about when to use compare()and when to usecompareTo()

If you need to think about any scenario that compare(byte b1[],int s1,int l1, byte b2[],int s2,int l2)has an edge over compareTo(object key1,Object key2), then please suggest fields or use cases or types of problems, where do we really need to decide which one to use?

Thanks!

+4
source share
1 answer

Using RawComparator:

, , RawComparator.

Mapper to Reducer. , Reducer from Mapper, .

, 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() WritableComparator:

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
}

+4

Source: https://habr.com/ru/post/1613339/


All Articles