Shell 如何在配置单元脚本中传递多个参数

Shell 如何在配置单元脚本中传递多个参数,shell,hive,hiveql,Shell,Hive,Hiveql,雇员: 我想通过运行配置单元脚本sample.hql获取2016年的记录 use octdb; select * from '${hiveconf:table}' where year = '${hiveconf:year}'; [cloudera@quickstart ~]$ hive -hiveconf table='employee', year=2016 -f sample.hql 但是我收到错误NoViableAltException(307@[])……您需要使用--hiveco

雇员:

我想通过运行配置单元脚本sample.hql获取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

通过这样做,R&D找到了正确的答案,${hiveconf:table}应该在不带“”的脚本中定义。 sample.hql:-

use ${hiveconf:database};

   select * from ${hiveconf:table} where year = ${hiveconf:year};
正在运行sample.hql

[cloudera@quickstart shell]$ hive -hiveconf database=octdb -hiveconf table=employee -hiveconf year=2016 -f sample.hql
使用文件:/etc/hive/conf.dist/hive-log4j.properties中的配置初始化日志记录 嗯


所用时间:4.423秒,获取:3行

传递变量也可以通过“hivevar”和“hiveconf”实现

区别如下:

已添加hiveconf命名空间,应使用(-hiveconf)设置配置单元配置值

添加了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
您应该在更新的配置单元版本中使用
--hivevar
。早些时候,开发人员可以使用
--hiveconf
设置配置,它还用于变量。但是,后来实现了
--hivevar
,以便为中提到的变量提供单独的名称空间

使用以下与直线

beeline--hivevar table=employee--hivevar year=2016-f sample.hql
这样,在配置单元脚本文件中,您可以直接或使用如下所示的hivevar名称空间访问此变量

从${table}中选择*;
从${hivevar:table}中选择*;

请注意,您可能需要使用
-u
选项指定URL字符串。

谢谢您的解决方案,但我仍然收到相同的错误。错误:NoViableAltException(307@[])失败:ParseException行2:14无法识别“employee,join source WARN中的“where”“year”:调用了方法类org.apache.commons.logging.impl.SLF4JLogFactory#release()。警告:请参阅以获取解释。您仍然在
员工
之后使用逗号;移除它。没有名为
employee,
的表。已删除,但仍然存在相同的错误:-(现在我可以传递多个参数,如hive-hiveconf year=2016-hiveconf id=1-f sample.hql,但如何在运行.hql脚本时传递表名
set hivevar:YEAR=2018;
SELECT * from table where year=${YEAR};

hive --hiveconf var='hello world' -e '!echo ${hiveconf:var};'
-- this will print: hello world