繁体   English   中英

如何在 hive 查询中使用嵌套变量替换

[英]How to use nested variable substitution in a hive query

我尝试在 hive 查询中直接使用嵌套变量。 我正在使用 Hive 和 Dbeaver。

我的目标是遍历条件列表并将结果保存在编号表中(condition_1 | tbl_1、condition_2、tbl_2 等)这是一个示例:

@set condition_1 = col1 in (1,10,11) and col2 in (1000,10000)
@set condition_2 = col1 in (2,20,22) and col2 in (2000,20000)
@Set ctrl= 1

create table tbl_${ctrl}
select ${ctrl} as id, * from my_table where ${condition_${ctrl}}

当我运行 select 语句时,它在where语句中失败并且仅解析ctrl变量,并且我收到以下错误消息:

SQL Error [40000] [42000]: Error while compiling statement: FAILED: ParseException line 22:6 cannot recognize input near '$' '{' 'condition_1' in expression specification

我认为 hive 忽略了最后一个结束大括号。 任何帮助,将不胜感激!

我阅读了语言手册变量替换,但它只显示了这一点:

set a=1;
set b=a;
set c=${hiveconf:${hiveconf:b}};
set c;
--uses nested variables.

我认为 hive 忽略了最后一个结束大括号。

因为美元符号需要放在大括号之前。

像在手册中那样做,它会起作用。 指定hiveconf命名空间,不要忘记花括号前的美元符号: ${hiveconf:condition_${hiveconf:ctrl}}

这个例子工作正常:

set condition_1 = col1 in (1,10,11) and col2 in (1000,10000);
set condition_2 = col1 in (2,20,22) and col2 in (2000,20000);
Set ctrl= 1;

with Table1 as( 
SELECT stack (2,
1, 1000,
2, 2000
) AS  (col1, col2)
)

select ${hiveconf:ctrl} as id, t.* from Table1 t where ${hiveconf:condition_${hiveconf:ctrl}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM