hive-create table语句,带有“select query”和“partition by”命令

oalqel3c  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(695)

我想在配置单元中创建一个分区表。我知道先用“createtable…”来创建一个表结构。。。使用“insert into table”命令将数据插入表中
但是我要做的是将这两个命令组合成一个单一的查询,如下面所示,但它会抛出错误。

CREATE TABLE test_extract AS
SELECT 

* 

FROM master_extract 
PARTITION BY (year string
,month string)
;

年和月都是主表中两个独立的列。
有什么办法可以达到这样的目的吗?

vom3gejh

vom3gejh1#

不,这是不可能的,因为create table as select(ctas)有限制:

The target table cannot be a partitioned table.
The target table cannot be an external table.
The target table cannot be a list bucketing table.

您可以单独创建表,然后插入或覆盖它。

vmdwslir

vmdwslir2#

自从最初提出和回答这个问题以来,已经有了一些发展。根据hive文档: Starting with Hive 3.2.0, CTAS statements can define a partitioning specification for the target table (HIVE-20241). 你也可以在这里看到相关的机票。早在2018年7月就已经解决了。
因此,如果你的Hive是3.2.0或更高,那么你可以简单地做

CREATE TABLE test_extract PARTITIONED BY (year string, month string) AS
SELECT 
    col1,
    col2, 
    year,
    month
FROM master_extract

相关问题