我有一个自定义类型,其中包含 Hadoop native 类型的字段(例如 Text
和 IntWritable
),并且需要在随机/排序阶段将其用作键并根据需要进行排序。有类似的问题,如 this one和 this one ,但它们是关于使用 native 类型的。如何与自定义类型达到同样的效果,需要满足哪些要求?
最佳答案
实现这一结果存在细微差别,有些是显而易见的,有些则不是。我将尝试用几个简短的点来解释它们:
compareTo()
方法。 All Writable implementations must have a default constructor so that the MapReduce framework can instantiate them, then populate their fields by calling readFields().
compare()
方法。在此之后,您有两种方法可以使用此比较器而不是默认的方法:或 你设置这个类在 Job 的 setSortComparatorClass 的帮助下使用方法:job.setSortComparatorClass(YourComparator.class)
static {
WritableComparator.define(CustomType.class, new YourComparator());
}
The static block registers the raw comparator so that whenever MapReduce sees the class, it knows to use the raw comparator as its default comparator.
https://stackoverflow.com/questions/55133030/