我刚开始学习hadoop,并使用自定义分区程序和比较器运行hadoop map-reduce程序。我面临的问题是主要和次要排序没有在复合键上完成,更多 - 在一个部分复合键正在与其他compsite-key部分进行更改。
例如我在mapper中创建以下键
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中键的顺序和内容如下:
//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
尝试长时间解决但尚未成功,任何帮助都表示赞赏?
答案 0 :(得分:0)
终于搞定了,实际上我改变了
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.