How to create a UDF in HIVE using python for a Timestamp transformation

272 Views Asked by At

I have a table which has the timestamp as below

DataFeedDTTM
------------
2016-10-20 15:57:42.1

But when I query it I need the format as below.

select DataFeedDTTM from atable;

(Data can be huge and hence need a optimal solution as well, but then I'm fine to have any inputs)

DataFeedDTTM
------------
20170209 12:14:54.100000

I have an alternative to do a Case When - Type casting. But then it doesn't seem to be simple.

The reason for it to be simple is, I have to dynamically for this query through a Python code and read the data from Hive and write it into a CSV.

1

There are 1 best solutions below

0
On

'datetime' module might help your task.

from datetime import datetime
d_in = "2016-10-20 15:57:42.1"
d = datetime.strptime("2016-10-20 15:57:42.1", "%Y-%m-%d %H:%M:%S.%f")
d_out = d.strftime("%Y%m%d %H:%M:%S.%f")
print(d_out)

This produces:

'20161020 15:57:42.100000'