如何在配置单元中一次删除所有分区?

px9o7tmv  于 2021-06-26  发布在  Hive
关注(0)|答案(4)|浏览(508)

配置单元版本1.1
我有一个Hive外部表如下:

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

但是这个表有很多分区。
如何一次删除所有现有分区?我想一次删除所有现有分区吗?有可能吗?

nfg76nw0

nfg76nw01#

alter table schema\u name.table\u name drop partition(partition\u column!='');

ruyhziif

ruyhziif2#

有多种选择,这里有一个:

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

配置单元:扩展alter table drop partition语法以使用所有比较器
"... 要从配置单元表中删除分区,请执行以下操作:
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

kgqe7b3p

kgqe7b3p3#

您可以使用类似的方法:

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

wlwcrazw4#

使用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)

相关问题