【问题标题】:Understanding LongWritable in Hive UDFs了解 Hive UDF 中的 LongWritable
【发布时间】:2020-08-25 19:08:34
【问题描述】:

我尝试用谷歌搜索它,但我不太了解文档。谁能解释一下这行代码的作用。

它是 Hive UDF 的一部分。我不完全理解 LongWritable 或 1L 的含义。

public class CustomUDF extends UDF {
    public LongWritable evaluate(Text schema) { // what is Text schema??
        if (schema == null) {
            return null;
        }
        try {
            return new LongWritable(1l); // what does this do??
        } catch (Exception ex) {
            // catch error
        }
    }
}

我是 Hive UDF 的新手,我无法理解这种方法。谢谢!!

【问题讨论】:

    标签: java hive schema user-defined-functions


    【解决方案1】:
    • Lo​​ngWritable 类

    Hadoop 需要能够通过 DataInput 和 DataOutputobjects(通常是 IO 流)将数据输入和输出 Java 类型。 Writable 类通过实现两个方法`write(DataOuput) 和 readFields(DataInput) 来做到这一点。具体来说,LongWritable 是一个包装了 java long 的 Writable 类。

    参考 - https://www.edureka.co/community/29194/understanding-longwritable#:~:text=Hadoop%20needs%20to%20be%20able,that%20wraps%20a%20java%20long

    对于其他相同类型的类 - https://blog.dataiku.com/2013/05/01/a-complete-guide-to-writing-hive-udf

    'evaluate' 方法是 udf 的入口点。因此,如果您在 Hive 中将 udf 称为 'select myudf('aa')',则输入 'aa' 将被传递给您的评估方法。 (我们也可以根据用例重载这个方法)

    现在来看看你的代码。首先,这段代码包含错误,因为如果它去捕获它不会返回任何东西。但是让我们假设如果输入不为空,它将返回一个新的 LongWritable(1L)。那么这段代码会

    • 如果将 null 传递给您的 udf,则返回 null。 Hive 命令 - 选择 myudf(null)
    • 如果没有向 udf 传递任何内容,则会给出错误,指出在此类中找不到匹配的方法,因为在这种情况下,它将寻找不带任何参数的评估方法。 Hive 命令 - 选择 myudf();
    • 如果您在 udf 中传递任何可以转换为文本的内容,那么它将返回 1(long)。 Hive 命令 - 选择 myudf('aa');

    另外,1和1L的区别在于1是int类型,1L是long类型。

    【讨论】:

      猜你喜欢
      • 2014-08-05
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多