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

时间:2017-09-01 16:33:43

标签: hadoop 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无法强制转换   to org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc。“

还有其他办法吗?

1 个答案:

答案 0 :(得分:1)

演示

<强>蜂房

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

bash

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> show partitions mytable;
OK
partition
day=2017-09-01
相关问题