无法查询依赖udtf的配置单元视图

7hiiyaii  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(317)

我创建了一个表,如下所示:

CREATE TABLE TEST (ID INT, SCORE INT, NAME STRING);

插入了几条记录。我想执行top-k查询,返回每个id的top记录,按分数排序。
我正在使用hivemall库中的each\u top \u k()自定义项,如下所述:https://hivemall.incubator.apache.org/userguide/misc/topk.html

SELECT EACH_TOP_K(1, ID, SCORE, ID, NAME) AS (RANK, SCORE, ID, NAME) FROM (
SELECT * FROM TEST
CLUSTER BY ID
) T;

它成功地返回每个id的最高分。但是,我随后创建了一个视图,如下所示:

CREATE VIEW TEST_VIEW AS SELECT EACH_TOP_K(1, ID, SCORE, ID, NAME) AS (RANK, SCORE, ID, NAME) FROM (
SELECT * FROM TEST
CLUSTER BY ID
) T;

它成功地执行了。然而,一个简单的

SELECT * FROM TEST_VIEW;

返回以下错误:
错误:编译语句时出错:失败:semanticexception视图test\视图对应于udtf,而不是selectoperator(州=42000,代码=40000)
我找不到任何提到这个错误的地方。有什么建议吗?

mutmk8jj

mutmk8jj1#

我认为hive在运行时为udtf推断每个字段的数据类型时会遇到问题。这应该可以解决这个问题,试着在你的查询上面放一个查询,比如

CREATE VIEW TEST_VIEW AS 
select cast(rank as long) as rank, cast(score as double) as score, cast(id as string) as id, cast(name as string) as name from (
SELECT EACH_TOP_K(1, ID, SCORE, ID, NAME) AS (RANK, SCORE, ID, NAME) FROM (
SELECT * FROM TEST
CLUSTER BY ID
) T ) t2;

相关问题