1

下面是我的 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';"`

问题陈述:-

如果你看到我hive -eecho $DATE_YEST_FORMAT1. 有时,由于某些原因,该查询会失败。所以目前发生的情况是,如果first Hive SQL query失败,那么它会second Hive SQL query在 sleep for 之后进入120 seconds。那是我不想要的。那么,如果由于任何原因导致会费失败,有什么办法first query,它应该stopped automatically在那个时候得到。并且它应该在几分钟后再次从头开始自动运行(应该是可配置的)

更新:-

正如所建议的那样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

状态代码是zero因为我的第一个查询成功,但之后我的程序退出并且它没有去执行我的第二个查询?为什么?我在这里肯定又错过了一些东西。

4

2 回答 2

2

您可能还会发现设置立即退出选项很有用:

     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"
于 2012-08-16T04:27:11.033 回答
1

除非我误解了情况,否则这很简单:

#!/bin/bash

DATE_YEST_FORMAT1=`perl -e 'use POSIX qw(strftime); print strftime "%Y-%m-%d",localtime(time()- 3600*504);'`
echo $DATE_YEST_FORMAT1

QUERY0="
        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;"

if hive -e "$QUERY0"
then
    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';"`
    # ...and whatever you do with $QUERY1...
fi

字符串$QUERY0是为了方便,而不是必要。关键是您可以使用if语句测试命令是否成功(返回状态 0)。该test命令(更好地称为[)只是一个命令,当满足测试条件时返回 0,不满足时返回 1(非零)。

因此,该if语句运行第一个hive查询;如果它通过(退出状态 0),那么(并且仅在那时)它会继续执行then子句中的操作。

我抵制住了重新格式化您的 SQL 的诱惑;可以说,这不是我在自己的代码中使用的布局。

于 2012-08-16T03:46:19.353 回答