我刚刚开始学习hadoop,并使用自定义partitioner和comparator运行hadoop map reduce程序。我面临的问题是,主要和次要排序没有在复合键上完成,一个复合键的更多部分正在与其他复合键部分一起更改。
例如,我正在映射程序中创建以下密钥
key1 -> tagA,1
key2 -> tagA,1
key3 -> tagA,1
key4 -> tagA,1
key5 -> tagA,2
key6 -> tagA,2
key7 -> tagB,1
key8 -> tagB,1
key9 -> tagB,1
key10 -> tagB,1
key11 -> tagB,2
key12 -> tagB,2
以及分割器和组合器如下
//Partitioner
public static class TaggedJoiningPartitioner implements Partitioner<Text, Text> {
@Override
public int getPartition(Text key, Text value, int numPartitions) {
String line = key.toString();
String tokens[] = line.split(",");
return (tokens[0].hashCode() & Integer.MAX_VALUE)% numPartitions;
}
@Override
public void configure(JobConf arg0) {
// TODO Auto-generated method stub //NOT OVERRIDING THIS METHOD
}
}
//Comparator
public static class TaggedJoiningGroupingComparator extends WritableComparator {
public TaggedJoiningGroupingComparator() {
super(Text.class, true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
String taggedKey1[] = ((Text)a).toString().split(",");
String taggedKey2[] = ((Text)b).toString().split(",");
return taggedKey1[0].compareTo(taggedKey2[0]);
}
}
在reducer中,这些键根据标签正确分组,但没有正确排序。减速器中密钥的顺序和内容如下:
//REDUCER 1
key1 -> tagA,1
key2 -> tagA,1
key3 -> tagA,1
key5 -> tagA,1 //2 changed by 1 here
key6 -> tagA,1 //2 changed by 1 here
key4 -> tagA,1
//REDUCER 2
key7 -> tagB,1
key11 -> tagB,1 //2 changed by 1 here
key12 -> tagB,1 //2 changed by 1 here
key8 -> tagB,1
key9 -> tagB,1
key10 -> tagB,1
花了很长时间试图解决这个问题,但还没有成功,有什么帮助吗?
终于让它工作起来了,实际上我改变了
conf.setOutputKeyComparatorClass(TaggedJoiningGroupingComparator.class);
至
conf.setOutputValueGroupingComparator(TaggedJoiningGroupingComparator.class);
同样来自hadoop API文档。——
setOutputValueGroupingComparator(Class<? extends RawComparator> theClass)
Set the user defined RawComparator comparator for grouping keys in the input to the reduce.