Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Date String from UTC to Specific TimeZone in HIVE?

My Hive table has a date column with UTC date strings. I want to get all rows for a specific EST date.

I am trying to do something like the below:

Select * 
from TableName T
where TO_DATE(ConvertToESTTimeZone(T.date))  = "2014-01-12" 

I want to know if there is a function for ConvertToESTTimeZone, or how I can achieve that?

I tried the following but it doesnt work (my default timezone is CST):

TO_DATE(from_utc_timestamp(T.Date) = "2014-01-12" 
TO_DATE( from_utc_timestamp(to_utc_timestamp (unix_timestamp (T.date), 'CST'),'EST'))

Thanks in advance.

Update:

Strange behavior. When I do this:

select "2014-01-12T15:53:00.000Z", TO_DATE(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP("2014-01-12T15:53:00.000Z", "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"), 'EST')) 
from TABLE_NAME T1
limit 3

I get

    _c0                          _c1
0   2014-01-12T15:53:00.000Z    1970-01-16
1   2014-01-12T15:53:00.000Z    1970-01-16
2   2014-01-12T15:53:00.000Z    1970-01-16
like image 772
Gadam Avatar asked Feb 13 '15 20:02

Gadam


People also ask

How do you convert UTC to specific time zones?

The best way to do this is simply to use TimeZoneInfo. ConvertTimeFromUtc . // you said you had these already DateTime utc = new DateTime(2014, 6, 4, 12, 34, 0); TimeZoneInfo tzi = TimeZoneInfo. FindSystemTimeZoneById("Pacific Standard Time"); // it's a simple one-liner DateTime pacific = TimeZoneInfo.

How do I change timezone in hive?

Hive uses the default timezone of the JVM. Currently the only way to change the timezone used by Hive is to change the default timezone of the JVM.

How do I change the date format in hive?

select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1; where table1 is the table name present in my hive database. I hope this help you!!!

How do I convert a string to timestamp in hive?

2.3 to_date(string timestamp) – Converts Timestamp string to Date type. to_date() function takes timestamp as an input string in the default format yyyy-MM-dd HH:mm:ss and converts into Date type.


1 Answers

Your system timezone CST doesn't matter for converting UTC to EST in Hive. You should be able to get the correct results with:

TO_DATE(FROM_UTC_TIMESTAMP(UNIX_TIMESTAMP(T.date, "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'") * 1000, 'EST'))

Note that because UNIX_TIMESTAMP returns seconds, you will lose the millisecond component of your timestamp.

like image 200
Jeremy Beard Avatar answered Oct 16 '22 09:10

Jeremy Beard