【问题标题】:Creating a partitioned hive table from a non partitioned table从非分区表创建分区 hive 表
【发布时间】:2015-11-07 00:17:07
【问题描述】:

我有一个 Hive 表,它是通过连接来自多个表的数据创建的。此数据位于一个包含多个文件(“0001_1”、“0001_2”、...等等)的文件夹中。我需要根据该表中名为pt_dt 的日期字段创建一个分区表(通过更改此表或创建一个新表)。有没有办法做到这一点?

我尝试创建一个新表并将其插入(如下),但没有成功

create external table table2 (acct_id bigint, eval_dt string)
partitioned by (pt_dt string);
insert into table2
partition (pt_dt) 
select acct_id, eval_dt, pt_dt
from jmx948_variable_summary;

这会引发错误

“失败:执行错误,从 org.apache.hadoop.hive.ql.exec.mr.MapRedTask 返回代码 2 MapReduce 职位发布: Stage-Stage-1:映射:189 累积 CPU:401.68 秒 HDFS 读取:0 HDFS 写入:0 FAIL MapReduce CPU 总耗时:6 分 41 秒 680 毫秒"

【问题讨论】:

    标签: hive partition


    【解决方案1】:

    经过一些试验和错误后能够弄清楚。

    在 Hive 中启用动态分区:

    SET hive.exec.dynamic.partition = true;
    SET hive.exec.dynamic.partition.mode = nonstrict;
    

    为分区表创建架构:

    CREATE TABLE table1 (id STRING, info STRING)
    PARTITIONED BY ( tdate STRING);
    

    插入分区表:

    FROM table2 t2
    INSERT OVERWRITE TABLE table1 PARTITION(tdate)
    SELECT t2.id, t2.info, t2.tdate
    DISTRIBUTE BY tdate;
    

    【讨论】:

      【解决方案2】:

      在我正在使用的以下版本中(Hive 0.14.0.2.2.4.2-2)

      INSERT INTO TABLE table1 PARTITION(tdate) SELECT t2.id, t2.info, t2.tdate
      

      从源表中选择需要按last分区的列,在上面的例子中,选择日期作为Select中的最后一列。同样,如果需要按“info”列对表进行分区,则

      INSERT INTO TABLE table1 PARTITION(info) SELECT t2.id, , t2.tdate, t2.info
      

      如果您想创建具有多个分区的表,则选择查询需要按该顺序排列。如果你想用“日期”和“信息”对上表进行分区

      INSERT INTO TABLE table1 PARTITION(date, info) SELECT t2.id, , t2.tdate, t2.info
      

      先是“信息”,然后是“日期”

      INSERT INTO TABLE table1 PARTITION(info, date) SELECT t2.id, , t2.info, t2.tdate
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-13
        • 1970-01-01
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多