#!/bin/sh
tmpdir="/root/a.txt"
tmpfile2="/root/a2.txt"
#读取msql表
mysql -u root -p123456 < /root/b.sql > ${tmpdir}
sed -i '1d' ${tmpdir}
cat ${tmpdir} | cut -f 1,2 > ${tmpfile2}
nameAndTypebuffer=""
while read line
do
#echo "${line}"
#获取前两列
name=`echo $line |cut -d " " -f 1`
type=`echo $line |cut -d " " -f 2`
#更改类型
if [[ $type =~ "char" ]];then
type="string"
fi
if [[ $type =~ "int" ]];then
type="int"
fi
if [[ $type =~ "date" ]];then
type="date"
fi
if [[ $type =~ "long" ]];then
type="bigint"
fi
#echo $name $type
nameAndTypebuffer="$nameAndTypebuffer,$name $type"
done < ${tmpfile2}
#拼接sql,去掉,
nameAndTypebuffer=`echo $nameAndTypebuffer |sed 's/./(/'`
#echo $nameAndTypebuffer
databasename=$1
tablename=$2
sql="create table $databasename.$tablename $nameAndTypebuffer ) row format delimited fields terminated by ',';"
echo $sql >/root/hive_createtable.sql
echo "desc $databasename.$tablename;" >>/root/hive_createtable.sql
echo $sql
hive -f /root/hive_createtable.sql