Adding a default value to a column while creating table in hive

心不动则不痛 提交于 2019-12-19 09:23:11

问题


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?


回答1:


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)



回答2:


I've been looking for a solution for this too and came up with this:

CREATE TABLE test_table AS SELECT
 CASE 
  WHEN TRUE
  THEN "desired_value" 
 END AS default_column_name;


来源:https://stackoverflow.com/questions/30033524/adding-a-default-value-to-a-column-while-creating-table-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!