二次排序示例程序,IntPair的定义中compareTo声明异常,为啥不能触发?

hadoop-2.6.2-src/hadoop-mapreduce-project/hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples/SecondarySort.java, 二次排序示例程序,IntPair的定义中compareTo声明异常,为啥不能触发?

 
1460377964663.png

fish - Hadooper

赞同来自: Hagrid

因为在代码中,用如下实现对IntPair使用的comparator进行了重新设置(最后那个WritableComparator.define(IntPair.class, new Comparator()) 起的效果):
    /** A Comparator that compares serialized IntPair. */
    public static class Comparator extends WritableComparator {
      public Comparator() {
        super(IntPair.class);
      }

      public int compare(byte b1, int s1, int l1,
                         byte b2, int s2, int l2) {
        return compareBytes(b1, s1, l1, b2, s2, l2);
      }
    }

    static {                                        // register this comparator
      WritableComparator.define(IntPair.class, new Comparator());
    }

fish - Hadooper

赞同来自: Hagrid

WritableComparator.define(IntPair.class, new Comparator())能够生效,是因为这里定义了类与comparator的对应关系。 在使用的时候,实现逻辑如下:
    public static WritableComparator get(
      Class<? extends WritableComparable> c, Configuration conf) {
    WritableComparator comparator = comparators.get(c);
    if (comparator == null) {
      // force the static initializers to run
      forceInit(c);
      // look to see if it is defined now
      comparator = comparators.get(c);
      // if not, use the generic one
      if (comparator == null) {
        comparator = new WritableComparator(c, conf, true);
      }
    }
    // Newly passed Configuration objects should be used.
    ReflectionUtils.setConf(comparator, conf);
    return comparator;
  }
可见,当定义了映射的时候,直接 WritableComparator comparator = comparators.get(c);就可以获取comparator。 如果没有用define进行声明,就会调用comparator = new WritableComparator(c, conf, true),writablecomparable自身会变成用来做对比的对象,其compareTo方法在对比中会被调用到。

Hagrid

赞同来自:

是因为这里FirstPartitioner已经取代了compareTo的作用吧。哈哈哈。

要回复问题请先登录注册