如何在配置单元中按月和日对表进行分区

时间:2018-09-01 14:57:39

标签: hive hiveql hive-partitions

我用以下方法创建了一个表

CREATE EXTERNAL TABLE extab (
vendorID string, 
orderID string , 
ordertime string
) 
location '/common_folder/data'

然后我按月和日创建了一个分区

CREATE EXTERNAL TABLE part_extab(
endorID string, 
orderID string , 
ordertime string
) 
PARTITIONED by (month string, day string)
location '/common_folder/data'

然后将数据插入分区表

INSERT OVERWRITE TABLE 
select vendorId, orderId, ordertime , month, day
FROM extab

我该如何从订购时间中提取month,day?

1 个答案:

答案 0 :(得分:0)

使用动态分区加载。如果您的日期格式正确,则month()day()函数将起作用:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

INSERT OVERWRITE TABLE part_extab partiion (month, day)
select vendorId, orderId, ordertime , 
       lpad(month(ordertime),2,0) as month,  
       lpad(day(ordertime),2,0) as day
FROM extab;

或者,您可以使用substr()提取月份和日期,例如this答案