Search code examples
bashhadoopimpala

Restarting Failed Scripted Queries in Impala Shell


I've been going over the Impala Shell documentation and haven't found anything addressing this issue. I've got a bash script that runs an Impala query and then sends an email containing the query results. The problem is, every once and a while the query will fail; when that happens, the next line of the script kicks in and a blank CSV file gets emailed out. Is there either a modification that can be made to the Impala Shell script itself, or to the framing bash script to address this? My initial thought was to have the script rerun the query if the CSV file is empty, but in cases where the query legitimately produces no results, this would get the script stuck in a loop.

Any suggestions?

Here's a model of what the script looks like now:

#!/bin/bash

NOW=$(date +"%F")
NAME="001"

impala-shell -i servername.com -B --output_delimiter=',' -o ../$NAME/$NAME.csv -f ../$NAME/001.sql

cat ../$NAME/$NAME/001.header > ../$NAME/$NAME-$NOW.csv
cat ../$NAME/$NAME.csv >> ../$NAME/$NAME-$NAME.csv

cat ../$NAME.email | mailx -a ../$NAME/$NAME-$NOW.csv -a ../$NAME.sql -s "Email subject" -r "Alias<[email protected]>" [email protected]

Solution

  • Since it's a bash script you can add logic that checks the exit code of your last executed command. So if your impala shell command runs fine then it will return a exit code of 0, if there is an error then it should return an exit code other than 0 (if they developed it correctly). I have read some posts about impala queries erroring out but still giving a code exit code so you may need to mess with it. The example below would loop until the impala statement ran successfully, so some sort of retry count logic may need to be added

    impala-shell -i servername.com -B --output_delimiter=',' -o ../$NAME/$NAME.csv -f ../$NAME/001.sql
    while [ $? -ne 0 ]
    do
    impala-shell -i servername.com -B --output_delimiter=',' -o ../$NAME/$NAME.csv -f ../$NAME/001.sql
    done
    

    Example with run count logic to exit after 2 attempts

    runCnt=0
    impala-shell -i servername.com -B --output_delimiter=',' -o ../$NAME/$NAME.csv -f ../$NAME/001.sql
    while [ $? -ne 0 ]
    do  
    runCnt=$(($runCnt + 1))
        if [ $runCnt -eq 2 ]
        then
            exit 1
        else
            impala-shell -i servername.com -B --output_delimiter=',' -o ../$NAME/$NAME.csv -f ../$NAME/001.sql
        fi
    done