【问题标题】:Calculate a date in behind 24 hours Hive计算 24 小时后的日期 Hive
【发布时间】:2018-12-27 23:25:44
【问题描述】:

我的需求实在是太傻了,所以基本上我需要在Hive的一个时间戳列中回溯24小时。

到目前为止,我已经尝试了两种不同的方法,但都没有成功:

 select 
 recordDate, --original date
 cast(date_sub(cast(recorddate as timestamp),1) as timestamp),  -- going one day behind without hour
cast((cast(cast(recorddate as timestamp) AS bigint)-1*3600) as timestamp)  -- crazy year
 from mtmbuckets.servpro_agents_events limit 10;

我的输出看起来:

感谢您给予我的支持。

谢谢

【问题讨论】:

    标签: datetime bigdata hiveql hue


    【解决方案1】:

    hive 中没有直接的功能。

    1 创建 UDF 以执行此操作。

    以秒为单位转换日期,然后计算(-24 *60*60)秒,然后将 int 改回数据。

    使用 from_unixtime 和 unix_timestamp 来实现下面的代码。

     select from_unixtime(unix_timestamp(recorddate) - 86400)
     from mtmbuckets.servpro_agen ts_events limit 10;;
    

    From_unixtime 将具有给定模式的时间字符串转换为 Unix 时间戳(以秒为单位) 此函数的结果以秒为单位。

    Unix_timestamp

    将格式为 yyyy-MM-dd HH:mm:ss 的时间字符串转换为 Unix 时间戳(以秒为单位),使用默认时区和默认语言环境,如果失败则返回 0: unix_timestamp('2009-03-20 11: 30:01') = 1237573801

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      相关资源
      最近更新 更多