Convert String to Timestamp in Hive HQL

十年热恋 提交于 2019-12-12 17:25:32

问题


I'm having a string like "08/03/2018 02:00:00" which i'm trying to convert into a timestamp value.

I'm using the below code:

unix_timestamp("08/03/2018 02:00:00", "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")

when i use the above code it's throwing a NULL value.

How can i convert this string to Timestamp in Hive/Hue Editor?


回答1:


The format you specified does not match to the actual timestamp. If 08/03 in your example is dd/MM then:

select unix_timestamp("08/03/2018 02:00:00", "dd/MM/yyyy HH:mm:ss")
OK
1520503200
Time taken: 0.299 seconds, Fetched: 1 row(s)



select from_unixtime(unix_timestamp("08/03/2018 02:00:00", "dd/MM/yyyy HH:mm:ss"))
OK
2018-03-08 02:00:00
Time taken: 0.068 seconds, Fetched: 1 row(s)

See this answer if you want convert from ISO timestamp https://stackoverflow.com/a/23520257/2700344

You can specify date pattern for unix_timestamp for non-standard format. See docs here: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions



来源:https://stackoverflow.com/questions/49814880/convert-string-to-timestamp-in-hive-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!