hive 定时加载分区

#!/bin/bash
#每天定时位外部表加载分区
#服务器当天的时间
#加载环境变量
source /etc/profile;
#如果没有指定日期用当前日期如果指定的日期使用指定的日期
echo 'starting...'
if [ -z $1 ]
then
curdate=`date +%Y%m%d`
else
curdate=$1
fi
# alter table click add if not exists partition(logdate='20170821') LOCATION '/maats5/click/logdate=20170821';

#数据库表
tableList="click install register login pay"
#为所有表加载当天的分区
addPartitionOfCurDate_All() {
for table in $tableList
do
echo "deal with " $table
createHdfsDir $table $curdate
addPartition $table $curdate
done
}

#判断分区是否存在,如果不存在则创建
createHdfsDir(){
#$1=tablename,$2=curdate
hdfs dfs -test -d /maats5/$1/logdate=$2
if [ ! $? -eq 0 ] ;then
#如果不存在则创建这个文件
hdfs dfs -mkdir /maats5/$1/logdate=$2
fi
}

#加载指定表的分区
addPartition(){
#$1=tablename, $2=curdate
/home/hadoop/apps/hive/bin/hive -e "alter table maats.$1 add if not exists partition(logdate='$2') LOCATION '/maats5/$1/logdate=$2';" 1>/home/hadoop/maats/crontabTask/maatsLogs/crontab_hive.std 2>/home/hadoop/maats/crontabTask/maatsLogs/crontab_hive.err
}

#删除分区
deletePartition(){
/home/hadoop/apps/hive/bin/hive -e "alter table maats.$1 drop if exists partition(logdate='$2') " 1>/home/hadoop/maats/crontabTask/maatsLogs/crontab_hive.std 2>/home/hadoop/maats/crontabTask/maatsLogs/crontab_hive.err
}

#执行
addPartitionOfCurDate_All
echo "ending"

转载于:https://www.cnblogs.com/rocky-AGE-24/p/7425440.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值