简体   繁体   English

如何使用Python进行Timestamp转换在HIVE中创建UDF

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

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. 我有一个替代方法来做一个Case when-类型转换。 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. 原因很简单,我必须通过Python代码动态地进行此查询,并从Hive中读取数据并将其写入CSV。

'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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM