我需要在csv文件顶部创建一个外部配置单元表。 CSV具有col1,col2,col3和col4。
但是我的外部配置单元表应该在 month 上进行分区,但是我的csv文件没有任何month字段。 col1是日期字段。 我该怎么办?
答案 0 :(得分:1)
您需要将数据重新加载到分区表中。
创建分区表(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
;
答案 1 :(得分:1)
尝试这种方式
将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';