2014-01-31 124 views
7

我試圖從CREATE AS和HiveCLI上的動態分區的另一個表中創建一個新表。我是從蜂巢官方的wiki學習哪裏有這個例子:動態分區+在HIVE上創建AS

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; 

但我收到此錯誤:

FAILED: SemanticException [Error 10065]:

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

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

回答

16

既然你已經知道的全模式目標表,請嘗試先創建並使用LOAD DATA命令填充它:

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; 

注意:由於您正在執行完整的動態分區插入,因此需要set命令。

+0

是的,我做到了,它似乎是最好的和獨特的解決方案 –

+4

如果你*不知道目標表的完整模式,最好的方法是什麼?是否有可能只使用配置單元查詢從未分區的表生成分區表? – Noah

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; 

在上面的代碼,而不是Create語句中使用:CREATE TABLE T like srcpart;

如果分區類似。