Dynamic Partitioning + CREATE AS on HIVE

本秂侑毒 提交于 2019-12-20 12:11:34

问题


I'm trying to create a new table from another table with CREATE AS and dynamic Partitioning on HiveCLI. I'm learning from Hive official wiki where there is this example:

 CREATE TABLE T (key int, value string) 
 PARTITIONED BY (ds string, hr int) AS
 SELECT key, value, ds, hr+1 hr1 
   FROM srcpart 
   WHERE ds is not null 
   And hr>10;

But I received this error:

FAILED: SemanticException [Error 10065]:

CREATE TABLE AS SELECT command cannot specify the list of columns for the target table

Source: https://cwiki.apache.org/confluence/display/Hive/DynamicPartitions#DynamicPartitions-Syntax


回答1:


Since you already know the full schema of the target table, try creating it first and the populating it with a LOAD DATA command:

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

CREATE TABLE T (key int, value string) 
PARTITIONED BY (ds string, hr int);

INSERT OVERWRITE TABLE T PARTITION(ds, hr) 
SELECT key, value, ds, hr+1 AS hr 
   FROM srcpart 
   WHERE ds is not null 
   And hr>10;

Note: the set command is needed since you are performing a full dynamic partition insert.




回答2:


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

CREATE TABLE T (key int, value string) 
PARTITIONED BY (ds string, hr int);

INSERT OVERWRITE TABLE T PARTITION(ds, hr) 
SELECT key, value, ds, hr+1 AS hr 
FROM srcpart 
WHERE ds is not null 
      And hr>10;

In the above code, instead of the Create statement use: CREATE TABLE T like srcpart;

In case the partitioning is similar.



来源:https://stackoverflow.com/questions/21477855/dynamic-partitioning-create-as-on-hive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!