【问题标题】:Timestamp operations in hiveHive 中的时间戳操作
【发布时间】:2016-10-18 08:32:50
【问题描述】:

如何在 hive 中减去 2 个时间戳列并将结果以等效小时格式存储在单独的列中?

【问题讨论】:

  • 我基本上有 2 列:start_dateend_date 在 hive 的表中,我想填充一个名为 elapsed_hours 通过运行 DML

标签: sql hadoop hive impala


【解决方案1】:

假设您有给定格式的时间戳:2016-10-16 10:51:00.000

您可以尝试以下操作:

SELECT
  cast(
    round(
      cast((e-s) as double) * 1000
    ) as int
  ) time_difference
FROM (SELECT cast(starttime as double) s, cast(endtime as double) e from table1) q;

它将以毫秒为单位为您提供两个时间戳的差异。然后您可以将其转换为您预期的格式(小时、天等)。

【讨论】:

  • 它给了我一个错误说 Missing Keyword 在演员功能之后
  • 希望您根据架构重命名列。你能告诉你得到的整个错误吗?
  • 反其道而行之呢?
    将填充了小时的列添加到时间戳列并在另一个时间戳列中获取输出?
【解决方案2】:

使用 unix_timestamp 函数将 hive 时间戳转换为自纪元以来的秒数。减去 unix timestemp 结果可以在几秒钟内得到答案。

    SELECT start_dttm,
             (unix_timestamp(end_dttm) - unix_timestamp(start_dttm))/60 AS duration_in_minutes
    FROM dev_edw.audit_history_hb
    WHERE script_name LIKE '%0_hive_d%'
            AND parent_audit_id is null
    ORDER BY start_dttm desc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2021-04-17
    • 1970-01-01
    相关资源
    最近更新 更多