【问题标题】:Secondary Sort in HadoopHadoop中的二级排序
【发布时间】:2016-12-10 22:12:53
【问题描述】:

我正在做一个hadoop项目,在多次访问各种博客和阅读文档后,我意识到我需要使用hadoop框架提供的二次排序功能。

我的输入格式是:

DESC(String) Price(Integer) and some other Text

我希望 reducer 中的值按价格降序排列。 此外,在比较 DESC 时,我有一个方法,它采用两个字符串和一个百分比,如果两个字符串之间的相似度等于或大于百分比,那么我应该认为它们相等。

问题是在Reduce Job完成后,我可以看到一些与其他字符串相似的DESC,但它们在不同的组中。

这是我的复合键的 compareTo 方法

public int compareTo(VendorKey o) {
    int result =-
    result = compare(token, o.token, ":") >= percentage ? 0:1;
    if (result == 0) {
        return pid> o.pid  ?-1: pid < o.pid ?1:0;
    }
    return result;
}

分组比较器的比较方法

public int compare(WritableComparable a, WritableComparable b) {
    VendorKey one = (VendorKey) a;
    VendorKey two = (VendorKey) b;
    int result = ClusterUtil.compare(one.getToken(), two.getToken(), ":") >= one.getPercentage() ? 0 : 1;
    // if (result != 0)
    // return two.getToken().compareTo(one.getToken());
    return result;
}

【问题讨论】:

  • 修复 compareTo 方法对你有用吗?

标签: java hadoop mapreduce hadoop2 hadoop-partitioning


【解决方案1】:

您的compareTo 方法似乎违反了要求sgn(x.compareTo(y)) 等于-sgn(y.compareTo(x)) 的常见contract

【讨论】:

    【解决方案2】:

    在您的 customWritable 之后,给一个带有复合键和 NullWritable 值的基本分区器。例如:

    public class SecondarySortBasicPartitioner extends
        Partitioner<CompositeKeyWritable, NullWritable> {
    
        public int getPartition(CompositeKeyWritable key, NullWritable value,
                int numReduceTasks) {
    
            return (key.DEPT().hashCode() % numReduceTasks);
        }
    }
    

    在此之后指定 Key 排序比较器并使用 2 个 CompositeKeyWritable 变量进行分组。

    【讨论】:

      【解决方案3】:

      洗牌过程有3个过程:PartitioningSortinggrouping。 我猜你有多个reducer,并且你的相似结果由不同的reducer处理,因为它们位于不同的分区中。

      您可以将 reducer 的数量设置为 1 或设置自定义 Partitioner,该 Partitioner 为您的作业扩展 org.apache.hadoop.mapreduce.Partitioner

      【讨论】:

        猜你喜欢
        • 2014-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-01
        • 2015-05-28
        相关资源
        最近更新 更多