#!/bin/bash
# Author: luke yan
# E-mail: 1510603449@qq.com
# Date: 2021/03/29
# Description: 批量删除hive脚本
db_name="tmp"
user_name="luke"
password="luke"
# 用于筛选出待删除表名的关键字
key_word="jtekt_sdwan"
curPath=$(cd "$(dirname "$0")"; pwd)
delete_tbls=${curPath}/delete_tbl_sql
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
info() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&1
}
source /etc/profile
# 查询需要删除的数据表名,并组装为表删除sql文件
info "查询需要删除的数据表名,并组装为表删除sql文件"
beeline -u \
"jdbc:hive2://cnguls033:2181,cnguls034:2181,cnguls032:2181/${db_name};serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" \
--verbose=true \
-n ${user_name} \
-p ${password} \
--outputformat=vertical \
-e "show tables;"|grep "${key_word}"|awk -v db_name="${db_name}" '{print "drop table if exists "db_name"."$2";"}' \
> ${delete_tbls}
# 执行sql文件删除数据表
info "执行sql文件删除数据表"
beeline -u \
'jdbc:hive2://cnguls033:2181,cnguls034:2181,cnguls032:2181/tmp;password=luke;serviceDiscoveryMode=zooKeeper;user=luke;zooKeeperNamespace=hiveserver2' \
--verbose=true \
-n luke \
-p luke \
--outputformat=vertical \
-f ${delete_tbls}
网友评论