在HIVE中删除一系列分区

13

我有一个按照日期分区、类型为字符串的Hive表(版本为0.11.0)。我想知道是否有一种在Hive中删除一定日期范围内分区的方法(例如从“date1”到“date2”)。我尝试了以下SQL类型的查询,但它们似乎语法不正确:

ALTER TABLE myTable DROP IF EXISTS PARTITION
(date>='date1' and date<='date2');

ALTER TABLE myTable DROP IF EXISTS PARTITION
(date>='date1' && date<='date2');

ALTER TABLE myTable DROP IF EXISTS PARTITION
(date between 'date1' and 'date2');
2个回答

21

我尝试过这个语法,它能用。

ALTER TABLE mytable DROP PARTITION (dates>'2018-04-14',dates<'2018-04-16');

命令输出:

    Dropped the partition dates=2018-04-15/country_id=107
    Dropped the partition dates=2018-04-15/country_id=110
    Dropped the partition dates=2018-04-15/country_id=112
    Dropped the partition dates=2018-04-15/country_id=14
    Dropped the partition dates=2018-04-15/country_id=157
    Dropped the partition dates=2018-04-15/country_id=159
    Dropped the partition dates=2018-04-15/country_id=177
    Dropped the partition dates=2018-04-15/country_id=208
    Dropped the partition dates=2018-04-15/country_id=22
    Dropped the partition dates=2018-04-15/country_id=233
    Dropped the partition dates=2018-04-15/country_id=234
    Dropped the partition dates=2018-04-15/country_id=76
    Dropped the partition dates=2018-04-15/country_id=83
    OK
    Time taken: 0.706 seconds

我正在使用Hive 1.2.1000.2.5.5.0-157版本。


认为这将删除表上的所有内容?大于201-04-14或小于2018-04-16... - Hein du Plessis
1
@HeinduPlessis 这是AND而不是OR。示例:dates>'2018-04-14' and dates<'2018-04-16' - Kharthigeyan
1
@HeinduPlessis,不会删除其他分区,它只会删除中间的那些。 - lhdgriver
@HeinduPlessis,您能否确认这个答案比我在2017年8月发的帖子更优雅、更时髦?如果是的话,我很乐意删除我误导和过时的解决方案。非常感谢和最好的祝愿。 - vreyespue
1
@vreyespue 我认为两者都有用 - 你的脚本更加动态,但对于OP(和我)的要求,这个答案更容易应用。 - Hein du Plessis
2
@Kharthigeyan,我确实没有意识到这是一个AND逻辑。 - Hein du Plessis

2
解决方案:alter table myTable drop partition (unix_timestamp('date1','yyyy-MM-dd')>=unix_timestamp(myDate,'yyyy-MM-dd') and unix_timestamp('date2','yyyy-MM-dd')<=unix_timestamp(myDate,'yyyy-MM-dd'));


这里的date1、date2和myDate是什么?假设你想要删除2018年11月1日至2019年2月12日之间的数据? - Paul Velthuis
修改表 mytable,删除分区(myDate > '2018-11-01',myDate < '2019-02-12')。 - HakkiBuyukcengiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接