Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive from_unixtime for milliseconds

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.

like image 902
Sourabh Potnis Avatar asked Jan 14 '15 12:01

Sourabh Potnis


People also ask

What is From_unixtime in Hive?

Hive from_unixtime() is used to get Date and Timestamp in a default format yyyy-MM-dd HH:mm:ss from Unix epoch seconds. Specify the second argument in pattern format to return date and timestamp in a custom format. Syntax – from_unixtime(bigint unixtime[, string format])

How does Hive store timestamp?

The hive timestamp format is YEAR-Month-Day-Hours-Minutes-seconds [YYYY-MM-DD HH:MM:SS] with an optional fraction of seconds. Anything else that will come with the above format should be read as a string and converted later.

How do I query a timestamp column in Hive?

You need to change the data type of your filter like Time-Stamp to String comparison can be the issue. Try using from_utc_timestamp('2017-01-01 22:30:57.375117') or from_unix() commands.

Is timestamp a datatype in Hive?

ii) Date/Time Data TypeHive provides Timestamp and Date data types to UNIX timestamp format. TIMESTAMP- It uses nanosecond precision and is denoted by yyyy-mm-dd hh:mm: ss format.


4 Answers

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;
like image 71
Sourabh Potnis Avatar answered Oct 10 '22 15:10

Sourabh Potnis


In the original answer you'll get string, but if you'd like to get date you need to call extra cast with date:

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

Docs for casting dates and timestamps. For converting string to date:

cast(string as date)
If the string is in the form 'YYYY-MM-DD', then a date value corresponding to that year/month/day is returned. If the string value does not match this formate, then NULL is returned.

Date type is available only from Hive > 0.12.0 as mentioned here:

DATE (Note: Only available starting with Hive 0.12.0)

like image 44
Anton Protopopov Avatar answered Oct 10 '22 13:10

Anton Protopopov


The type should be double to ensure precision is not lost:

select from_unixtime(cast(1601256179170 as double)/1000.0, "yyyy-MM-dd hh:mm:ss.SSS") as event_timestamp
like image 42
NicolasLi Avatar answered Oct 10 '22 14:10

NicolasLi


timestamp_ms is unixtime in milliseconds

SELECT from_unixtime(floor(CAST(timestamp_ms AS BIGINT)/1000), 'yyyy-MM-dd HH:mm:ss.SSS') as created_timestamp FROM table_name;

like image 44
anjaneyulu yarrapothu Avatar answered Oct 10 '22 13:10

anjaneyulu yarrapothu