如何在Hive中一次性删除所有分区?

15

Hive版本1.1

我有一个如下所示的外部表:

 CREATE EXTERNAL TABLE `schedule_events`(
  `schedule_id` string COMMENT 'from deserializer',
  `service_key` string COMMENT 'from deserializer',
  `event_start_date_time` string COMMENT 'from deserializer',
  `event_id` string COMMENT 'from deserializer',
  `event_type` string COMMENT 'from deserializer',
  `transitional_key` string COMMENT 'from deserializer',
  `created_date_time` string COMMENT 'from deserializer',
  `bus_date` string COMMENT 'from deserializer')
    PARTITIONED BY (
                    `year` string,
                    `month` string,
                    `day` string)
   ROW FORMAT SERDE
   'org.apache.hadoop.hive.serde2.avro.AvroSerDe'
   STORED AS INPUTFORMAT
   'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat'
   OUTPUTFORMAT
   'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat'
   LOCATION
   'hdfs://nameservice1/hadoop/raw/omega/scheduled_events'
  TBLPROPERTIES (
    'avro.schema.url'='hdfs:////hadoop/raw/omega/schema/schedule_events.avsc',
   'transient_lastDdlTime'='1505742141')

现在,我可以运行以下 ALTER 命令来删除特定的分区:

 ALTER TABLE schedule_events DROP IF EXISTS PARTITION  (year='2016',month='06',day='01')
 Dropped the partition year=2016/month=06/day=01

 hive> show partitions schedule_events;
 OK
 year=2017/month=09/day=01
 year=2017/month=09/day=02
 year=2017/month=09/day=03
 year=2017/month=09/day=04
 year=2017/month=09/day=05

但是这个表有很多分区。

我该如何一次性删除所有现有的分区?我想一次性删除所有现有的分区,这可能吗?


尝试使用以下命令ALTER TABLE schedule_events DROP IF EXISTS PARTITION (year is not null) - Ambrish
@Ambrish 我不认为那会行得通。你的查询 ALTER TABLE schedule_events DROP IF EXISTS PARTITION (year is not null) 会检查是否存在一个名为 year is not null 的分区,而这是错误的。 - Ani Menon
5个回答

28

有多个选项,这里是其中之一:

alter table schedule_events drop if exists partition (year<>'');

Hive:扩展ALTER TABLE DROP PARTITION语法以使用所有比较符号

"... 要从Hive表中删除分区,可以使用以下命令:
ALTER TABLE foo DROP PARTITION(ds = 'date')
... 但是应该也可以删除在某个日期之前的所有分区。
ALTER TABLE foo DROP PARTITION(ds < 'date') 本任务旨在实现对所有比较符号 < > <= >= <> = != 的 ALTER TABLE DROP PARTITION 操作,而不仅限于 ="

https://issues.apache.org/jira/browse/HIVE-2908


1
作为一个旁注,我在 AWS Athena 上尝试过了,但并没有成功。我收到了以下错误代码:no viable alternative at input 'alter table TABLE_NAME drop' (service: amazonathena; status code: 400; error code: invalidrequestexception; request id: 3fe0eb78-2a17-...) - otmezger
@otmezger,Athena与Hive无关。 - David דודו Markovitz
Athena只是在引擎盖下的Hive,David是错的。问题(从错误消息中很难看出来)是Athena坚持使用双引号而不是单引号。 - Bill Clark
1
@BillClark - 不,Athena在内部是Presto。https://docs.aws.amazon.com/athena/latest/ug/presto-functions.html https://aws.amazon.com/big-data/what-is-presto/ - David דודו Markovitz
2
足够公平,但两者之间的差异在此无关紧要。重点是错误是由于使用单引号而不是双引号引起的,这从错误消息本身并不明显。否则,对于Hive、Presto(以及因此Athena)来说,语法是相同的。 - Bill Clark

13

你可以使用类似这样的东西:

ALTER TABLE schedule_events drop if exists partition (year>'0');

ALTER TABLE table_name DROP IF EXISTS PARTITION(year>0)。这个查询对我有用。非常感谢。 - sunitha

1

请删除schema_name.table_name表中partition_column不为空的分区。


1
使用Spark SQL:
val paritions_values = spark.sql("show partitions "+databasename+'.'+tablename)
.collect().map(f=>f(0).toString)
.toArray.mkString("partition(", "," , "\")")
.replace("," , "\") ,partition(")
.replace("=", "=\"")

spark.sql("alter table "+databasename+'.'+tablename+" drop "+paritions_values)

0
例如:假设分区是按日期进行的,名称为partition_column:-
alter table database.table_name drop if exists partition (partition_column>'2023-01-01');

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