java - 复合 key 正在更改,Hadoop Map-Reduce?

标签 java hadoop mapreduce hadoop-partitioning

我刚刚开始学习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 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.

关于java - 复合 key 正在更改,Hadoop Map-Reduce?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26072962/

相关文章:

java - Hadoop> WordCount按值排序

java - 在 Java 中解析 ISO 8601 日期格式,如 2015-06-27T13 :16:37. 363Z

csv - 将 csv 文件加载到 Hive 表

hadoop - Hadoop-如何在运行另一个mapreduce作业的同时运行它?

hadoop - Spark RDD 操作

java - Mapreduce-FloatArrayWritable打印地址

java - Java 8 中的 map() 和 flatMap() 方法有什么区别?

java - 从 Json 路径查询返回并在 Junit 断言中使用的字符串的排序

hadoop - 瘤胃和定制柜台

hadoop - Cassandra 和 MapReduce - 最低设置要求