了解Hive UDF中的LongWritable

问题描述

我尝试使用Google搜索,但是对文档的了解不多。谁能解释这行代码的作用。

它是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的新手,但在理解此方法时遇到困难。谢谢!!

解决方法

  • LongWritable类别

Hadoop需要能够通过DataInput和DataOutputobjects(通常是IO Streams)对Java类型中的数据进行序列化。 Writable类通过实现两个方法`write(DataOuput)和readFields(DataInput)实现此目的。具体来说,LongWritable是一个Writable类,它包装了一个java long。

参考-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'将传递给您的评估方法。 (根据使用情况,我们也可以重载此方法)

现在进入您的代码。首先,此代码包含错误,因为如果要捕获它,将不会返回任何内容。但是让我们假设,如果输入不为null,它将返回一个新的LongWritable(1L)。然后这段代码将

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

此外,1和1L之间的区别在于1是int类型,而1L是long类型。