在当前日期之前动态删除配置单元中的分区

s3fp2yjn  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(357)

在工作中,我们每天都会收到一个新文件,并将其传输到hive。表按列进行分区“ day “包含数据传输到配置单元的时间,格式为” yyyy-MM-dd ". 每当添加新文件时,我们都希望删除以前的分区,以便表只包含最新文件的数据。有没有一种方法可以编写一个hiveql查询来动态删除当前日期之前的任何分区?我试过:

alter table table_name drop if exists partition (day < current_date);

但我有个错误:
失败:classcastexception org.apache.hadoop.hive.ql.plan.exprnodegenericfuncdesc不能强制转换为org.apache.hadoop.hive.ql.plan.exprnodeconstantdesc。“
还有别的办法吗?

wvyml7n5

wvyml7n51#

演示

Hive

hive> create table mytable (i int) partitioned by (day date);
OK
hive> alter table mytable add partition (day=date '2017-08-29') partition (day=date '2017-08-30') partition (day=date '2017-08-31') partition (day=date '2017-09-01');
OK
hive> show partitions mytable;
OK
partition
day=2017-08-29
day=2017-08-30
day=2017-08-31
day=2017-09-01

猛击

date +"%Y-%m-%d"
2017-09-01
hive --hivevar today=$(date +"%Y-%m-%d") -e \
> 'alter table local_db.mytable drop partition (day<date '\''${hivevar:today}'\'')'
Dropped the partition day=2017-08-29
Dropped the partition day=2017-08-30
Dropped the partition day=2017-08-31
OK

Hive

hive> show partitions mytable;
OK
partition
day=2017-09-01

相关问题