【问题标题】:Add a new partition in hive external table and update the existing partition to column of the table to non-partition column在 hive 外部表中添加新分区并将现有分区更新为表的列到非分区列
【发布时间】:2021-11-05 05:37:31
【问题描述】:

我在现有表 emp 的下方,分区列为 as_of_date(current_date -1)。

CREATE EXTERNAL TABLE IF NOT EXISTS emp(
  student_ID INT, 
  name STRING)
  partitioned by (as_of_date date)
  ROW FORMAT DELIMITED
  FIELDS TERMINATED BY ','
  STORED AS TEXTFILE
  LOCATION '/user/emp';

下面是现有的分区路径

user/emp/as_of_date=2021-09-02
user/emp/as_of_date=2021-09-03
user/emp/as_of_date=2021-09-04

在emp表中,我必须将新的分区列添加为businessdate(current_date)并将分区列(as_of_date)更改为非分区列。

预期的输出应该如下。

describe table emp;
CREATE EXTERNAL TABLE IF NOT EXISTS emp(
student_ID INT, 
Name STRING, 
as_of_date date)
  partitioned by (businessdate date)
  ROW FORMAT DELIMITED
  FIELDS TERMINATED BY ','
  STORED AS TEXTFILE
  LOCATION '/user/emp';

更新后为hdfs路径

user/emp/buinessdate=2021-09-03
user/emp/buinessdate=2021-09-04
user/emp/businessdate=2021-09-05

预期输出表:

|student_ID |name |as_of_date | business_date |
|--| --- | --- |----|
|1 | Sta |2021-09-02| 2021-09-03 |
|2 |Danny|2021-09-03| 2021-09-04 |
|3 |Elle |2021-09-04| 2021-09-05 |

【问题讨论】:

    标签: hadoop hive hdfs hql partition


    【解决方案1】:

    创建新表,从旧表加载数据,删除旧表,重命名新表。

    --1 创建新表emp1

    CREATE EXTERNAL TABLE emp1(
    student_ID INT, 
    Name STRING, 
    as_of_date date)
      partitioned by (businessdate date)
      ROW FORMAT DELIMITED
      FIELDS TERMINATED BY ','
      STORED AS TEXTFILE
      LOCATION '/user/emp1';
    

    --2 将数据从emp中加载到emp1中,计算出新的分区列

    --dynamic partition mode
    SET hive.exec.dynamic.partition=true;
    SET hive.exec.dynamic.partition.mode=nonstrict;
    
    insert overwrite table emp1 partition (business_date)
    select 
         student_ID, 
         Name, 
         as_of_date,
         date_add(as_of_date,1) as business_date
     from emp;
    

    现在您可以删除旧表(也可以首先管理删除位置)并在必要时重命名新表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 2020-09-08
      • 2015-09-02
      • 2017-03-09
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多