如何更改配置单元中的日期格式? [英] How to change date format in hive?
问题描述
我在 hive 中的表的日期格式为2016/06/01".但我发现它与2016-06-01"的格式不协调.例如,他们无法比较.它们都是 string .所以我想知道如何使它们和谐并可以比较它们.或者另一方面,如何将2016/06/01"更改为2016-06-01"以便它们进行比较.
My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'. They can not compare for instance. Both of them are string . So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
非常感谢.
推荐答案
要将日期字符串从一种格式转换为另一种格式,您必须使用 hive 的两个日期函数
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern)
转换时间字符串使用给定模式到 unix 时间戳(以秒为单位),如果返回 0失败.from_unixtime(bigint unixtime[, string format])
转换从 unix epoch (1970-01-01 00:00:00 UTC) 到 a 的秒数表示当前时刻的时间戳的字符串系统时区.
unix_timestamp(string date, string pattern)
convert time string with given pattern to unix time stamp (in seconds), return 0 if fail.from_unixtime(bigint unixtime[, string format])
converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone.
使用以上两个功能可以达到您想要的效果.
Using above two function you can achieve your desired result.
示例输入和输出可以从下图中看到:
The sample input and output can be seen from below image:
最终查询是
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
其中 table1 是我的 hive 数据库中的表名.
where table1 is the table name present in my hive database.
希望对你有帮助!!!
这篇关于如何更改配置单元中的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!