Hive from_unixtime for milliseconds

Sourabh Potnis picture Sourabh Potnis · Jan 14, 2015 · Viewed 58.2k times · Source

We have a timestamp epoch column (BIGINT) stored in Hive. We want to get Date 'yyyy-MM-dd' for this epoch. Problem is my epoch is in milliseconds e.g. 1409535303522. So select timestamp, from_unixtime(timestamp,'yyyy-MM-dd') gives wrong results for date as it expects epoch in seconds.

So i tried dividing it by 1000. But then it gets converted to Double and we can not apply function to it. Even CAST is not working when I try to Convert this double to Bigint.

Answer

Sourabh Potnis picture Sourabh Potnis · Jan 15, 2015

Solved it by following query:

select timestamp, from_unixtime(CAST(timestamp/1000 as BIGINT), 'yyyy-MM-dd') from Hadoop_V1_Main_text_archieved limit 10;