简体   繁体   English

如何将不同格式的日期时间戳转换成hive表中的时间戳格式

[英]How to convert different formats of date timestamp to the format of timestamp in hive table

I have a list of different formats of timestamp.我有一个不同格式的时间戳列表。 How to change its for to the format accepted in hive tables.如何将其更改为 hive 表中接受的格式。 For eg.例如。 20210811:12:55:56.563 to 2021-08-11 12:55:56.563 25/05/1999 02:35:05.532 to 1999-05-25 02:35:05.532. 20210811:12:55:56.563 至 2021-08-11 12:55:56.563 25/05/1999 02:35:05.532 至 1999-05-25 02:35:05.532。 How to do it in python. I have around 7-8 different formats.如何在 python 中进行操作。我有大约 7-8 种不同的格式。

Does anyone have any ideas or approach around it.有没有人对此有任何想法或方法。 Your ideas are most welcome.非常欢迎您的想法。

You can use below py function to check the format.您可以使用下面的 py function 来检查格式。 If below functions returns not none values, then its in expected format else no.如果下面的函数不返回任何值,那么它是预期的格式,否则没有。 Return value will be a date time in yyyy-MM-dd HH:MI:SS.SSSSS format.返回值将是yyyy-MM-dd HH:MI:SS.SSSSS格式的日期时间。 You can easily insert this into hive date time field.您可以轻松地将其插入 hive 日期时间字段。

import datetime

#formats to be checked
fmts=['%d/%m/%Y %H:%M:%S.%f','%Y%m%d %H:%M:%S.%f','%d-%m-%Y %H:%M:%S.%f']

#func to check the formats and return proper date time if its in correct format.
def try_strptime(s, fmts=fmts):
    for fmt in fmts:
        try:
            return datetime.strptime(s, fmt)
        except:
            continue

    return None 
    

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

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