如何在蜂巢中验证和匹配不同的日期格式 [英] how to verify and match different date formats in hive

查看:71
本文介绍了如何在蜂巢中验证和匹配不同的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的配置单元表中有以下日期:

I have below dates in my hive table :

Jan 2014
Oct-13
8-Nov
8-Oct
30-Nov-11

我需要将它们转换为'yyyy-MM-dd'格式.我已经使用 from_unixtime(unix_timestamp(change_log_date,'yyyyMMdd'),'yyyy-MM-dd')来隐蔽日期格式,这种格式在11年11月30日之前可以正常使用,但是由于我使用了不同的日期格式在数据中,因此如何编写通用代码来检查日期格式并将其转换为"yyyy-MM-dd".如果天/月/年不存在,我需要输入0.

I need to convert them in the 'yyyy-MM-dd' format. I have used from_unixtime(unix_timestamp(change_log_date ,'yyyyMMdd'), 'yyyy-MM-dd') to covert date format which is working fine for 30-Nov-11 however since I have different date formats in the data so how to write generic code which will check date format and convert it into 'yyyy-MM-dd'. I need to put 0 for day/month/year if its not present.

for eg. I need to convert 8-Oct into '0000-10-08'

需要帮助

推荐答案

如果您知道所有可能的日期格式,则可以使用case语句为每行应用正确的格式:

If you know all possible date format, you can use case statement to apply the correct format for each line:

CASE WHEN change_log_date rlike '[0-9]{2}-[a-zA-Z]{3}-[0-9]{2}' 
     THEN from_unixtime(unix_timestamp(change_log_date ,'yyyyMMdd'), 'yyyy-MM-dd')
     WHEN change_log_date rlike --other regex 
     THEN -- and so on

如果您更熟悉Java或python,则可以轻松编写代码并将其用作

If you are more familiar with java or python, you can easily write your piece of code and use it as Hive function.

这篇关于如何在蜂巢中验证和匹配不同的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆