gpt4 book ai didi

java - 如何在 Hadoop 中对自定义可写类型进行排序

转载 作者:行者123 更新时间:2023-12-02 20:27:30 28 4
gpt4 key购买 nike

我有一个自定义类型,其中包含 Hadoop native 类型的字段(例如 TextIntWritable ),并且需要在随机/排序阶段将其用作键并根据需要进行排序。有类似的问题,如 this onethis one ,但它们是关于使用 native 类型的。如何与自定义类型达到同样的效果,需要满足哪些要求?

最佳答案

实现这一结果存在细微差别,有些是显而易见的,有些则不是。我将尝试用几个简短的点来解释它们:

  • 首先自定义类型必须实现WritableComparable而不仅仅是Writable当然,定义 compareTo()方法。
  • 来自 Hadoop: The Definitive Guide 的非常重要的说明:

    All Writable implementations must have a default constructor so that the MapReduce framework can instantiate them, then populate their fields by calling readFields().



    也许最容易出错的部分是默认构造函数应该实例化该字段(如果它们未初始化),因为它们 不得为空。
  • 如果您对默认排序不满意,这一点是关于创建自定义比较器。在这种情况下,您需要创建一个扩展 WritableComparator 的新类。并覆盖其compare()方法。在此之后,您有两种方法可以使用此比较器而不是默认的方法:你设置这个类在 Job 的 setSortComparatorClass 的帮助下使用方法:
    job.setSortComparatorClass(YourComparator.class)

    在您的自定义类型的静态 block 中注册它:
    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.


  • Here是具有静态嵌套比较器的此类的示例。

    关于java - 如何在 Hadoop 中对自定义可写类型进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55133030/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com