4

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.

I'll explain with an example for both:

1) Passing argument from shell script to hiveQL->

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

My test shell script:

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:

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->

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.

Any ideas, anyone?

Thanks in advance.

2
  • can you do echo in second case and see what you are getting: echo 'select count(*) from demodb.demo_table limit $cnt' Commented Jun 2, 2015 at 9:25
  • Using single-quotes suppresses expansion. Commented Jun 3, 2015 at 0:22

4 Answers 4

9

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"
Sign up to request clarification or add additional context in comments.

3 Comments

num="$cnt", if you want to make the answer more generic (able to pass things that aren't just numbers and may contain whitespace, glob characters, etc).
Thanks sras, this is perfect. I never used multiple options for hive from command line. Totally agree with Charles; single quotes suppresses information.
to be more precise, we can use hivevar name space rather than hiveconf name space. However both will work for your query.
2

This works, if put in a file named hivetest.sh, then invoked with 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.

Comments

1

hadoop@osboxes:~$ export val=2;

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";
#####################

Comments

0

Try this
cnt=1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.