时间格式转换

日期格式→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

presto和hive日期函数对比的更多相关文章

  1. hive日期函数

    今天select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') UNIX时间戳转日期函数: from_unixtime 语法: from_ ...

  2. hive日期函数-原生函数(二)

    1. from_unixtime 日期函数UNIX时间戳转日期函数: from_unixtime 语法:from_unixtime(bigint unixtime[, stringformat]) 返 ...

  3. 【hive 日期函数】Hive常用日期函数整理

    1.to_date:日期时间转日期函数 select to_date('2015-04-02 13:34:12');输出:2015-04-02122.from_unixtime:转化unix时间戳到当 ...

  4. Hive日期函数总结(转学习使用)

    一.时间戳函数 1.获取当前时区的UNIX时间戳:select unix_timestamp(); 2.将指定时间转为UNIX时间戳: select unix_timestamp('2012-03-0 ...

  5. hive日期函数-Demo(二)

    需求:某资产近一个月的资产值 比如:今天是2018年2月28日,近一个月若是按照自然月来算,那么是2018年2月1日至2018年2月28日.最终需要的日期格式为:yyyyMMdd. 当日时间戳 uni ...

  6. hive日期函数-杂谈(一)

    来到广发返现由于历史遗留问题很多时间格式十分杂乱 我将总结一下时间日期的事情 1.hive原生时间函数的功能 2.一些基本业务时间范围的指标的sql案例 3.自定义udf函数让后来人更方便

  7. hive日期函数-广发实战(三)

    近一月客户新增常规里程数与额度比即上个月 第一天(包含)到上个月最后一天(包含) 字段是batch_date==>格式是 yyyymmdd ),'MM'),'-',''); +--------- ...

  8. hive函数总结-日期函数

    获取当前UNIX时间戳函数: unix_timestamp语法: unix_timestamp() 返回值: bigint说明: 获得当前时区的UNIX时间戳举例: hive> select u ...

  9. Hive中日期函数总结

    --Hive中日期函数总结: --1.时间戳函数 --日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数 select unix_timestamp(); --获得当前时区 ...

随机推荐

  1. Java 将PDF/XPS转为Word/html /SVG/PS/PCL/PNG、PDF和XPS互转(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了接口PdfConvertApi通过convert()方法将PDF文档以及XPS文档转为指定文档格式,如转PDF为Word(支持Docx.Doc). ...

  2. 【Spring】@Transactional 闲聊

    菜瓜:上次的AOP理论知识看完收获挺多的,虽然有一个自定义注解的demo,但还是觉得差点东西 水稻:我也觉得没有跟一遍源码还是差点意思,这次结合@Transactional注解深入源码看一下 菜瓜:事 ...

  3. 【转载】npx 真香

    npx 主要提供了一些便捷操作: 调用项目安装的模块 避免全局安装模块 使用不同版本的 node 执行 GitHub 源码 原文地址:http://www.ruanyifeng.com/blog/20 ...

  4. Git报错信息

    1. 解决办法: 当在最后提交的时候,出现的错误. 解决办法: git remote rm origin 执行下面代码: git remote add origin https://github.co ...

  5. SpringBoot--集成actuator

    actuator是spring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api 请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节 ...

  6. python文件处理-将图像根据坐标画矩形标记

    内容涉及:文件遍历,选取csv后缀文件,用cv操作图片 import csv import os import sys import numpy as np import copy import sh ...

  7. Python实用笔记 (18)面向对象编程——类和实例

    类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各 ...

  8. jQurey Select2 4.0

    https://jeesite.gitee.io/front/jquery-select2/4.0/index.htm

  9. 'printf' Function

    printf is not part of the C language; there is no input or output defined in C itself. printf is jus ...

  10. python基础知识-1

    1.python是静态的还是动态的?是强类型还弱类型? python是强类型的动态脚本语言: 强类型:不允许不同类型相加 动态:不使用显示类型声明,且确定一个变量的类型是在第一次给它赋值的时候 脚本语 ...