将参数传递给HIVE查询

时间:2018-10-25 12:55:46

标签: hive hiveql

我正在使用--hiveconf参数将参数传递给HIVE脚本,例如,将一个值传递给HIVE查询。还有其他方法可以将参数传递给HIVE脚本吗?

beeline -u "${dbconection}" --hiveconf load_id=${loadid} -f /etc/sql/hive_script.sql

hive_script.sql正在从表a中选择记录并插入表b中。

INSERT into TABLE table-b
SELECT column1,
Column2,
Column3,
${hiveconf:loadid} as load_id,
Column5
From table-a;

我收到以下错误消息

Error: Failed to open new session: org.apache.hive.service.cli.HiveSQLException: java.lang.IllegalArgumentException: Cannot modify load_id at runtime. It is not in list of params that are allowed to be modified at runtime

这是在我的环境中用于配置单元变量替换的设置。

 set hive.variable.substitute;
+--------------------------------+--+
|              set               |
+--------------------------------+--+
| hive.variable.substitute=true  |
+--------------------------------+--+

3 个答案:

答案 0 :(得分:1)

如果您使用beeline,则需要使用--hivevar

beeline -u "${dbconection}" --hivevar load_id=${loadid} -f /etc/sql/hive_script.sql

.sql或.hql扩展名没有任何区别。

配置单元查询将通过以下方式使用变量:

INSERT into TABLE table-b
SELECT column1,
Column2,
Column3,
${loadid} as load_id,
Column5
From table-a;

答案 1 :(得分:0)

首先,您的配置单元脚本应如下所示:

hive_script.hql

INSERT into TABLE '${hiveconf:table_b}'
SELECT column1,
Column2,
Column3,
${hiveconf:loadid} as load_id,
Column5
From '${hiveconf:table_a}';

然后按如下所示进行调用: hive --hiveconf table_a=employee --hiveconf table_b=department -f hive_script.hql

答案 2 :(得分:0)

这是我使用的内容,它对我有用,而不是对“ --hiveconf”使用“ --hivevar”,这将适用于HIVE v0.8.X及更高版本。

loaded='201810251040'
beeline -u "${dbconection}" --hivevar load_id=${loadid} -f /etc/sql/hive_script.sql

如下更新hive_script.sql

INSERT into TABLE table-b
SELECT column1,
Column2,
Column3,
${hivevar:load_id} as load_id,
Column5
From table-a;

这是伪代码....

相关问题