Hive - is it possible to create a column from another column

I was wondering if it is possible to create a new column from an existing column in the bush.

Say I have a table People (name string, age int)and I want to add a column is_old stringthat will be defined as if(age > 70, 'old', 'not_old'). Is there any way to do this?

The only way I can imagine now is to create a new table People_tempfrom the old one, delete the old table and rename the new table as follows:

create table People_new as select name, age, if(age > 70, 'old', 'not_old') as is_old from People;
drop table People;
alter table People_new rename People;

** Is there a way to do this without creating a temporary table?
(for example, the oracle has the idea of ​​computed columns.)

+4
source share
2 answers

, TEMP TABLE. , :

ALTER TABLE People ADD COLUMNS (is_old string);

INSERT OVERWRITE TABLE People 
SELECT name, age, if(age > 70, 'old', 'not_old') as is_old
  FROM People ; 
+2

, .

CASE WHEN:

,CASE WHEN age > 70 THEN ''old' ELSE 'not_old' END AS is_old string

IF:

,IF(age > 70, 'old', 'not_old') AS is_old string
+2

All Articles