如何从具有 MM/dd/yyyy 格式的日期列在 hive 中创建分区年、月、日

问题描述

每天加载数据。 需要创建一个带有日期列的分区。

日期
2021/3/15 上午 8:02:32
12/21/2020 下午 12:20:41

解决方法

您需要将表转换为表的分区。然后更改加载sql,使其正确插入表中。

  1. 创建一个与原始表相同的新表,并确保从列列表中排除分区列并将其添加到分区中,如下所示。
create table new_tab() partitioned by ( partition_dt string );
  1. 将数据从原始表加载到 new_tab 中。确保选择子句中的最后一列是分区列。
set hive.exec.dynamic.partition.mode=nonstrict;
insert into new_table partition(partition_dt ) 
select src.*,from_unixtime(unix_timestamp(dttm_column),'MM/dd/yyyy') as partition_dt from original_table src;
  1. 删除原始表并将 new_table 重命名为原始表。
drop table original_table ;
alter table new_table rename to original_table ;