autoincrementudf在hive中工作,但在impala中返回null

ars1skjm  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(368)

我创建了一个java函数来创建自动增量值,我还基于这个函数创建了一个hiveudf,它在hive中非常有效。我基于这个函数创建了一个impala自定义项,它返回'null'而不是自动递增整数。
以下是java udf代码:

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.udf.UDFType;

@UDFType(stateful = true)
public class AutoIncrementUDF extends UDF {

    int ctr;

    public int evaluate() {
        ctr++;
        return ctr;
    }
}

正在创建配置单元自定义项:

create function autoincr as 'AutoIncrementUDF';

创建impala自定义项:

create function autoincr() returns int LOCATION '/user/acombs/AutoIncrementUDF.jar' symbol='AutoIncrementUDF';

在Hive和 Impala 中使用:

select  autoincr() as testkey, * from mapfund

非常感谢您的帮助!谢谢你,安娜

tct7dpnv

tct7dpnv1#

不幸的是 Impala 不支持 @UDFType 有状态的hive-udf的注解或概念。我们将在我们的文档中添加一个注解来说明这个限制。
但是,如果要返回行号,可以使用分析窗口函数,如 ROW_NUMBER() .
例如,

> select ROW_NUMBER() over (order by int_col) as testkey, int_col, float_col from alltypestiny;
+---------+---------+-------------------+
| testkey | int_col | float_col         |
+---------+---------+-------------------+
| 1       | 0       | 0                 |
| 2       | 0       | 0                 |
| 3       | 0       | 0                 |
| 4       | 0       | 0                 |
| 5       | 1       | 1.100000023841858 |
| 6       | 1       | 1.100000023841858 |
| 7       | 1       | 1.100000023841858 |
| 8       | 1       | 1.100000023841858 |
+---------+---------+-------------------+
Fetched 8 row(s) in 0.12s

有关详细信息,请参见impala分析函数。

相关问题