[英]Hive | Create partition on a date
我需要在 csv 文件之上創建一個外部 hive 表。 CSV 有 col1、col2、col3 和 col4。
但是我的外部 hive 表應該按月分區,但我的 csv 文件沒有任何月份字段。 col1 是日期字段。 我怎樣才能做到這一點?
您需要將數據重新加載到分區表中。
創建分區表 (mytable_part)
create table mytable_part( --columns specification here for col1, col2, col3, col4 ) partitioned by (part_month string)... stored as textfile --you can chose any format you need
使用動態分區將數據加載到分區表中,在查詢中計算分區列:
設置 hive.exec.dynamic.partition=true; 設置 hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table mytable_part partition (part_month) select col1, col2, col3, col4, substr(col1, 1, 7) as part_month --partition column in yyyy-MM format from mytable distribute by substr(col1, 1, 7) --to reduce the number of files;
試試這個方法
將 csv 數據復制到 HDFS 位置 hdfs://somepath/5 的文件夾中,並將該路徑作為分區添加到外部表中。
create external table ext1(
col1 string
,col2 string
,col3 string
,col4 string
)
partition by (mm int)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS ORC;
alter table ext1 add partition(mm = 5) location 'hdfs://yourpath/5';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.