Reputation: 241
Need to add extra date column in my existing hive table so that it picks up the current date from the system for that day
hive (hivejobs)>
select * from my_current_Table;
OK
name age
Aditya 2
Aditya 7
I want to add a date column here so that i can get the current system date for that day as soon as I add a column. I think the work around would be to do a join of two tables with other table with the current system date.
below is my code and thought process.
alter table my_current_Table add columns( todays_date current_date());
this gives me an error , I am unable to find the actual way to do it
please help.
expected output
hive (hivejobs)>
select * from my_current_Table;
OK
name age todays_date
Aditya 2 2019-02-08 13:21:50
Aditya 7 2019-02-08 13:21:50
Upvotes: 2
Views: 6594
Reputation: 1525
Below are the steps for same,
create table my_current_Table_temp(name string, age int);
name|age
Aditya | 2
Aditya | 7
create table my_current_Table(name string, age tinyint, todays_date string);
insert overwrite table my_current_Table select name, age, FROM_UNIXTIME( UNIX_TIMESTAMP(), 'dd/MM/YYYY HH:mm') from my_current_Table_temp;
Upvotes: 1
Reputation: 38335
You cannot define calculated column in Hive table.
If you want always query today's date, use current_timestamp
function in the select:
select t.*,
date_format(current_timestamp,'yyyyMMdd HH:mm:ss') as todays_date
from my_current_Table t;
If you want to add column and store the date when the record was inserted, add column to the table and reload from itself, for example like this:
alter table my_current_Table add columns(load_date string);
insert overwrite table my_current_Table
select t.*,
date_format(current_timestamp,'yyyyMMdd HH:mm:ss') as load_date
from my_current_Table t;
Upvotes: 3