• presto和hive日期函数对比


    时间格式转换

    日期格式→Unix时间戳

    转10位Unix时间戳

    数据:2020-07-23 15:01:13

    Prestoselect to_unixtime(cast('2020-07-23 15:01:13' as timestamp))

    Hiveselect unix_timestamp(cast('2020-07-23 15:01:13' as timestamp))

    转13位Unix时间戳

    数据:2020-07-23 15:01:13.343

    Prestoselect to_unixtime(cast('2020-07-23 15:01:13.343' as timestamp))*1000

    Hiveselect unix_timestamp(cast(substr('2020-07-23 15:01:13.343', 1, 19) as timestamp)) * 1000 + cast(substr('2020-07-23 15:01:13.343', 21) as bigint)

    Unix时间戳→日期格式

    10位Unix时间戳

    数据:1595487673

    Prestoselect format_datetime(from_unixtime(1595487673),'yyyy-MM-dd HH:mm:ss')

    Hiveselect from_unixtime(1595487673,'yyyy-MM-dd HH:mm:ss')

    13位Unix时间戳(如果不要毫秒就把concat和ss后面的.去掉)

    数据:1595487673343

    Prestoselect concat(format_datetime(from_unixtime(1595487673343/1000),'yyyy-MM-dd HH:mm:ss.'), cast(1595487673343%1000 as varchar))

    Hiveselect concat(from_unixtime(cast(1595487673343/1000 as int),'yyyy-MM-dd HH:mm:ss.'), cast(1595487673343%1000 as string))

    时间计算

    时间间隔

    数据:2020-07-24 11:42:58 - 2020-07-23 15:01:13

    Prestoselect date_diff('day', cast('2020-07-23 15:01:13' as timestamp), cast('2020-07-24 11:42:58' as timestamp))

    Hiveselect datediff('2020-07-24 11:42:58','2020-07-23 15:01:13');

    这个数据,因为相差的时间小于24小时,Presto输出的是0,而Hive是1,这个坑要注意一下。还有要注意的就是Presto是时间大的放后面,而Hive是时间大的放前面。

    时间相加

    数据:2020-07-24 11:42:58 + 1

    Prestoselect date_add('day', 1, cast('2020-07-24 11:42:58' as timestamp))

    Hiveselect date_add('2020-07-24 11:42:58', 1)

    如果要计算相差的其他时间单位,Presto是修改前面的时间单元即可,可选有如下几个:

    Unit Description
    millisecond Milliseconds
    second Seconds
    minute Minutes
    hour Hours
    day Days
    week Weeks
    month Months
    quarter Quarters of a year
    year Years

    Hive是通过对应的时间单元函数获取到时间单元后在进行计算,例如上面的例子2020-07-24 11:42:58 - 2020-07-23 15:01:13,我要计算他们的小时差,那么我可以这么写:

    select hour('2020-07-24 11:42:58') - hour('2020-07-23 15:01:13') + datediff('2020-07-24 11:42:58','2020-07-23 15:01:13')*24
    
  • 相关阅读:
    hibernate中获得session的方式
    html中meta的介绍
    dwr和spring的整合
    hibernate.hbm2ddl.auto配置详解
    java生成二维码(需导入第三方ZXing.jar包)
    公共语言运行库(CLR)开发系列课程(1):Pinvoke 简介 学习笔记
    SQL case when 遇到null值
    OpenLayers 3 之 地图图层数据来源(ol.source)详解
    公共语言运行库(CLR)开发系列课程(3):COM Interop基础 学习笔记
    公共语言运行库(CLR)开发系列课程(2):Pinvoke 进阶 学习笔记
  • 原文地址:https://www.cnblogs.com/harrylyx/p/13371792.html
Copyright © 2020-2023  润新知