与shell传参一样,当参数值里面有空格时,需要用双引号括起来,不然的话,会被当作两个参数截断。
问题描述:
Hive的脚本中需要给变量赋值,最常见的就是日期+时间
select a,b,c
from table
where bizdate=${hiveconf:bizdate}
执行的命令如果是hive -hiveconf bizdate="2020-05-15 15:00:00" -f xxx.hql
这样传入进去的参数值是2020-05-15
而不是想象中的 2020-05-15 15:00:00
解决这个问题,换个思路:既然在给shell的脚本传入参数时,可以利用双引号解决value中存在空格的情况,那么我们这里也用这个套路来尝试下。
正确的传参姿势:
hive -hiveconf "bizdate='2020-05-15 15:00:00'" -f xxx.hql
下面就来个实际的操作
test.hql:
select "${hiveconf:bizhourtime}"
调用命令:
hive -hiveconf "bizhourtime=`date -d '1 day ago' '+%Y-%m-%d %H:00:00'`" -f test.hql