Skip to content

Instantly share code, notes, and snippets.

@archongum
Last active August 24, 2019 02:08
Show Gist options
  • Save archongum/764b1a2d746fc041440a45a463048e17 to your computer and use it in GitHub Desktop.
Save archongum/764b1a2d746fc041440a45a463048e17 to your computer and use it in GitHub Desktop.
presto-cli to support custom parameters
#!/bin/bash
SCRIPT_NAME=`basename "$0"`
function _usage ()
{
cat <<EOF
Usage: $SCRIPT_NAME [OPTIONS]
Options:
-s --server: presto server
--catalog: presto catalog
--schema: presto schema
--user: presto user
--sql-file: sql file path
--extract-start-time: [OPTIONAL] extract start time (format: "2019-08-21T00:00:00Z" or "yesterday")
--extract-end-time: [OPTIONAL] extract end time (format: "2019-08-21T00:00:00Z" or "today")
--args: [OPTIONAL] additional custom parameters (format: "key1=value1;key2=value2")
EOF
}
TEMP=`getopt \
-o s: \
--long server:,catalog:,schema:,user:,sql-file:,extract-start-time:,extract-end-time:,args: \
-n $SCRIPT_NAME \
-- "$@"`
if [ $? != 0 ] ; then _usage ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true; do
case "$1" in
-s | --server ) server="$2"; shift 2 ;;
--catalog ) catalog="$2"; shift 2 ;;
--schema ) schema="$2"; shift 2 ;;
--user ) user="$2"; shift 2 ;;
--sql-file ) sql_file="$2"; shift 2 ;;
--extract-start-time ) extract_start_time="$2"; shift 2 ;;
--extract-end-time ) extract_end_time="$2"; shift 2 ;;
--args ) args="$2"; shift 2 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
# check
if [ -z "$server" ] || [ -z "$catalog" ] || \
[ -z "$schema" ] || [ -z "$user" ] || \
[ -z "$sql_file" ] ;
then
echo ""
echo "Error: parameter(s) is empty."
_usage
exit 1
fi
if [ ! -f "$sql_file" ] ;
then
echo ""
echo "Error: sql file not exist: $sql_file"
_usage
exit 1
fi
# transform date
_today=$(date +%Y-%m-%dT00:00:00Z )
_yesterday=$(date -d'-1 day' +%Y-%m-%dT00:00:00Z )
if [ "$extract_start_time" = "yesterday" ] ;
then
extract_start_time=$_yesterday
fi
if [ "$extract_end_time" = "today" ] ;
then
extract_end_time=$_today
fi
echo ""
echo "----------------------- Parameters -----------------------"
echo "server : $server"
echo "catalog : $catalog"
echo "schema : $schema"
echo "user : $user"
echo "sql-file : $sql_file"
echo "extract_start_time : $extract_start_time"
echo "extract_end_time : $extract_end_time"
echo "args : $args"
# replace variables
temp_file=$(mktemp)
trap "rm -f $temp_file" INT TERM HUP EXIT
cp -r $sql_file $temp_file
sed -i "s/\${extract_start_time}/date_parse('$extract_start_time','%Y-%m-%dT%H:%i:%sZ')/g" $temp_file
sed -i "s/\${extract_end_time}/date_parse('$extract_end_time','%Y-%m-%dT%H:%i:%sZ')/g" $temp_file
# custom args
if [ ! -z "$args" ] ;
then
for kv in ${args//;/ }; do
_k=`echo $kv | cut -d'=' -f1`
_v=`echo $kv | cut -d'=' -f2`
sed -i "s/\${$_k}/$_v/g" $temp_file
done
fi
echo ""
echo "----------------------- SQL -----------------------"
cat $temp_file
CMD="presto-cli
--server $server
--catalog $catalog
--schema $schema
--user $user
--session hive.insert_existing_partitions_behavior=OVERWRITE
--file $temp_file
"
# echo ""
# echo "----------------------- Command -----------------------"
# echo "$CMD"
echo ""
echo "----------------------- Result -----------------------"
eval $CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment