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



我尝试在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}}

最新更新