简体   繁体   English

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

[英]create table in hive with additional columns

I am new to Hive . 我是Hive的新手。 I want to create the table in hive with the same columns as that of existing table plus some additional columns. 我想用与现有表相同的列以及一些其他列在配置单元中创建表。 I Know we can use something like this. 我知道我们可以使用这样的东西。

CREATE TABLE new_table_name
AS
SELECT *
FROM old_table_name

This will create the table with same columns as that of old_table_name. 这将创建具有与old_table_name相同列的表。

But How do I specify additional columns in new_table_name ? 但是,如何在new_table_name中指定其他列?

Here is how you can achieve it: 这是您可以实现的方法:

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

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

first create a new table like the first one and after that alter this new table and add the columns that you want. 首先,像第一个表一样创建一个新表,然后更改该新表并添加所需的列。

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

if you wish to avoid data copy, make your table external 如果您希望避免数据复制,请将表放在外部

I wish that it helps you :) 希望对您有所帮助:)

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

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