通过创建临时表合并某一个库的hive小文件

#!/bin/bash
#需要指定hive中的库名

#set -x
set -e

DB=$1

if [ -z $1 ];then
    echo “Usage:$0  DbName”
    exit 1
fi

#注释掉cron对应的计划任务
row=$(sed -n '/sjob_exec.sh/=' /etc/crontab)
sed -i "$row s/^/#/" /etc/crontab

#获取hive表定义
ret=$(sudo -u hdfs hive -e "use ${DB};show tables;"|grep -v _tmp|grep -v importinfo)

for tem in $ret;
do
    echo ${tem}
    sudo -u hdfs hive -e "use ${DB};drop table ${tem}_tmp"
    sudo -u hdfs hive -e "use ${DB};create table ${tem}_tmp_1 as select * from ${tem}"
    sudo -u hdfs hive -e "use ${DB};alter table ${tem} rename to ${tem}_tmp"
    sudo -u hdfs hive -e "use ${DB};alter table ${tem}_tmp_1 rename to ${tem}"
done

impala-shell -q 'invalidate metadata' -i hslave1

#恢复对应的cron任务
row=$(sed -n '/sjob_exec.sh/=' /etc/crontab)
sed -i "$row s/#//" /etc/crontab

你可能感兴趣的:(Hive)