下面是我的 shell 脚本,我试图从中调用一些工作正常的 hive SQL 查询。
#!/bin/bash
DATE_YEST_FORMAT1=`perl -e 'use POSIX qw(strftime); print strftime "%Y-%m-%d",localtime(time()- 3600*504);'`
echo $DATE_YEST_FORMAT1
hive -e "
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;"
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';"`
问题陈述:-
如果您在 echo $DATE_YEST_FORMAT1
之后看到我的第一个 hive -e
block 。有时该查询会由于某些原因而失败。所以目前发生的情况是,如果 第一个 Hive SQL 查询
失败,那么它将在休眠 120 秒
后转到第二个 Hive SQL 查询
.而这正是我不想要的。因此,如果 first query
由于任何原因而失败,有没有什么办法,它应该在那个时候自动停止
。并且它应该在几分钟后再次从头开始自动运行(应该是可配置的)
更新:-
根据 Stephen
的建议。
我试过这样的东西-
#!/bin/bash
hive -e " blaah blaah;"
RET_VAL=$?
echo $RET_VAL
if [ $RET_VAL -ne 0]; then
echo "HiveQL failed due to certain reason" | mailx -s "LIP Query Failed" -r rj@host.com rj@host.com
exit(1)
我在下面收到类似这样的错误消息,而且我也没有收到任何电子邮件。我的语法和方法有什么问题吗?
syntax error at line 152: `exit' unexpected
注意:-
如果 Hive 查询执行成功,则此处为零。
放置空格后的另一个更新:- 进行如下更改后
#!/bin/bash
hive -e " blaah blaah;"
RET_VAL=$?
echo $RET_VAL
if [ $RET_VAL -ne 0 ]; then
echo "HiveQL failed due to certain reason for LIP" | mailx -s "LIP Query Failed" -r rj@host.com rj@host.com
fi
exit
hive -e 'Another SQL Query;'
我得到了类似下面的东西-
RET_VAL=0
+ echo 0
0
+ [ 0 -ne 0 ]
+ exit
状态代码为零
,因为我的第一个查询成功但我的程序在那之后退出并且没有去执行我的第二个查询?为什么?我肯定又在这里遗漏了一些东西。
最佳答案
您可能还会发现设置立即退出选项很有用:
set -e Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or || list, or if the command's return value is being inverted via !. A trap on ERR, if set, is executed before the shell exits.
在这个例子中
#!/bin/bash
set -e
false
echo "Never reached"
关于linux - 如果任何查询失败则停止 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11978427/