how to add columns to existing hive external table?

8.3k views Asked by At
create external table demotable(
column1 string,
column2 string,
column3 string)
row format delimited fields terminated by '|' 
location '/data/demotable';

I create external table 'demotable' and the data in '/data/demotable' is like

aaa|bbb|ccc
ddd|eee|fff
www|ttt|uuu
...
yyy|uuu|kkk

Now I want to add two more columns in my data and it is going to be like

aaa|bbb|ccc
ddd|eee|fff
www|ttt|uuu
...
yyy|uuu|kkk|ppp|lll
vvv|mmm|zzz|ttt|hhh

Is there any way to :

1.add new columns in my table(for new data)

2.keep the old data(just mark the last two columns as 'NULL') ?

2

There are 2 answers

0
keyz57 On

Since it's an external table, you can just drop the table and recreate with additional columns placed at the end. Dropping external table, doesn't ideally remove the files. When you query the table, output will be NULL for those rows where those columns doesn't have any data.

0
Rajesh Singam On

You can new columns to HIVE external table using the below command:

ALTER TABLE demotable ADD columns ( column4 string, column5 string );