hive 配置单元将字符串转换为日期dd-MM-yyyy

c7rzv4ha  于 2022-11-05  发布在  Hive
关注(0)|答案(6)|浏览(176)

在Hive中,如何将格式为“dd-MM-yyyy”的字符串转换为同样格式为“dd-MM-yyyy”的日期类型?
大致如下:

CAST('12-03-2010' as date 'dd-mm-yyyy')
f8rj6qna

f8rj6qna1#

尝试:

from_unixtime(unix_timestamp('12-03-2010' , 'dd-MM-yyyy'))
pcww981p

pcww981p2#

如果我没有理解错的话,您正在尝试将一个表示给定日期的String转换为另一种类型。

注:(如@Samson Scharfrichter所述)

  • 日期的默认表示形式为ISO 8601
  • 日期以二进制存储(而不是字符串)

有几种方法可以做到这一点。你已经接近解决方案。我将使用CAST(它转换为DATE_TYPE):

SELECT cast('2018-06-05' as date);

结果:2018年6月5日日期类型
或(取决于您的模式)

select cast(to_date(from_unixtime(unix_timestamp('05-06-2018', 'dd-MM-yyyy'))) as date)

结果:2018年6月5日日期类型
如果您决定将ISO 8601转换为日期类型:

select cast(to_date(from_unixtime(unix_timestamp(regexp_replace('2018-06-05T08:02:59Z', 'T',' ')))) as date);

结果:2018年6月5日日期类型
Hive有它自己的函数,我写了一些例子来说明这些date-和cast-函数:

日期与时间戳函数示例:

将字符串/时间戳/日期转换为DATE

SELECT cast(date_format('2018-06-05 15:25:42.23','yyyy-MM-dd') as date); -- 2018-06-05 DATE_TYPE
SELECT cast(date_format(current_date(),'yyyy-MM-dd') as date); -- 2018-06-05 DATE_TYPE
SELECT cast(date_format(current_timestamp(),'yyyy-MM-dd') as date);  -- 2018-06-05 DATE_TYPE

将字符串/时间戳/日期转换为BIGINT_TYPE

SELECT to_unix_timestamp('2018/06/05 15:25:42.23','yyyy/MM/dd HH:mm:ss'); -- 1528205142 BIGINT_TYPE
SELECT to_unix_timestamp(current_date(),'yyyy/MM/dd HH:mm:ss'); -- 1528205000 BIGINT_TYPE
SELECT to_unix_timestamp(current_timestamp(),'yyyy/MM/dd HH:mm:ss'); -- 1528205142 BIGINT_TYPE

将字符串/时间戳/日期转换为STRING

SELECT date_format('2018-06-05 15:25:42.23','yyyy-MM-dd'); -- 2018-06-05 STRING_TYPE
SELECT date_format(current_timestamp(),'yyyy-MM-dd'); -- 2018-06-05 STRING_TYPE
SELECT date_format(current_date(),'yyyy-MM-dd'); -- 2018-06-05 STRING_TYPE

将BIGINT unixtime转换为STRING

SELECT to_date(from_unixtime(unixtime,'yyyy/MM/dd HH:mm:ss')); -- 2018-06-05 STRING_TYPE

将字符串转换为BIGINTunixtime

SELECT unix_timestamp('2018-06-05 15:25:42.23','yyyy-MM-dd') as TIMESTAMP; -- 1528149600 BIGINT_TYPE

将字符串转换为TIMESTAMP

SELECT cast(unix_timestamp('2018-06-05 15:25:42.23','yyyy-MM-dd') as TIMESTAMP); -- 1528149600 TIMESTAMP_TYPE

幂等(字符串-〉字符串)

SELECT from_unixtime(to_unix_timestamp('2018/06/05 15:25:42.23','yyyy/MM/dd HH:mm:ss')); -- 2018-06-05 15:25:42 STRING_TYPE

幂等(日期-〉日期)

SELECT cast(current_date() as date); -- 2018-06-26 DATE_TYPE

当前日期/时间戳

SELECT current_date(); -- 2018-06-26 DATE_TYPE
SELECT current_timestamp(); -- 2018-06-26 14:03:38.285 TIMESTAMP_TYPE
yrdbyhpb

yrdbyhpb3#

AFAIK您必须将 String 重新格式化为ISO格式,以便能够将其转换为 Date

cast(concat(substr(STR_DMY,7,4), '-',
            substr(STR_DMY,1,2), '-',
            substr(STR_DMY,4,2)
           )
     as date
     ) as DT

要将 Date 显示为具有特定格式的 String,则需要反过来,除非您有Hive 1.2+并且可以使用date_format()
=〉顺便问一下,您是否检查了文档?

oalqel3c

oalqel3c4#

假设您的表中有一个字符串格式的列'birth_day',您应该使用以下查询来使用birth_day进行过滤

date_Format(birth_day, 'yyyy-MM-dd')

您可以通过以下方式在查询中使用它

select * from yourtable
where 
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
js4nwp54

js4nwp545#

这将转换整个列:

select from_unixtime(unix_timestamp(transaction_date,'yyyyMMdd')) from table1
vcirk6k6

vcirk6k66#

更简单的方式选择演员(“07/15/2022”作为日期格式“MM/DD/YYYY”)从双下面是一个链接到wiki**https://cwiki.apache.org/confluence/display/Hive/CAST...FORMAT+with+SQL%3A2016+datetime+formats**

相关问题