繁体   English   中英

Hive | 在日期创建分区

[英]Hive | Create partition on a date

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

但是我的外部 hive 表应该按月分区,但我的 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. 使用动态分区将数据加载到分区表中,在查询中计算分区列:

    设置 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM