【问题标题】:How to use nested variable substitution in a hive query如何在配置单元查询中使用嵌套变量替换
【发布时间】:2021-06-06 12:22:37
【问题描述】:

我尝试在配置单元查询中直接使用嵌套变量。我正在使用 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 忽略了最后一个右大括号。 任何帮助将不胜感激!

我阅读了Language Manual Variable Substitution,但它只显示:

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

【问题讨论】:

    标签: variables hive hiveql dbeaver


    【解决方案1】:

    我认为 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}}
    

    【讨论】:

    • 嗨!谢谢你的回答。在我的脚本中,我正确使用了括号。我只是在我的问题中打错了代码。
    • 我尝试运行您的代码,但没有成功。我正在使用 Dbeaver 运行。这是一个截图:snipboard.io/EMU3q8.jpg 在第一个 SS 中,我尝试完全按照您发布的方式运行。第二个屏幕截图:snipboard.io/T1xksP.jpg 所以,我尝试以与之前相同的方式运行 (@set),它显示了一个弹出窗口 当我尝试运行一个变量而不先声明它时,通常会出现此弹出窗口。
    • @arnaldoleandro 是的,在 Dbeaver 中它的工作方式可能不同,我不确定 Dbeaver。在纯 Hive 中,这有效,您不需要 @set
    • 是的。在纯蜂巢中它确实有效。但不幸的是,我需要我的脚本在 Dbeaver 中运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 2020-09-19
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    相关资源
    最近更新 更多