如何在Hive中打印毫秒而不忽略零

问题描述

我正在尝试打印hive> select current_timestamp(); OK 2020-09-22 12:00:26.658 ,最后精确到3毫秒。

这是我通常得到的。

hive> select current_timestamp();
OK
2020-09-22 12:00:25.5
Time taken: 0.065 seconds,Fetched: 1 row(s)

hive> select cast(current_timestamp() as timestamp);
OK
2020-09-22 12:00:00.09
Time taken: 0.084 seconds,Fetched: 1 row(s)

hive> select current_timestamp() as string;
OK
2020-09-22 11:07:12.27
Time taken: 0.076 seconds,Fetched: 1 row(s)

但是在极端情况下我也会得到

hive> select current_timestamp();
OK
2020-09-22 12:00:25.500
Time taken: 0.065 seconds,Fetched: 1 row(s)

hive> select cast(current_timestamp() as timestamp);
OK
2020-09-22 12:00:00.090
Time taken: 0.084 seconds,Fetched: 1 row(s)

hive> select current_timestamp();
OK
2020-09-22 11:07:12.270
Time taken: 0.076 seconds,Fetched: 1 row(s)

我期望的是不要像最后那样忽略0:

hive> select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:MM:ss.S');
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
2020-09-22 11:09:30.0
Time taken: 0.064 seconds,Fetched: 1 row(s)

我尝试过的事情:

{{1}}

我还尝试将current_timestamp()转换为字符串,这样它就不会忽略0,但这也不起作用

解决方法

尝试rpad(string str,int len,string pad)

Doc

返回str,右用pad填充长度为len。如果str大于len,则返回值缩短为len个字符。如果填充字符串为空,则返回值为空。

,

如果您使用date_format()可以使用吗?

select date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss.SSS')