Abort the shell script if any hive sql query gets failed


 
Thread Tools Search this Thread
Operating Systems Solaris Abort the shell script if any hive sql query gets failed
# 1  
Old 08-15-2012
Abort the shell script if any hive sql query gets failed

Below is my shell script from which I am trying to invoke few Hive SQL queries and the below shell script works fine.

Problem Statement:-

If you see my first `hive -e` block in the below shell script which contains a very long Hive SQL query. Sometimes that Hive SQL query gets failed due to certain reasons and when it gets failed it moved to second hive sql query after sleeping for 120 seconds which I don't want. Is there any way if my first `hive SQL query` gets failed due to any reasons, it should get stopped automatically at that point without executing any other hive sql query after the first one. I should be notified in an email of this error. I am running SunOS.

Code:
bash-3.00$ uname -a
SunOS lvsaishdc3in0001 5.10 Generic_142901-02 i86pc i386 i86pc

Below is my shell script.


Code:
#!/bin/bash

HADOOP_HOME=/home/hadoop/latest
export HADOOP_HOME
JAVA_HOME=/usr/jdk/latest
export JAVA_HOME
HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology"
export HIVE_OPTS
DATE_YEST_FORMAT1=`perl -e 'use POSIX qw(strftime); print strftime "%Y-%m-%d",localtime(time()- 3600*504);'`
echo $DATE_YEST_FORMAT1
DATE_YEST_FORMAT2=`perl -e 'use POSIX qw(strftime); print strftime "%Y%m%d",localtime(time()- 3600*504);'`
echo $DATE_YEST_FORMAT2

hive -e "
set mapred.job.queue.name=hdmi-technology;
set mapred.output.compress=true;
set mapred.output.compression.type=BLOCK;
set mapred.output.compression.codec=org.apache.hadoop.io.compress.LzoCodec;
add jar UserDefinedFunction.jar;
create temporary function rank as 'com.ebay.hive.udf.Rank';

insert overwrite table lip_data_quality partition (dt='$DATE_YEST_FORMAT2')
SELECT table2.buyer_id
	,table2.count2 as count2
	,coalesce(table1.error, cast(0 AS BIGINT)) AS count1
FROM (
	SELECT buyer_id
		,count(1) AS count2
	FROM (
		SELECT t1 [0] AS buyer_id
			,t1 [1] AS item_id
			,created_time
		FROM (
			SELECT split(ckey, '\\\\|') AS t1
				,created_time
			FROM (
				SELECT CONCAT (
						buyer_id
						,'|'
						,item_id
						) AS ckey
					,created_time
				FROM dw_checkout_trans
				WHERE to_date(from_unixtime(cast(UNIX_TIMESTAMP(created_time) AS BIGINT))) = '$DATE_YEST_FORMAT1' distribute BY ckey sort BY ckey
					,created_time DESC
				) a
			WHERE rank(ckey) < 1
			) X
		ORDER BY buyer_id
			,created_time DESC
		) a
	WHERE rank(buyer_id) < 10
	GROUP BY buyer_id
	) table2
LEFT OUTER JOIN (
	SELECT TT1.BUYER_ID
		,SUM(CASE
				WHEN ABS(cast((TT2.prod_and_ts_split / 1000) - UNIX_TIMESTAMP(TT1.created_time) AS DOUBLE)) <= 900
					THEN 0
				ELSE 1
				END) AS ERROR
	FROM (
		SELECT buyer_id
			,item_id
			,rank(buyer_id)
			,created_time
		FROM (
			SELECT t1 [0] AS buyer_id
				,t1 [1] AS item_id
				,created_time
			FROM (
				SELECT split(ckey, '\\\\|') AS t1
					,created_time
				FROM (
					SELECT CONCAT (
							buyer_id
							,'|'
							,item_id
							) AS ckey
						,created_time
					FROM dw_checkout_trans
					WHERE to_date(from_unixtime(cast(UNIX_TIMESTAMP(created_time) AS BIGINT))) = '$DATE_YEST_FORMAT1' distribute BY ckey sort BY ckey
						,created_time DESC
					) a
				WHERE rank(ckey) < 1
				) X
			ORDER BY buyer_id
				,created_time DESC
			) a
		WHERE rank(buyer_id) < 10
		) TT1
	LEFT OUTER JOIN (
		SELECT t1 [0] AS uid
			,t1 [1] AS product_id
			,prod_and_ts_split
		FROM (
			SELECT split(ckey, '\\\\|') AS t1
				,prod_and_ts_split
			FROM (
				SELECT CONCAT (
						uid
						,'|'
						,product_id
						) AS ckey
					,prod_and_ts_split
				FROM (
					SELECT uid
						,product_id
						,prod_and_ts_split
					FROM (
						SELECT *
						FROM (
							SELECT uid
								,prod_and_ts.product_id AS product_id
								,prod_and_ts.timestamps AS timestamps
							FROM pds_attribute_data_realtime LATERAL VIEW explode(last_items_purchased) exploded_table AS prod_and_ts
							) prod_and_ts
						) tt2 LATERAL VIEW explode(split(timestamps, '#')) exploded_table2 AS prod_and_ts_split
					WHERE to_date(from_unixtime(cast(prod_and_ts_split / 1000 AS BIGINT))) = '$DATE_YEST_FORMAT1'
					GROUP BY uid
						,product_id
						,prod_and_ts_split
					ORDER BY uid
						,prod_and_ts_split DESC
					) X
						 distribute BY ckey sort BY ckey
					,prod_and_ts_split DESC
				) a
			WHERE rank(ckey) < 1
			) YY
		ORDER BY uid
			,prod_and_ts_split DESC
		) TT2 ON (
			TT1.item_id = TT2.product_id
			AND TT1.BUYER_ID = TT2.uid
			)
	GROUP BY TT1.BUYER_ID
	) table1 ON table1.buyer_id = table2.buyer_id
ORDER BY table2.buyer_id;"

sleep 120

QUERY1=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT SUM(total_items_purchased), SUM(total_items_missingormismatch) from lip_data_quality where dt='$DATE_YEST_FORMAT2';"`

QUERY2=`hive -e "
set mapred.job.queue.name=hdmi-technology;
SELECT 100 * SUM(total_items_missingormismatch*1.0) / SUM(total_items_purchased) FROM lip_data_quality where dt='$DATE_YEST_FORMAT2';"`


rm /tmp/chart.html

TEMP=`mktemp -t chart.html`
NOERROR=100-$QUERY2
ERROR=$QUERY2
cat > $TEMP <<EOF
<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Title');
        data.addColumn('number', 'Value');
        data.addRows([
          ['No Error Percentage', $NOERROR],
		     ['Error Percentage', $ERROR]
        ]);

        // Set chart options
        var options = {'title':'LIP Data Quality Report',
                       'width':700,
                       'height':600};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div" style="width:900px; height: 800px;"></div>
  </body>
</html>
EOF


mailx -s "LIP Data Quality Report for $DATE_YEST_FORMAT1" -r email_person@sample_email.com <<EOF
Data Successfully loaded into LIP_DATA_QUALITY table


Total Items Purchased: `echo $QUERY1 | awk '{print $1}'`

Total Items MissingorMismatch: `echo $QUERY1 | awk '{print $2}'`

Error Percentage: $QUERY2

********* Before Making Assumptions, please read the below Notes***********************

Above Percentage could be mainly because of below reasons:-
1) Multiple USER's sharing the same browser and export is done basis on LAST_LOGGED_IN_USER.
2) Either the Export Issue.
3) Thirdly, ITEM was not captured properly in PDS.

`uuencode /tmp/chart.html percentage_graph.html`

EOF

# 2  
Old 08-15-2012
I don't use "Hive", But can you check the return code after the hive command and if it is not 0, exit?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to embed sql query into our shell script?

Hi I would like to embed a sql query in my shell script. Also, before any the sql query is executed, i would like to validate username and password. (1 Reply)
Discussion started by: arghadeep adity
1 Replies

2. Shell Programming and Scripting

Convert the SQL Query in Shell Script

Hi All, I have a query with output below select 'create synonym "'||TABLE_NAME||'" for '||Table_owner||'."'||table_name||'"'||chr(59) from user_synonyms; ================== create synonym "RV_SBC_SIG" for WFCONTROLLER_TE."RV_SBC_SIG"; create synonym "AQM_TASK" for AWQM_TE."AQM_TASK";... (2 Replies)
Discussion started by: pvmanikandan
2 Replies

3. Red Hat

Sql query through shell script

hey , i am using this code to store value of a sql query and and then use it in other query but after some time , but it is not working. please help #!/bin/bash val_1=$( sqlplus -s rte/rted2@rel76d2 << EOF setting heading off select max(stat_id) from cvt_stats; exit EOF ) nohup... (5 Replies)
Discussion started by: ramsavi
5 Replies

4. UNIX for Dummies Questions & Answers

Regarding executing sql query in shell script

Hi, I have one SQL file prepared in UNIX and one script that is executing that. In SQL i have Update and create queries. I want to introduce conditions in SQL file (in UNIX) that if either of the create or update query failes whole transaction should be rollback. I just have 1 create... (2 Replies)
Discussion started by: abhii
2 Replies

5. Shell Programming and Scripting

$ symbol in sql query in shell script

Hi Team, Can you please help me to resolve this issue. Am unable to use this $ symbol in sql query in the shell script. For Example: # !/bin/sh export USER_NAME=XXX export PASSWORD=YYY export ORACLE_SID=xamdb echo $ORACLE_SID echo " Session Details ..." ... (1 Reply)
Discussion started by: indira_s
1 Replies

6. Shell Programming and Scripting

query sql using shell script

query sql using shell script, is it possible? my friend told me to do a file.sql and link to my shell script, but can i query sql using shell script? thanks in advance! (2 Replies)
Discussion started by: kingpeejay
2 Replies

7. Shell Programming and Scripting

executing a SQL query in shell script

Hi ALL, I need an help in connecting to oracle database, executing a select query and printing it on the screen. Can any one please write a simple code or psuedo code and let me know. select query returns multiple values( say select name from emp) Thanks in advance LM (1 Reply)
Discussion started by: lijju.mathew
1 Replies

8. Shell Programming and Scripting

Executing Sql Query Using Shell Script

HI ALL i have a requirement like this. i have to write a shell script to run a sql query. DB is oracle. once the query is run, the results of the query has to be published in a data file. can you please advice me how to go about it. i am absolutely new to shell scripts and this is a part of my job. (14 Replies)
Discussion started by: ragha81
14 Replies

9. UNIX for Advanced & Expert Users

Connecting DB in the Shell Script to do SQL Query

Any link or example to write shell script for the Connecting Oracle for Quering through SQL thanks in advance ... Cheers !! Mehul Doshi (3 Replies)
Discussion started by: mehuldoshi
3 Replies

10. UNIX for Dummies Questions & Answers

Executing a SQL query from a shell script

I cannot figure out how to run a SQL script, or just a sqlplus query, from a shell script (bash or ksh). Basically, I need to su - oracle from root and run a query, then test the exit status. (3 Replies)
Discussion started by: 98_1LE
3 Replies
Login or Register to Ask a Question