【问题标题】:Converting only time to unixtimestamp in Hive在 Hive 中仅将时间转换为 unix 时间戳
【发布时间】:2018-12-02 13:56:21
【问题描述】:

我有一列 eventtime 仅将一天中的时间存储为字符串。例如: 0445AM - 表示04:45 AM。我正在使用以下查询转换为 UNIX 时间戳。

select unix_timestamp(eventtime,'hhmmaa'),eventtime from data_raw limit 10;

这似乎适用于测试数据。我一直认为 unixtimestamp 是日期和时间的组合,而在这里我只有时间。我的问题是在执行上述功能时它会考虑什么日期?时间戳似乎很小。

【问题讨论】:

    标签: sql hive timestamp hiveql unix-timestamp


    【解决方案1】:

    Unix 时间戳是从 Unix 纪元 (1970-01-01 00:00:00 UTC) 算起的 bigint 秒数。 unix 时间戳是一种以运行总秒数来跟踪时间的方法。

    select unix_timestamp('0445AM','hhmmaa') as unixtimestamp
    

    返回

    17100
    

    这正好是 4 小时 45 分钟转换为秒。

    select 4*60*60 + 45*60
    

    返回17100

    并将其转换回使用 from_unixtime 函数

    select from_unixtime (17100,'hhmmaa') 
    

    返回:

    0445AM
    

    如果您使用包含日期的格式进行转换,您会看到它假定日期为1970-01-01

    select from_unixtime (17100,'yyyy-MM-dd hhmmaa') 
    

    返回:

    1970-01-01 0445AM
    

    请参阅 Hive 函数 dosc here

    还有一个关于Unix timestamp的非常有用的网站

    【讨论】:

    • 谢谢。这是一个明确的解释。我是 Hive 的新手。从未想过将其转换回来提供“yyyy-MM-dd”选项。
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2013-04-07
    • 2020-07-29
    • 2010-10-25
    • 2020-10-19
    • 1970-01-01
    相关资源
    最近更新 更多