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"

posted @ 2017-08-24 21:51  牵牛花  阅读(919)  评论(0编辑  收藏  举报