apache配置单元-时间戳查询

8fsztsew  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(272)

我在一个配置单元数据库中有两个时间戳列,以以下格式存储时间戳:

hive> select last_date from xyz limit 2;  
OK
2019-08-21 15:11:23.553
2019-08-21 15:11:23.553

[上面默认存储毫秒]

hive> select last_modify_date from xyz limit 2;
OK
2018-04-18 23:32:58
2017-09-22 04:02:32

我需要一个公共的hiveselect查询,它将上述两个时间戳转换为'yyyy-mm-dd hh:mm:ss.sss'格式,如果存在则保留毫秒值,如果不存在则附加'.000'。
到目前为止,我尝试了:

select 
    last_modify_date, 
    from_unixtime(unix_timestamp(last_modify_date), "yyyy-MM-dd HH:mm:ss.SSS") as ts 
from xyz limit 3;

但是,上面的查询为上述两个时间戳列显示“.000”。
请帮忙

uqdfh47h

uqdfh47h1#

从实现 unix_timestamp ,您可以看到返回值以senconds为单位,由 LongWritable . 任何小于1秒的都被四舍五入。
您可以编写自己的自定义项,也可以使用纯sql来实现这一点。
其中一个简单的方法是使用genericudfrpad rpad :

select rpad(your_date, 23, '.000') from your_table;

一些例子:

hive> select rpad('2018-04-18 23:32:58', 23, '.000');
    OK
    2018-04-18 23:32:58.000
    hive> select rpad('2018-04-18 23:32:58.553', 23, '.000');
    OK
    2018-04-18 23:32:58.553

相关问题