将参数从 shell 脚本传递到配置单元脚本

passing argument from shell script to hive script

我有一个问题可以分为两种方式: 我的要求是将参数从 shell 脚本传递到配置单元脚本。 要么 在一个 shell 脚本中,我应该在配置单元语句中包含变量的值。

我将通过示例对两者进行解释:

1) 将参数从 shell 脚本传递到 hiveQL->

My test Hive QL:
select count(*) from demodb.demo_table limit ${hiveconf:num}

我的测试shell脚本:

cnt=1
sh -c 'hive -hiveconf num=$cnt -f countTable.hql'

所以基本上我想在 HQL 中包含 'cnt' 的值,在这种情况下不会发生这种情况。我得到的错误是:

FAILED: ParseException line 2:0 mismatched input '<EOF>' expecting Number near 'limit' in limit clause

我确定这个错误意味着变量的值没有被传递。

2) 在 shell 脚本中直接传递参数->

cnt=1
hive -e 'select count(*) from demodb.demo_table limit $cnt'

在上述两种情况下,我都无法传递参数值。有什么想法吗??

PS:我知道在计数中包含 'limit' 的查询似乎很荒谬,但我已经改写了我实际遇到的问题。传递参数的要求保持不变。

任何想法,任何人?

提前致谢。

这样设置变量:

#!/bin/bash
cnt=3
echo "Executing the hive query - starts"
hive -hiveconf num=$cnt -e ' set num; select * from demodb.demo_table limit ${hiveconf:num}'
echo "Executing the hive query - ends"

试试这个
cnt=1

hive -hiveconf number=$cnt select * from demodb.demo_table limit ${hiveconf:number}

如果将其放入名为 hivetest.sh 的文件中,然后使用 sh hivetest.sh:

调用,则此方法有效
cnt=2
hive -e "select * from demodb.demo_table limit $cnt"

您使用的是单引号而不是双引号。 对选项 #1 使用双引号也可以正常工作。

hadoop@osboxes:~$ export val=2;

hadoop@osboxes:~$ hive -e "select * from bms.bms1 where max_seq=$val";

vi test.sh
#########
export val=2
hive -e "select * from bms.bms1 where max_seq=$val";
#####################