presto和hive日期函数对比
时间格式转换
日期格式→Unix时间戳
转10位Unix时间戳
数据:2020-07-23 15:01:13
Presto:select to_unixtime(cast('2020-07-23 15:01:13' as timestamp))
Hive:select unix_timestamp(cast('2020-07-23 15:01:13' as timestamp))
转13位Unix时间戳
数据:2020-07-23 15:01:13.343
Presto:select to_unixtime(cast('2020-07-23 15:01:13.343' as timestamp))*1000
Hive:select 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
Presto:select format_datetime(from_unixtime(1595487673),'yyyy-MM-dd HH:mm:ss')
Hive:select from_unixtime(1595487673,'yyyy-MM-dd HH:mm:ss')
13位Unix时间戳(如果不要毫秒就把concat和ss后面的.去掉)
数据:1595487673343
Presto:select concat(format_datetime(from_unixtime(1595487673343/1000),'yyyy-MM-dd HH:mm:ss.'), cast(1595487673343%1000 as varchar))
Hive:select 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
Presto:select date_diff('day', cast('2020-07-23 15:01:13' as timestamp), cast('2020-07-24 11:42:58' as timestamp))
Hive:select 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
Presto:select date_add('day', 1, cast('2020-07-24 11:42:58' as timestamp))
Hive:select 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日期函数对比的更多相关文章
- hive日期函数
今天select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') UNIX时间戳转日期函数: from_unixtime 语法: from_ ...
- hive日期函数-原生函数(二)
1. from_unixtime 日期函数UNIX时间戳转日期函数: from_unixtime 语法:from_unixtime(bigint unixtime[, stringformat]) 返 ...
- 【hive 日期函数】Hive常用日期函数整理
1.to_date:日期时间转日期函数 select to_date('2015-04-02 13:34:12');输出:2015-04-02122.from_unixtime:转化unix时间戳到当 ...
- Hive日期函数总结(转学习使用)
一.时间戳函数 1.获取当前时区的UNIX时间戳:select unix_timestamp(); 2.将指定时间转为UNIX时间戳: select unix_timestamp('2012-03-0 ...
- hive日期函数-Demo(二)
需求:某资产近一个月的资产值 比如:今天是2018年2月28日,近一个月若是按照自然月来算,那么是2018年2月1日至2018年2月28日.最终需要的日期格式为:yyyyMMdd. 当日时间戳 uni ...
- hive日期函数-杂谈(一)
来到广发返现由于历史遗留问题很多时间格式十分杂乱 我将总结一下时间日期的事情 1.hive原生时间函数的功能 2.一些基本业务时间范围的指标的sql案例 3.自定义udf函数让后来人更方便
- hive日期函数-广发实战(三)
近一月客户新增常规里程数与额度比即上个月 第一天(包含)到上个月最后一天(包含) 字段是batch_date==>格式是 yyyymmdd ),'MM'),'-',''); +--------- ...
- hive函数总结-日期函数
获取当前UNIX时间戳函数: unix_timestamp语法: unix_timestamp() 返回值: bigint说明: 获得当前时区的UNIX时间戳举例: hive> select u ...
- Hive中日期函数总结
--Hive中日期函数总结: --1.时间戳函数 --日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数 select unix_timestamp(); --获得当前时区 ...
随机推荐
- el-table表头与内容右边框错位问题
在用element写一个后台管理系统时遇到一个坑,el-table标签渲染出数据后表头th和内容会有错位,之前没有的 修改大小都没用 于是看百度搜索一波最终解决修改之前 虽然不大 但是别扭 在全局添加 ...
- 计算机网络之DNS协议
DNS( Domain Name System)是“域名系统”的英文缩写,是一种组织成域层次结构的计算机和网络服务命名系统,它用于TCP/IP网络,它所提供的服务是用来将主机名和域名转换为IP地址的工 ...
- py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe. : java.lang.IllegalArgumentException: Unsupported class file major version 55
今天小编用Python编写Spark程序报了如下异常: py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apach ...
- 隐藏input的三种方法和区别
一.<input type="hidden" />二.<input type="text" style="display:none& ...
- CentOS 的数字命令级别
1 user commands 2 system calls 3 library functions 4 special files 5 file formats 6 ...
- C语言学习笔记——特别篇(VScode安装使用)
B站有同步教学视频 参考博文: https://www.cnblogs.com/czlhxm/p/11794743.html 注意事项: 请在英文目录下运行!!! VScode下载链接: https: ...
- 理解css中min-width和max-width,width与它们之间的区别联系
css中,min-width是用来限制元素的最小宽度,max-width用来限制元素的最大宽度,也就是说当元素的width大于max-width,或者小于min-width.就被它们的值所代替,尤其适 ...
- 浏览器缓存_HTTP强缓存和协商缓存
浏览器缓存 浏览器缓存是浏览器在本地磁盘对用户最近请求过的文档进行存储,当访问者再次访问同一页面时,浏览器就可以直接从本地磁盘加载文档. 所以根据上面的特点,浏览器缓存有下面的优点: 减少冗余的数据传 ...
- LeetCode 哈希表 380. 常数时间插入、删除和获取随机元素(设计数据结构 List HashMap底层 时间复杂度)
比起之前那些问计数哈希表的题目,这道题好像更接近哈希表的底层机制. java中hashmap的实现是通过List<Node>,即链表的list,如果链表过长则换为红黑树,如果容量不足(装填 ...
- React学习路径快速进入AntDesignPro开发
好久没有写博客,有空再来记一下.最近在整些小东西,需要用到前端,最开始本着对nodejs的动不动几百兆插件的恐惧, 于是使用自己以前写的 OSS.Pjax 小框架(类似国外的Pjax,利用pushSt ...