1

私は MR ジョブを改善する必要があります。私が考えていることの 1 つは、カスタマイズされた rawComparator を実装することですが、私の主要なクラスには、いくつかの int フィールド以外に文字列として多くのフィールドがあります。バイトから文字列フィールドを解析する方法がわかりません[] 、

私のキークラス

public GeneralKey {
  private int day;
  private int hour;
  private String type;
  private String name;
  ..
}

私のカスタマイズした rawComparator:

public class GeneralKeyComparator extends WritableComparator {
    private static final Text.Comparator TEXT_COMPARATOR = new Text.Comparator();

    protected GeneralKeyComparator() {
        super(GeneralKey.class);
    }

    @Override
    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
        int day1 = readInt(b1, s1);
        int day2 = readInt(b2, s2);

        int comp = (intDay1 < intDay2) ? -1 : (intDay1 == intDay2) ? 0 : 1;
        if (0 != comp) {
            return comp;
        }

        int hr1 = readInt(b1, s1+4);
        int hr2 = readInt(b2, s2+4);
        comp = (hr1 < hr2) ? -1 : (hr1 == hr2) ? 0 : 1;

            .... how to compare the String fields here???   

        return comp;
    }

周りのグーグルは、人々がこれを試したことを発見しました:

try {
    int firstL1 = WritableUtils.decodeVIntSize(b1[s1]) + readInt(b1, s1+8);
    int firstL2 = WritableUtils.decodeVIntSize(b2[s2]) + readVInt(b2, s2+8);
    comp = TEXT_COMPARATOR.compare(b1, s1, firstL1, b2, s2, firstL2);

} catch (IOException e) {
    throw new IllegalArgumentException(e);
}

しかし、私はこれがどのように機能するのか理解できず、私の場合は機能しないと思います。誰か助けてもらえますか? ありがとう

ここに readField() および write() メソッドを追加しました。

public void readFields(DataInput input) throws IOException {
    intDay = input.readInt();
    hr = input.readInt();
    type = input.readUTF();
    name = input.readUTF();
    ...
    }

@Override
public void write(DataOutput output) throws IOException {
    output.writeInt(intDay);
    output.writeInt(hr);
    output.writeUTF(type);
    output.writeUTF(name);
            ...
    }
4

1 に答える 1