在配置单元中安全地设置STRING变量?

时间:2018-04-09 22:58:43

标签: bash hive environment-variables hiveql

问题

我可以在运行时在hive中设置变量:

$ cat my_query.sql
select '${hiveconf:my_string_variable}'
$ hive -f my_query.sql -hiveconf my_string_variable=foobar

(返回)

foobar

但是,如果我运行查询并忘记设置变量(无-hiveconf参数),则hive会将呼叫签名视为字符串:

$ hive -f my_query.sql

(返回)

${hiveconf:my_string_variable}

期望结果

我希望它提供错误(用于尝试引用不存在的变量)。

我知道这不会发生,因为我的“变量”只是被视为一个字符串。所以,我正在寻找一些在设置变量时传递字符串的方法,但是当它不存在时会输出错误。

我试过了:

在使用前设置检查变量

$ cat my_query2.sql
set my_variable; --hoping this errors out when hive realizes it's undefined
select '${hiveconf:my_string_variable'
$ 
$ hive -f my_query2.sql

(退货)

my_variable is undefined
OK
${hiveconf:my_string_variable

使用其他数据类型

如果字符串可以解释为Int文字,我可以使这个“工作”:

$ cat my_query3.sql
select cast(${hiveconf:my_string_variable} as string)
$ 
$ hive -f my_query3.sql --hiveconf my_string_variable=1234

(返回)

1234

但是当我在其中放入一个真正的字符串时它就会失败:

$ hive -f my_query3.sql --hiveconf my_string_variable=foobar
FAILED: SemanticException [Error 10004]: Line 1:12 Invalid table alias or column reference 'foobar': (possible column names are: )

0 个答案:

没有答案