我需要改进我的 MR 工作,我想到的一件事是实现一个定制的 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);
...
}