Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To get the weekday from TIMESTAMP in hive

I need to calculate mean sales for sunday. Values for the column salesdate(timestamp) are:

2012-01-01 09:00:00
2012-01-01 09:00:00
2012-01-01 09:00:00
...........

I have extracted the date part using to_date().Now how to get weekday(like sunday) from this date in hive? Please guide.

like image 881
ashwini Avatar asked Dec 26 '22 09:12

ashwini


2 Answers

You can use a combination of unix_timestamp, and from_unixtime UDFs.

from_unixtime(unix_timestamp(col), 'EEEE')

If you check the documentation for SimpleDateFormat, which from_unixtime uses, you can see that "EEEE" is the code for the full name of the day of the week. "EEE" gets you the abbreviated version, i.e. "Sun" or "Mon".

like image 72
Joe K Avatar answered Jan 05 '23 19:01

Joe K


There is no OOTB feature to achieve this as of now. A ticket is open though.

You need to write a UDF for this. Or, you could also try the patch available with the above mentioned ticket.

HTH

like image 24
Tariq Avatar answered Jan 05 '23 17:01

Tariq