SQL queries in BASH script

Viewed 442

I am using Hadoop to execute my queries.

What I want is using BASH variables within my query. Here is an example :

export month="date +%m"
export year="date +%Y"

beeline -u jdbc:hive2://clustername.azurehdinsight.net:443/tab' 
-n myname -e "select * from mytable where month = '$month' and  
year = '$year';"

But the query is empty so that in reality, it's not the case within Hive.

select * from mytable where month = '$month' and  
year = '$year';

is not an empty query in Hive.

Is there a problem in my bash script ?

1 Answers

You need execute date command using $(), change

export month="date +%m"
export year="date +%Y"

with

export month=$(date +%m)
export year=$(date +%Y)

You can use hivevar arguments with beeline

beeline -u jdbc:hive2://clustername.azurehdinsight.net:443/tab \
-n myname \
--hivevar month=$month \
--hivevar year=$year \
-e "select * from mytable where month = '${hivevar:month}' and year = '${hivevar:year}';"
Related