employee:表数据
我想通过运行 hive 脚本 sample.hql 来获取 year=2016 的记录。
use octdb;
select * from '${hiveconf:table}' where year = '${hiveconf:year}';
[cloudera@quickstart ~]$ hive -hiveconf table='employee', year=2016 -f sample.hql
但是我收到错误NoViableAltException(307@[])......。
使用 --hiveconf
选项两次:
hive --hiveconf table=employee --hiveconf year=2016 -f sample.hql
你应该在较新的 Hive 版本中改用--hivevar
。早些时候,开发人员能够使用 --hiveconf
设置配置,它也用于变量。但是,后来实现了--hivevar
,如 HIVE-2020 中所述,为变量提供单独的命名空间。
直线使用以下
beeline --hivevar table=employee --hivevar year=2016 -f sample.hql
有了这个,在 Hive 脚本文件中,您可以直接访问此变量或使用 hivevar 命名空间,如下所示。
select * from ${table};
select * from ${hivevar:table};
请注意,您可能需要使用-u <db_URL>
选项指定 URL 字符串。
通过R&D找到正确的答案,${hiveconf:table}应该在脚本中定义,而不是''。sample.hql:-
use ${hiveconf:database};
select * from ${hiveconf:table} where year = ${hiveconf:year};
运行示例.hql
[cloudera@quickstart shell]$ hive -hiveconf database=octdb -hiveconf table=employee -hiveconf year=2016 -f sample.hql
使用 file:/etc/hive/conf.dist/hive-log4j.properties 中的配置初始化的日志记录还行
Time taken: 1.484 seconds
OK
1 A 2016
2 B 2016
4 D 2016
所用时间:4.423 秒,获取时间:3 行
传递变量也可以通过"hivevar"和"hiveconf"来实现。
这是区别:
添加了 hiveconf 命名空间,应使用 (--hiveconf) 来设置 Hive 配置值。
添加了 hivevar 命名空间,并且应该使用 (--hivevar) 来定义用户变量。
使用hiveconf 也可以使用,但不建议用于变量替换,因为 hivevar 是为此目的显式创建的。
set hivevar:YEAR=2018;
SELECT * from table where year=${YEAR};
hive --hiveconf var='hello world' -e '!echo ${hiveconf:var};'
-- this will print: hello world