Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing argument from shell script to hive script

Tags:

bash

hadoop

hive

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.

like image 891
knowone Avatar asked Dec 15 '22 14:12

knowone


2 Answers

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"
like image 183
sras Avatar answered Jan 09 '23 19:01

sras


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.

like image 29
Madhu Avatar answered Jan 09 '23 18:01

Madhu