蜂巢 |在日期创建分区



我需要在csv文件之上创建一个外部配置单元表。CSV 具有 col1、col2、col3 和 col4。

但是我的外部配置单元表应该按分区,但我的 csv 文件没有任何月份字段。 col1 是日期字段。 我该怎么做?

您需要将数据重新加载到分区表中。

  1. 使用 CSV 在文件夹顶部创建非分区表(mytable(。
  2. 创建分区表 (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

  3. 使用动态分区将数据加载到分区表中,计算查询中的分区列:

    set 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';

最新更新