你可以像这样组织你的 map/reduce 计算:
地图输入:默认
地图输出:“键:数字,值:单词”
_ 按键排序阶段 _
在这里,您将需要覆盖默认排序器以按降序排序。
减少 - 1个减速器
减少输入:“键:数字,值:单词”
减少输出:“key: word, value: (number, rank)”
保留一个全局计数器。对于每个键值对,通过递增计数器来添加排名。
编辑:这是自定义后代排序器的代码片段:
public static class IntComparator extends WritableComparator {
public IntComparator() {
super(IntWritable.class);
}
@Override
public int compare(byte[] b1, int s1, int l1,
byte[] b2, int s2, int l2) {
Integer v1 = ByteBuffer.wrap(b1, s1, l1).getInt();
Integer v2 = ByteBuffer.wrap(b2, s2, l2).getInt();
return v1.compareTo(v2) * (-1);
}
}
不要忘记将其实际设置为您工作的比较器:
job.setSortComparatorClass(IntComparator.class);