通过查询hive元数据,获取到建表语句
db=$1
if [ -z "$db" ];then
echo "请指定数据库名称:"
exit 1
fi
if [ ! -d "tmp" ]; then
mkdir tmp
fi
errFile="err.info"
>$errFile
mkdir /opt/sql/EMR/${db}
for t in `impala-shell -i 127.0.0.1:25003 -k -B -q "use $db;show tables;"|grep -v _view`;do
echo ${db}.$t
parqFile="${db}/${db}.${t}.sql"
> $parqFile
for hdfs in `impala-shell -i 127.0.0.1:25003 -k -B -q "show partitions ${db}.$t;"|awk '{print $NF}'|grep ^hdfs`
do
parqStr=""
newhdfs=`echo $hdfs|sed 's#hdfs://WH-BI-NS##g'`
parqinfo=`echo $hdfs|grep -oP "\w*=?\d+-\d+-\d+.*$"`
if [ -z "$parqinfo" ];then
echo "${db}.${t} is not a partition table" >> $errFile
continue
fi
if [ `echo $parqinfo|grep -c "="` -eq 0 ];then
parq=`echo $parqinfo|grep -oP "\d+-\d+-\d+"`
parqStr="dt='${parq}'"
elif [ `echo $parqinfo|grep -c "="` -eq 1 ]; then
parqStr=`echo $parqinfo|sed -e "s#=#='#g" -e "s#/#',#g"`"'"
else
echo "${db}.${t} may have not format partitions, please confirm.." >> $errFile
continue
fi
echo "ALTER TABLE ${db}.${t} ADD IF NOT EXISTS PARTITION (${parqStr}) LOCATION '${newhdfs}';" >> $parqFile
done
done