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(); --获得当前时区 ...
随机推荐
- IntelliJ IDEA 2018.3.6 安装、激活 JRebel
在 IntelliJ IDEA 2018.3.6 中安装## JRebel 1.代开 IDEA 开发工具,然后用快捷键 Ctrl+Alt+S 打开设置并搜索 jrebel 插件 2.安装 jrebel ...
- C# @string $string $@string
@string 保证换行后也属于同一个字符串 (请特别注意\r\n这样也会直接输入,不在起到换行的效果) string execSql = @" SELECT T1.ProcInstID ...
- CSS背景颜色透明
{ filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0.5; opacity: 0.5; } 兼容大部分主流浏览器 filter ...
- Centos7-Docker1.12开启守护进程(远程调用)
本文讲述了Docker1.12.6在Linux下开启守护进程(远程调用),理论上来说其他版本也是一样的改法,博主参考很多都是巨坑,综合自己实战分享给大家,如有错误请留言; - 修改配置 1.修改 do ...
- sharepoint 2010项目中,ashx页面获取SPContext.Current 为null的原因和解决方法
//错误的写法 public void ProcessRequest(HttpContext context) { SPSecurity.RunWithElevatedPrivileges(deleg ...
- python抓取头条文章
python抓取头条美文并存储到mongodb # Author:song from multiprocessing import Pool from urllib.parse import urle ...
- Pop!_OS配置Python环境
Pop!_OS配置Python环境 #0x0 安装vscode #0x1 配置vscode #0x0 安装vscode 见vscode安装 #0x1 配置vscode 安装Python插件 安装pyl ...
- 双向绑定和 vuex 是否冲突
在严格模式下确实有问题,解决方案: https://vuex.vuejs.org/zh/guide/forms.html
- nginx限制访问域名,禁止IP访问
有些时候我们希望系统只能通过固定的域名访问,禁止IP或者恶意绑定的域名访问. 下面的nginx配置,假如host变量不是指定的域名,将返回403. server { listen 80; server ...
- Python List comprehension列表推导式
http://blog.chinaunix.net/uid-28631822-id-3488324.html 具体内容需要进一步学习