Bharath
Bharath

Reputation: 66

Return status of a hive script

I have two questions regarding the capture of the return status/exit status of hive script.

  1. Capture the return status in a unix script

try2.hql

select from_unixtime(unix_timestamp(),'YYYY-MM-DD')

This is called in the shell script try1.sh

echo "Start of script"
hive -f try2.hql
echo "End of script"

Now, I need to capture the return status of try2.hql. How can I do this ?

  1. Control flow when multiple queries are available

There are a couple of hive queries in a script try3.hql

select stockname, stock_date from mystocks_stg;

select concat('Top10_Stocks_High_OP_',sdate,'_',srnk) as rowkey, sname, sdate, sprice, srnk from (
select stockname as sname, stock_date as sdate, stock_price_open as sprice,rank() over(order by stock_price_open desc) as srnk
from mystocks
where from_unixtime(unix_timestamp(stock_date,'yyyy-mm-dd'),'yyyymmdd') = '${hiveconf:batch_date}') tab
where tab.srnk <= 10;

try3.hql is called in the script try4.sh be passing the relevant parameters.

My question : In try3.hql, if there is any error in the first query, I must return to the shell script and abort the program, without executing the second script.

Please suggest.

Upvotes: 0

Views: 3340

Answers (3)

Sushant Dhingra
Sushant Dhingra

Reputation: 142

echo "Start of script" hive -f try2.hql hive_status=$? echo "End of script" echo $hive_status>>$HOME/exit_status.log

In the home directory, you'll find the exit_status.log file created, in which you'll have the exit status of the script.

Upvotes: 0

Marco Polo
Marco Polo

Reputation: 738

And I have a solution for part 2.

You do know that "hive" CLI is deprecated in favor of "beeline" according to the documentation ?

HiveServer2 (introduced in Hive 0.11) has its own CLI called Beeline, which is a JDBC client based on SQLLine. Due to new development being focused on HiveServer2, Hive CLI will soon be deprecated in favor of Beeline (HIVE-10511).

In beeline, by default, your script will stop as soon as there is an error in it. This is controlled by the "force" parameter.

--force=[true/false]            continue running script even after errors

BTW, the solution provided by codeforester for part 1 still works with beeline.

Upvotes: 0

codeforester
codeforester

Reputation: 42999

For part 1 of your problem, you can change your script to exit the status of hive:

echo "Start of script"
hive -f try2.hql; hive_status=$?
echo "End of script"
exit $hive_status

Upvotes: 0

Related Questions