输入“/”快速插入内容

Impala特殊用法补充

初次认知:impala
HUE=HadoopUser Experience,看这名字就知道怎么回事了吧,没错,直白来说就是Hadoop用户体验,是一个开源的Apache Hadoop UI系统,由Cloudera Desktop演化而来,最后Cloudera公司将其贡献给Apache基金会的Hadoop社区,它是基于Python Web框架Django实现的。通过使用HUE我们可以在浏览器端的Web控制台上与Hadoop集群进行交互来分析处理数据。
其在大数据框架中的定位,也可以通过该图简单了解
0、常见日期转换
代码块
一、当前时间相关
--时间戳格式 2022-06-13 17:27:07.560211000
now()
current_timestamp()
--字符串格式 2022-06-13
substr(regexp_replace(cast(now() as string),'-',''),1,8)
CURRENT_DATE()
二、时间戳相关
--当前时间戳相对于 linux epoch 的秒数
unix_timestamp() , 不带参数, 则返回 '1970-01-01 00:00:00' UTC 到现在的秒数
unix_timestamp(now()+ interval 3 days), 如果传入 timestamp 参数, 返回该时间戳相对于 linux epoch 的秒数
unix_timestamp(string datetime, string format), 还支持传入时间字符串, 返回值还是相对于 linux epoch 的秒数
-- 将秒数转换到字符串
int 是10位的时间戳
from_unixtime(int, 'yyyy/MM/dd HH:mm'), 将指定的时间戳,格式化为字符串. 时间戳参数应该是秒数格式, 所以该参数需要用 unix_timestamp() 包一下.
注意月份和分钟对应的格式字符串, 常用的格式有 "yyyy-MM-dd HH:mm:ss.SSSSSS", "dd/MM/yyyy HH:mm:ss.SSSSSS", "MMM dd, yyyy HH.mm.ss (SSSSSS)"
int 是13位
from_unixtime(cast(register_time/1000 as bigint),'yyyy-MM-dd')
-- 将时间戳转换为日期字符串
to_date(timestamp) , 将指定时间戳转换为日期字符串, "yyyy-MM-dd HH:mm:ss.SSSSSS" 转为 yyyy--MM-dd .
-- 将秒数转换成时间戳
to_timestamp(bigint unixtime) bigint10位转为 "yyyy-MM-dd HH:mm:ss.SSSSSS"
-- 将字符串转换成时间戳
to_timestamp(string date, string pattern) yyyy--MM-dd 转为 "yyyy-MM-dd HH:mm:ss.SSSSSS"
-- 时间戳转字符串
所以经常的写法是: from_unixtime(unix_timestamp( t1 ),'yyyyMMdd HH:mm')
四、日期比较与加减法
-- 两个时间戳比较
datediff(timestamp enddate, timestamp startdate) ,相差多少天, 精度是天
--加减法
验证可以成功的
SELECT now()- interval 5 days --时间戳格式
SELECT to_date(now()- interval 5 days)--date 字符串格式
SELECT distinct date
from rawdata.events
where date
between CURRENT_DATE() - INTERVAL '7' DAY and CURRENT_DATE()
timestamp_cmp(now() + interval 70 minutes, now()), 比较两个时间戳的大小, 本例的结果为 1
impala 没有好用的 timestamp_diff() 函数, 比如我们想要知道两个时间相差多少个小时, 不能直接求出, 下面是一个简单的步骤:
1. 先算出一个小时对应的秒数是多少
2. 将两个时间都转成秒数, 然后做差, 然后除以一个小时的秒数.
-- 时间加减
时间戳可以直接加减 interval n days/months/years/hours/minutes .
也可以使用下面的函数:
years_add(timestamp t, int n)
years_sub(timestamp t, int n)
months_add(timestamp t, int n)
months_sub(timestamp t, int n)
days_add(timestamp t, int n)
days_sub(timestamp t, int n)
hours_add(timestamp t, int n)
hours_sub(timestamp t, int n)
minutes_add(timestamp t, int n)
minutes_sub(timestamp t, int n)
也可以用下面两个通用的函数:
date_add(timestamp startdate, int days)
date_add(timestamp startdate, interval_expression)
date_sub(timestamp startdate, int days)
date_sub(timestamp startdate, interval_expression)
--tips
1、直接把to_date 放在 date between to_date(now()) and to_date(now())是可以的 但是between and 中间放加减法是不可以的
2、betweend 不能和date_sub 一起来用,只能用 >= 和 <= 实现
date = '${dt}' --格式的话设置