如何将现有的每小时分区合并到配置单元中的每日分区

hk8txs48  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(456)

我的要求是合并现有的每小时分区到每日分区的所有日子。
我的分区列如下:

  1. 2019_06_22_00, 2019_06_22_01, 2019_06_22_02, 2019_06_22_03..., 2019_06_22_23 => 2019_06_22
  2. 2019_06_23_00, 2019_06_23_01, 2019_06_23_02, 2019_06_23_03..., 2019_06_23_23 => 2019_06_23
wkyowqbh

wkyowqbh1#

简单的方法是从当前分区列中提取数据并加载到新表中。
创建新表:

  1. create table new (
  2. ...
  3. )
  4. partitioned by (partition_date date);

然后从旧表插入覆盖:

  1. set hive.exec.dynamic.partition=true;
  2. set hive.exec.dynamic.partition.mode=nonstrict;
  3. insert overwrite table new partition(partition_date )
  4. select
  5. col1,
  6. col2,
  7. ...
  8. coln,
  9. --extract hours if you need this column
  10. substr('old_partition_col',12,2) hour,
  11. --partition column is the last one
  12. date(concat_ws('-',substr(old_partition_col,1,4),substr(old_partition_col,6,2),substr(old_partition_col,9,2))) as partition_date
  13. from old_table;

或者,您可以使用 unix_timestamp 以及 from_unixtime 功能:

  1. from_unixtime(unix_timestamp(old_partition_col,'yyyy_MM_dd_HH'),'yyyy-MM-dd') as partition_date

然后删除旧表并重命名新表。

展开查看全部

相关问题