我想知道如何更改減少任務後,我簡單的WordCount程序的排序順序?我已經制作了另一張地圖,通過按鍵進行價值排序,但仍然按升序排列。 有沒有一個簡單的方法來做到這一點(改變排序順序)?
感謝 Vellozo
我想知道如何更改減少任務後,我簡單的WordCount程序的排序順序?我已經制作了另一張地圖,通過按鍵進行價值排序,但仍然按升序排列。 有沒有一個簡單的方法來做到這一點(改變排序順序)?
感謝 Vellozo
如果您使用的是較舊的API(mapred.*
),然後設置OutputKeyComparatorClass作業的conf:
jobConf.setOutputKeyComparatorClass(ReverseComparator.class);
ReverseComparator可以是這樣的:
static class ReverseComparator extends WritableComparator {
private static final Text.Comparator TEXT_COMPARATOR = new Text.Comparator();
public ReverseComparator() {
super(Text.class);
}
@Override
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
try {
return (-1)* TEXT_COMPARATOR
.compare(b1, s1, l1, b2, s2, l2);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
if (a instanceof Text && b instanceof Text) {
return (-1)*(((Text) a)
.compareTo((Text) b)));
}
return super.compare(a, b);
}
}
在新的API(mapreduce.*
)中,我認爲您需要使用Job.setSortComparator()方法。
小錯誤...您沒有定義「firstL1」和「firstL2」變量。我想你的意思是說'l1'和'l2' – 2012-03-01 04:53:20
Thx傢伙......我找到的解決方案几乎與這個Raze2dust相同! 謝謝你! – Vellozo 2012-03-01 10:32:35
@PradeepGollakota謝謝,修正了它.. – 2012-03-01 13:34:51
這一個幾乎是和上面一樣,只是看起來有點簡單
class MyKeyComparator extends WritableComparator {
protected DescendingKeyComparator() {
super(Text.class, true);
}
@SuppressWarnings("rawtypes")
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
Text key1 = (Text) w1;
Text key2 = (Text) w2;
return -1 * key1.compareTo(key2);
}
}
然後將其添加到工作
job.setSortComparatorClass(MyKeyComparator.class);
Text key1 = (Text) w1;
Text key2 = (Text) w2;
您可以根據您的使用更改上述文本類型。
解決! http://hadoop.sourcearchive.com/documentation/0.20.2plus-pdfsg1-1/TestComparators_8java-source.html – Vellozo 2012-02-29 06:03:58