#!/bin/bash # AMPY script to drop/recreate a subset of the ampy tables # John Bonifas # last updated 07-10-20 # https://docs.cloudera.com/documentation/enterprise/5-9-x/topics/impala_shell_options.html let "var=0"; TARGETFOLDER=/development/etl/source/ampy; echo '' > dropandrecreate_errors.txt; echo '' > dropandrecreate_log.txt; # the input flat file MUST be run through the dos2unix utility first or it won't work here, # due to the carriage return / linefeeds in Windows mapfile -t OUTPUTTABLES < <(cat tablenamesNeedDropRecreate.csv) for TABLENAME in ${OUTPUTTABLES[@]} do let "VAR++"; echo Number $VAR ... Working with table: $TABLENAME; CREATEHQLFILE=$TARGETFOLDER/$TABLENAME/create_table_ampy-$TABLENAME.hql; CREATEQUERY=`hdfs dfs -cat $CREATEHQLFILE`; # https://stackoverflow.com/questions/40714970/escaping-forward-slashes-in-sed-command/40715028 # you can use | instead of / to avoid having to escape out forward slashes RUNQUERY=`echo "$CREATEQUERY" | sed 's|${environment}|dev_|g;s|${directory}|development/data|g;s|varchar(8000)|string|g'`; #DROP AND RECREATE impala-shell -k -i srphdw010 --quiet -q "use dev_ampy;drop table if exists $TABLENAME purge;" && echo "$RUNQUERY" | impala-shell -k -i srphdw010 --quiet -f - 2>>dropandrecreate_errors.txt; echo -e dropped and recreated $TABLENAME. "\n" | tee -a dropandrecreate_log.txt; echo -e "\n"; done