java实现hadoop的writeablecomparable

ruoxqz4g  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(378)

我已经为map作业实现了writeablecomparable,并向它传递了三个值。

  1. public class KeyCustom implementsw WritableComparable<KeyCustom>
  2. {
  3. private Text placeOfBirth;
  4. private Text country;
  5. private LongWritable age;
  6. //Implemented constructors and set methods, write, readFields, hashCode and equals
  7. @Override
  8. public int compareTo(KeyCustom arg0)
  9. {
  10. return placeOfBirth.compareTo(arg0.placeOfBirth);
  11. }
  12. }

但是当我在我的reducer中记录这三个字段时,我可以清楚地看到,同一个国家的所有人都聚集在一起。如果有人能帮我,让我所有的减速机都能找到出生地相同的人,那就太好了。我不知道如何做到这一点,如果我的比较功能是错误的。
谢谢你的帮助。

bihw5rsg

bihw5rsg1#

你试图用错误的方法解决你的任务。您真正需要的是实现适当的分区器。
下面是编写自定义分区器的详细示例。
下面是mapreduce分区器的基本类api。
顺便说一下,你不需要特别的 compareTo() 实现做特殊的分区。
更新:
试着在你的工作中将partitioner改为totalorderpartitioner,也许你的问题就会得到解决。这是一个不错的例子,说明它应该是什么样子。

14ifxucb

14ifxucb2#

我想你有两个选择
1) 像上面讨论的那样,一个定制的党派成员?
或2) Overwride HashCode() 作为

  1. @Override public int hashCode() {
  2. return placeOfBirth.hashCode();
  3. }

原因
默认的partitioner类处理writablecomaparable的hashcode。因此,对于一个定制的writeablecomparable,您需要有一个hashcode()overidden,它使partioner能够将Map输出分段到reducer。或者您可以实现并将自己的partioner类分配给只考虑palceofbirthfield进行分区的作业。

相关问题