【发布时间】:2016-10-18 08:32:50
【问题描述】:
如何在 hive 中减去 2 个时间戳列并将结果以等效小时格式存储在单独的列中?
【问题讨论】:
-
我基本上有 2 列:start_date 和 end_date 在 hive 的表中,我想填充一个名为 elapsed_hours 通过运行 DML
如何在 hive 中减去 2 个时间戳列并将结果以等效小时格式存储在单独的列中?
【问题讨论】:
假设您有给定格式的时间戳: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;
它将以毫秒为单位为您提供两个时间戳的差异。然后您可以将其转换为您预期的格式(小时、天等)。
【讨论】:
使用 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
【讨论】: