#!/bin/sh
###################
. /etc/profile
. ~/.bash_profile
##################
SCRIPT_NAME=$(readlink -f "$0")
dir=`dirname ${SCRIPT_NAME}`
cd $dir
if [[ -z $1 ]]
then
echo "Useage : $0 tablename"
exit
else
table=$1
fi
HOST=10.10.83.141
USER=hivemeta
PAWD=hivemeta
DATABASE=hive8meta
mysql -h$HOST -u$USER -p$PAWD $DATABASE -e "SELECT t5.TBL_ID,t5.TBL_TYPE,t5.CD_ID,t5.SLIB,t6.PARAM_VALUE,t5.LOCATION,t5.INPUT_FORMAT,t5.OUTPUT_FORMAT from
(SELECT t3.TBL_ID,t3.TBL_TYPE,t3.CD_ID,t4.SLIB,t3.SERDE_ID,t3.LOCATION,t3.INPUT_FORMAT,t3.OUTPUT_FORMAT from (SELECT t1.TBL_ID,t1.TBL_TYPE,t2.CD_ID,t2.SERDE_ID,t2.LOCATION,t2.INPUT_FORMAT,t2.OUTPUT_FORMAT from
(SELECT TBL_ID,SD_ID,TBL_TYPE FROM hive8meta.TBLS where TBL_NAME='$table')t1
join
(SELECT SD_ID,CD_ID,SERDE_ID,LOCATION,INPUT_FORMAT,OUTPUT_FORMAT FROM hive8meta.SDS)t2
on t1.SD_ID=t2.SD_ID)t3
join
(SELECT SERDE_ID,SLIB FROM hive8meta.SERDES)t4
on t3.SERDE_ID =t4.SERDE_ID)t5
left join
(SELECT SERDE_ID,PARAM_VALUE FROM hive8meta.SERDE_PARAMS where PARAM_KEY='field.delim')t6
on t5.SERDE_ID=t6.SERDE_ID" > $dir/information
sed -i '1d' $dir/information
TBL_ID=`cat $dir/information | awk '{print $1}'`
TBL_TYPE=`cat $dir/information | awk '{print $2}'`
CD_ID=`cat $dir/information | awk '{print $3}'`
SLIB=`cat $dir/information | awk '{print $4}'`
PARAM_VALUE=`cat $dir/information | awk '{print $5}'`
LOCATION=`cat $dir/information | awk '{print $6}' | awk -F '9000' '{print $2}'`
INPUT_FORMAT=`cat $dir/information | awk '{print $7}'`
OUTPUT_FORMAT=`cat $dir/information | awk '{print $8}'`
mysql -h$HOST -u$USER -p$PAWD $DATABASE -e "SELECT PKEY_NAME,PKEY_TYPE FROM hive8meta.PARTITION_KEYS where TBL_ID=$TBL_ID order by INTEGER_IDX asc;" > $dir/partition
sed -i '1d' $dir/partition
cat $dir/partition | tr "\n" "," > $dir/partition_value
sed -i 's/,$//g' $dir/partition_value
PARTITION=`cat $dir/partition_value`
mysql -h$HOST -u$USER -p$PAWD $DATABASE -e "SELECT COLUMN_NAME,TYPE_NAME FROM hive8meta.COLUMNS_V2 where CD_ID=$CD_ID order by INTEGER_IDX asc;" > $dir/column
sed -i '1d' $dir/column
cat $dir/column | tr "\n" "," > $dir/column_value
sed -i 's/,$//g' $dir/column_value
COLUMN=`cat $dir/column_value`
CREATE_SQL="create "
if [[ "$TBL_TYPE" == "EXTERNAL_TABLE" ]];then
CREATE_SQL=$CREATE_SQL"EXTERNAL "
fi
CREATE_SQL=$CREATE_SQL"TABLE "$table"("$COLUMN") "
num=`cat $dir/partition | wc -l`
if [[ $num -ge 1 ]];then
CREATE_SQL=$CREATE_SQL"PARTITIONED BY("$PARTITION") "
fi
CREATE_SQL=$CREATE_SQL"ROW FORMAT "
if [[ "$PARAM_VALUE" != "NULL" ]];then
CREATE_SQL=$CREATE_SQL"delimited fields terminated by '"$PARAM_VALUE"' "
else
CREATE_SQL=$CREATE_SQL"SERDE '"$SLIB"' "
fi
CREATE_SQL=$CREATE_SQL"STORED AS INPUTFORMAT '"$INPUT_FORMAT"' OUTPUTFORMAT '"$OUTPUT_FORMAT"' LOCATION '"$LOCATION"'"
echo $CREATE_SQL
后期的hive版本,可以直接show create table $tablename 获得建表语句了。
hive 0.8.1版本获取建表语句方法
最新推荐文章于 2022-03-25 10:23:24 发布