【问题标题】:What is the hadoop sort comparator class for?hadoop 排序比较器类有什么用?
【发布时间】:2015-06-28 11:50:09
【问题描述】:

我已经实现了 hadoop 排序比较器类来对我的键进行排序。我知道它用来比较每个键。但是,我不知道它如何详细工作?如果用来比较的话,是这样吗?

谢谢大家....

【问题讨论】:

    标签: sorting hadoop mapreduce compare comparator


    【解决方案1】:

    说,你的密钥是(Attribute1, Attribute2)。现在您可以使用排序比较器,先按 Attribute1 排序,然后按 Attribute2。

    例如,

    Key = (2008,32) // year, temperature
    

    现在,如果您想按年份然后按温度排序,您可以使用排序比较器,如下所示:

    public static class KeyComparator extends WritableComparator {
        protected KeyComparator() {
            super(CompositeKey.class, true);
        }
    
        @Override
        public int compare(WritableComparable w1, WritableComparable w2) {
            CompositeKey ip1 = (CompositeKey) w1;
            CompositeKey ip2 = (CompositeKey) w2;
            int result = CompositeKey.compare(ip1.getYear(), ip2.getYear());
            if (result != 0) {
                return result;
            }
            return CompositeKey.compare(ip1.getTemperature(), ip2.getTemperature());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-22
      • 2013-08-21
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多