因为一些原因需要批量清空外表数据,故编写脚本进行操作:
#!/bin/bash
## 配置文件位置
conf_pwd=/data/shell/xxxx.conf
## mysql host
mysql_host=$(cat ${conf_pwd} | grep mysql_host |awk -F"=" '{print $2}')
## mysq用户名
mysql_user=$(cat ${conf_pwd} | grep mysql_user |awk -F"=" '{print $2}')
## mysql密码
mysql_pwd=$(cat ${conf_pwd} | grep mysql_pwd |awk -F"=" '{print $2}')
## mysql数据库
mysql_db=$(cat ${conf_pwd} | grep mysql_db |awk -F"=" '{print $2}')
## mysql连接
mysql_conn="mysql -h${mysql_host} -u${mysql_user} -p${mysql_pwd} -s -e"
## mysql数据库循环查询
query_sql="select Lower(t.tablename) from table_list t;"
## 得到表名称
query_result=$(${mysql_conn} "${query_sql}")
## hive beeline连接
hive_beeline=$(cat ${conf_pwd} | grep hive_beeline |awk -F"=" '{print $2}')
## 将外表转换为内表,清空数据后,再转回外表
for tablename in ${query_result}
do
echo ${tablename}
hive_sql="alter table db.${tablename} set TBLPROPERTIES('EXTERNAL'='false');truncate table db.${tablename};alter table db.${tablename} set TBLPROPERTIES('EXTERNAL'='TRUE');"
${hive_beeline} -e "${hive_sql}"
done
注:Hive转换为外表时,TRUE大写!!!TRUE大写!!!TRUE大写!!!否则会不成功的