繁体   English   中英

在带有附加列的配置单元中创建表

[英]create table in hive with additional columns

我是Hive的新手。 我想用与现有表相同的列以及一些其他列在配置单元中创建表。 我知道我们可以使用这样的东西。

CREATE TABLE new_table_name
AS
SELECT *
FROM old_table_name

这将创建具有与old_table_name相同列的表。

但是,如何在new_table_name中指定其他列?

这是您可以实现的方法:

旧表:

hive> describe departments;
OK
department_id           int                     from deserializer   
department_name         string                  from deserializer   

创建表:

create table ctas as 
select department_id, department_name, 
cast(null as int) as col_null 
from departments;

显示新表的结构:

hive> describe ctas;
OK
department_id           int                                         
department_name         string                                      
col_null                int                                         
Time taken: 0.106 seconds, Fetched: 3 row(s)

新表的结果:

hive> select * from ctas;
OK
2       Fitness         NULL
3       Footwear        NULL
4       Apparel         NULL
5       Golf            NULL
6       Outdoors        NULL
7       Fan Shop        NULL
8       TESTING         NULL
8000    TESTING         NULL
9000    testing export  NULL

简单的方法是发出ALTER TABLE命令以在上述CREATE语句之后添加更多(附加)列。

首先,像第一个表一样创建一个新表,然后更改该新表并添加所需的列。

CREATE TABLE new_table LIKE old_table;
ALTER TABLE new_table ADD COLUMNS (newCol1 int,newCol2 int);

如果您希望避免数据复制,请将表放在外部

希望对您有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM