在具有附加列的配置单元中创建 table

create table in hive with additional columns

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

CREATE TABLE new_table_name
AS
SELECT *
FROM old_table_name

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

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

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

实现方法如下:

Old table:

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

Create table:

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

Displaying Structure of new table:

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

Results from new table:

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

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

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

如果您希望避免数据复制,请将您的table设为外部

希望对你有所帮助:)