在配置单元中创建表格时向列添加默认值 [英] Adding a default value to a column while creating table in hive

查看:111
本文介绍了在配置单元中创建表格时向列添加默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从外部文件中的数据创建配置单元表。现在我希望从上表中的数据创建另一个表,并添加具有缺省值的列。



我知道可以使用CREATE TABLE AS SELECT,但是如何添加额外的有缺省值的列?

解决方案

您可以指定从create / update表中选择哪些列。只需将默认值作为其中一列提供即可。使用UPDATE的示例如下:

创建简单表并使用值填充它:

 蜂房>创建表table1(col1字符串); 
hive>插入表table1值('val1');
hive>从table1中选择col1;
OK
val1
所用时间:0.087秒,提取:1行

允许动态分区:

  hive> SET hive.exec.dynamic.partition.mode = nonstrict; 

创建第二张表:

 蜂房>创建表table2(col1 string,col2 string); 

使用默认值从表1填充它:

 分群>插入覆盖表table2,从table1中选择col1,'DEFAULT'; 
hive>从table2中选择*;
OK
val1 DEFAULT
所用时间:0.081秒,提取:1行


I'm able to create a hive table from data in external file. Now I wish to create another table from data in previous table with additional columns with default value.

I understand that CREATE TABLE AS SELECT can be used but how do I add additional columns with default value?

解决方案

You could specify which columns to select from table on create/update. Simply provide default value as one of columns. Example with UPDATE is below:

Creating simple table and populating it with value:

hive> create table table1(col1 string);
hive> insert into table table1 values('val1');
hive> select col1 from table1;
OK
val1
Time taken: 0.087 seconds, Fetched: 1 row(s)

Allowing dynamic partitions:

hive> SET hive.exec.dynamic.partition.mode=nonstrict;

Creating second table:

hive> create table table2(col1 string, col2 string);

Populating it from table1 with default value:

hive> insert overwrite table table2 select col1, 'DEFAULT' from table1;
hive> select * from table2;
OK
val1    DEFAULT
Time taken: 0.081 seconds, Fetched: 1 row(s)

这篇关于在配置单元中创建表格时向列添加默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆