【问题标题】:Hadoop Comparator method compare is not calling from the class of composite keyHadoop比较器方法比较不是从复合键类调用
【发布时间】:2015-07-23 03:52:30
【问题描述】:

我正在从 Hadoop:权威指南 学习 Hadoop。本书中有一个二级排序的例子:MaxTemperatureUsingSecondarySort

在这个例子中,我添加了 IntPair 类(与第 4 章的 Text Pair 类相同)。我已经在Eclipse中编写了所有代码,当我编写以下代码时:

IntPair.compare(ip1.getFirst(), ip2.getFirst());
return -IntPair.compare(ip1.getSecond(), ip2.getSecond());

然后IntPair.compare 导致错误,说比较方法不在IntPair 类中。

  1. 是否还需要在IntPair类中定义比较方法,如果需要,那么比较方法的主体类型是什么?

  2. 既然compare方法是通过WritableComparator接口实现的,那是不是就不用在IntPair类中重新定义body了?还是我缺少什么?

【问题讨论】:

  • 有人可以对此发表评论吗?

标签: java hadoop


【解决方案1】:

这就是我们必须在IntPair 类中定义比较方法的方式。

public int compare(IntPair tp) {
        int cmp = this.first.compareTo(tp.first);
        if (cmp != 0) {
            return cmp;
        }
        return this.second.compareTo(tp.second);
    }

以下是二级排序比较器中的比较方法:

public int compare(final WritableComparable firstValue,
            final WritableComparable secondValue) {
        final IntPair ip1 = (IntPair ) firstValue;
        final IntPair ip2 = (IntPair ) secondValue;

        final int cmp = ip1.getFirst().compareTo(ip2.getFirst());
        if (cmp != 0) {
            return cmp;
        }

        return -ip1.getSecond().compareTo(ip2.getSecond()); // reverse
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-17
    • 2018-06-30
    • 1970-01-01
    • 2014-02-01
    • 2020-02-13
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多