Impala常用函数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44455388/article/details/102502177

时间函数:

–当前时间戳
now()
current_timestamp()

–当前时间戳相对于 linux epoch 的秒数
unix_timestamp() , 不带参数, 则返回 ‘1970-01-01 00:00:00’ UTC 到现在的秒数

– 转换到相对于 linux epoch 的秒数
unix_timestamp(now()+ interval 3 days), 如果传入 timestamp 参数, 返回该时间戳相对于 linux epoch 的秒数
unix_timestamp(string datetime, string format), 还支持传入时间字符串, 返回值还是相对于 linux epoch 的秒数

– 将秒数转换到字符串
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)”

– 将时间戳转换为日期字符串
to_date(timestamp) , 将指定时间戳转换为日期字符串, 日期格式为 yyyy–MM-dd .

– 将秒数转换成时间戳
to_timestamp(bigint unixtime)

– 将字符串转换成时间戳
to_timestamp(string date, string pattern)

说明: impala 没有直接将时间戳转换为字符串的函数, 所以经常的写法是: from_unixtime(unix_timestamp( t1 ),‘yyyyMMdd HH:mm’)

– 时间戳取整
Impala 2.11 之前的取整当前时间的写法:
select trunc(now(), ‘YEAR’) --取整到年份, 得到当年 1 月 1 日 0 点 0 分
select trunc(now(), ‘MONTH’) --取整到月份, 得到当月 1 日 0 点 0 分
select trunc(now(), ‘DD’) --取整到日期, 得到当天 0 点 0 分
select trunc(now(), ‘DAY’) --取整到星期, 得到本星期第一天的 0 点 0 分
select trunc(now(), ‘HH24’) --取整到小时, 得到当前小时的 0 分
select trunc(now(), ‘MI’) --取整到分钟, 得到当前分钟 0 秒

Impala 2.11 之后增加了 date_trunc() 函数, 下面是几个取整的写法:
date_trunc(‘year’,now())
date_trunc(‘month’,now())
date_trunc(‘week’,now())
date_trunc(‘day’,now())
date_trunc(‘hour’,now())
date_trunc(‘minute’,now())
date_trunc() 的语法和 date_part() 类似, 下面是完整的时间 part 列表:
microseconds
milliseconds
second
minute
hour
day
week
month
year
decade
century
millennium

– 时间戳提取
date_part(‘year’, now())
extract(now(), ‘year’)
extract(year from now())

– 两个时间戳比较
datediff(timestamp enddate, timestamp startdate) ,相差多少天, 精度是天
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)

– 月份相关的
last_day(timestamp t)
months_between(timestamp newer, timestamp older)

字符串函数:

base64decode(string str) : base64 解码.
base64encode(string str) : base64 编码.

fnv_hash(type v) : 对参数值做hash, 注意结果有正有负

trim(string a): 去除 leading 和 trailing 的 space.
btrim(string a, string chars_to_trim): trim() 函数的加强版, 可以去除任何指定字符.

implala 的字符串长度是按照字节计算的, 下面三个函数是完全一样的.
length(‘中国’) : impala 返回值为 6.
char_length(‘中国’) : impala 返回值为 6.
character_length(‘中国’) : impala 返回值为 6.

Vertica 的字符串长度可以按照字节或字符计数的, 在 DDL 中是按照字节计算长度 (比如 char(10)), 在字符串函数中, 缺省是按照字符计数的.
length(‘中国’) : vertica 返回值为 2.
select CHAR_LENGTH(‘中国’ USING OCTETS) : vertica 返回值为 6.
select CHAR_LENGTH(‘中国’ USING characters) : vertica 返回值为 2.
vertica 的 CHAR_LENGTH() 也可以写成 CHARACTER_LENGTH().

concat(string a, string b…), 字符串拼接.
concat_ws(string sep, string a, string b…), 按照指定分隔符拼接字符串.
group_concat(string s, string sep), 按照指定分隔符, 将多行记录的 s 表达式结果拼接起来.

find_in_set(string str, string strList), 在以逗号分隔的字符串列表 strList 中查找字符串, 结果为列表的下标, 下标起始为 1, 没找到的话为 0, 如果两个参数有一个为 null, 返回值为 null.
instr(string str, string substr [, bigint position [, bigint occurrence ] ])
locate(string substr, string str[, int pos])
locate() 和 instr() 作用相同,返回子串在长字符串的下标, 下标以 1 开始. 两个函数的差异有:
它们的子串参数一个在前一个在后.
推荐是使用 instr(),因为它还可以支持匹配出现的次数, 并且可以用负数指定扫描的起始下标, 负数表示从长字符串结尾算.

substr(string a, int start [, int len]) ,提取子字符串
substring(string a, int start [, int len]) ,提取子字符串
replace(string initial, string target, string replacement), 替换字符串.
split_part(string source, string delimiter, bigint n) , split 字符串并获取指定下标的子串.

repeat(string str, int n), 重复拼接 n 次字符串.

lower(string a) 和 lcase(string a), 都是将参数转成小写形式.
upper(string a) 和 ucase(string a), 都是将参数转成大写形式.

regexp_extract(string subject, string pattern, int index) , 正则提取.
regexp_like(string source, string pattern[, string options]) ,正则 like.
regexp_replace(string initial, string pattern, string replacement) ,正则替换.

parse_url(string urlString, string partToExtract [, string keyToExtract]) , 解析 url 中的指定的部位.

数值函数:

select abs() 绝对值

select power(2,5) 求幂

select sin/cos/tan/asin/acos/atan/atan2() 三角函数

select bin() 十进制转换二进制

ceil() 返回大于参数的最小整数

floor() 返回小于参数的最大整数

conv(23,16,2) 第一个参数从第二个参数16进制转变成第三个参数2进制

degrees(3.14159) 约等于180 弧度转角度

factorial(5) 5的阶乘

fmod/mod/%(11.0,9.0) 2 取余

fnv_hash(type v) 返回64位hash值

greatest(bigint a[, bigint b …]), greatest(double a[, double b …]), greatest(decimal(p,s) a[, decimal(p,s) b …]), greatest(string a[, string b …]), greatest(timestamp a[, timestamp b …]) 返回最大值

least(bigint a[, bigint b …]), least(double a[, double b …]), least(decimal(p,s) a[, decimal(p,s) b …]), least(string a[, string b …]), least(timestamp a[, timestamp b …])

precision() scale() 返回精确度

rand() 产生0-1随机数

select truncate(3.456,7) 3.4560000 保留小数位

条件函数:

CASE a WHEN b THEN c [WHEN d THEN e]… [ELSE f] END

coalesce(type v1, type v2, …) 返回第一个不是null的参数,常用默认值来取代null值

decode(type expression, type search1, type result1 [, type search2, type result2 …] [, type default] )

if(boolean condition, type ifTrue, type ifFalseOrNull)

ifnull(type a, type ifNull)

isfalse(boolean) isnottrue(boolean)

isnotfalse(boolean) istrue(boolean)

isnull(type a, type ifNull)

nullif(expr1,expr2)

nvl(type a, type ifNull)

nvl2(type a, type ifNull, type ifNotNull)

猜你喜欢

转载自blog.csdn.net/weixin_44455388/article/details/102502177