bash 将参数从 shell 脚本传递到 hive 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30592007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:59:49  来源:igfitidea点击:

passing argument from shell script to hive script

bashhadoophive

提问by knowone

I've a concern which can be categorized in 2 ways: My requirement is of passing argument from shell script to hive script. OR within one shell script I should include variable's value in hive statement.

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

I'll explain with an example for both:

我将用一个例子来解释两者:

1) Passing argument from shell script to hiveQL->

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

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

My test shell script:

我的测试 shell 脚本:

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

So basically I want to include the value of 'cnt' in the HQL, which is not happening in this case. I get the error as:

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

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

I'm sure the error means that the variable's value isn't getting passed on.

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

2) Passing argument directly within the shell script->

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

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

In both the above cases, I couldn't pass the argument value. Any ideas??

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

PS: I know the query seems absurd of including the 'limit' in count but I have rephrased the problem I actually have. The requirement remains intact of passing the argument.

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

Any ideas, anyone?

任何想法,任何人?

Thanks in advance.

提前致谢。

回答by sras

Set the variable this way:

以这种方式设置变量:

#!/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"

回答by Madhu

This works, if put in a file named hivetest.sh, then invoked with sh hivetest.sh:

这是有效的,如果放在一个名为 的文件中hivetest.sh,然后调用sh hivetest.sh

cnt=2
hive -e "select * from demodb.demo_table limit $cnt"

You are using single quotes instead of double. Using double quotes for OPTION #1 also works fine.

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

回答by y durga prasad

hadoop@osboxes:~$ export val=2;

hadoop@osboxes:~$ export val=2;

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

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

or

或者

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

回答by Aman

Try this
cnt=1

试试这个
cnt=1

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