目录
关系运算
#1. = ,<>(!=),< , <= ,> ,>=
#2. is null 判断是否为空值
select 1 from test where xx is null
#3. is not null 判断非空
select 1 from test where xx is not null
#4. like 比较匹配 not like 不匹配
select 1 from test where zhangsan like '%san'
#5. rlike 正则表达式匹配
select 1 from test where 'football' rlike '^f.*r$'
#6. regexp 和rlike 一样
数学运算
#1. + , - , * , / , % 加减乘除取模
#2. 位运算,将两个数值转为相应的二进制,在相应位上进行与,或,反,异或操作
& | ~ ^
例子:select 4 | 8 ===> 12
4的二进制是0100 8的二进制是1000 取每个位置上的最大值再转为10进制
逻辑运算
#1. and逻辑与操作
select 1 from test where 1=1 and 2=2
#2. or逻辑或操作
select 1 from test where 1=1 or 2=1
#3. not 逻辑非操作
select 1 from test where not 1=2
数值计算
#1. round 精度小数函数
select round(3.145,2) ===>3.15
#2. floor 向下取整
select floor(3.5) ===>3
#3. ceil 向上取整
select ceil(3.5) ===>4
#4. rand()随机函数 0-1
select rand() ===>0.21312321
#5. exp 自然指数函数
exp(double a) 返回自然对数e(2.7几)的a次方
select exp(2) ===>7.389...
#6. In(double a) 返回a的自然对数
select In(7.389) ===>2
#7. log10 以10为底对数函数
log10(double a) 返回以10为底的a的对数
select log10(100) ===>2
#8. log2 以2为底对数函数
#9. log(double base,double a) 返回base为底,a的对数
#10.幂运算 pow或者power
pow(double a,double p) 返回a的p次方
#11. 开平方函数 sqrt
sqrt(double a) 返回a的平方根
select (16) ==> 4
#12.二进制函数 bin
bin(BigInt a) ==> 返回a的二进制
#13.十六进制 hex
#14. 进制转换函数: conv
conv(BigInt num,int from base,int to base)
select conv(17,10,2) ==>10001
把数字17从10进制转成2进制
#15. 绝对值函数 abs
select abs(-10) ===>10
#16. 正取余函数: pmod
pmod(int a ,int b ) ==>返回a除以b的余数
select pmod(-9,4)
#17 取反函数 negative
select negative(-1) ==>1
日期函数
#1.unix时间戳转日期格式 from_unixtime
select from_unixtime('1323309615 ','yyyy-MM-dd') ==>2021-10-10
#2.获取当前unix的时间戳 unix_timestamp
select unix_timestamp ===>1323309615
#3.把日期转成时间戳
select unix_timestamp('2021-12-07 13:01:03') ==>1323309667
#4. 日期时间转日期函数 to_date
select to_date(current_timestamp)
#5.当前日期 当前具体时间
current_date current_timestamp
#6.转自定义格式日期 date_format
select date_dormat(current_timestamp,'yyyy-MM-dd')
#7.日期转年月日小时分秒
year month day hour minute second
#8. 每年的第几周 weekofyear
select weekofyear(current_date) ===>42
#9.日期比较函数 datediff(string enddate,string startdate)
select datediff('2012-12-08','2012-05-09') ===>213
#10.日期加 date_add()
select date_add(current_date,2) ==>2022-10-20
#11.日期减 date_sub()
#12.last_day() 返回月份的最后一天日期
select last_day(current_date) ===> 2022-10-31
条件函数
#1.if 函数
if(boolean,a,b)
如果是true就是a 否则就是b
#2. nvl(a,b) 函数 (等同于mysql的ifnull)
如果a是空就返回b,如果不是空就返回a
#3.非空查找函数 coalesce(a,b,c....)
返回第一个非空值,如果都是空就返回空
select coalesce(null,5,2) ===>5
#4.case判断
case when a=1 then male when a=0 then female else end
字符串函数
#1.字符串长度 length()
#2.反转字符串 reverse()
select reverse('abc') ===>cba
#3.连接字符串 concat
select concat('a','b','c') ==>abc
#4.带分隔符拼接 concat_ws
select concat_ws(',','a','b') ==>a,b
#5.截取字符串 substr(str,n,m)
从n开始截取,截取m的长度
#6.转大小写 upper() lower()
#7.去空格 trim() ltrim() rtrim()
#8.正则表达式替换函数 regexp_replace()
regexp_replace(string a,string b,string c)
将字符串a中符合b的正则表达式部分替换成c
select regexp_replace('abc123cde','[0-9]+',' ') ===>abc cde
#9.正则截取函数 regexp_extract
regexp_extract(string str,string pattern,int index)
将字符串str按照pattern正则表达式的规则拆分,返回index指定的字符。将str这个字符串分成很多小组,取第几个组的数据。
select regexp_extract('2022-3-5T12:34:52.123S','.*T([0-9]{2}:[0-9]{2}:[0-9]{2})\..*',1)
#10.URL解析函数 parse_url
#11.json解析函数 get_json_object
get_json_object(string json_string,string path)说明:解析json的字符串json_string返回path指定的内容。如果输入的path字符串无效,返回Null。json中存储的就是对象,根据对象的属性取对应的值。
select get_json_object(lines,'$.name')names from exp01.myjson
===> zhansgan
lisi
wangwu
#12. 空格字符串函数 space
space(int n) 返回长度为n的空格字符串
#13.重复字符串 repeat
repeat(string str,int n) 把str重复n次
select repeat('abc',2) abcabc
#14.首字符ascii函数 : ascii(string str)
返回str第一个字符的ascii码
#15.左右补足函数 lpad rpad
lpad(string str,int len,string pad)将str进行用pad进行左边补足到len位
select lpad('ab',4,'d') ===>ddab
#16.分割字符串函数 split
split(string str,string pat)将str按照pat分割,返回字符数组
select split('abac','a') ===>["b","c"]
#17.集合查找函数 find_in_set (根java中的indexof一个意思)
find_in_set(string str,string strList) 返回str在strLsit中第一次出现的位置,strList是用逗号分割的字符串。如果没找到返回0
select find_in_set('ab','cd,bf') ===>0
select find_in_set('ab','sc,ab,vc') ===>2
复杂类型访问操作
#1.array类型访问:A[n]
A为array类型,n为int类型。返回数组A中第n个变量(下表从0开始)
hive> create table lxw_test as select array("tom","mary","tim") as t from lxw_dual; hive> select t[0],t[1],t[2] from lxw_test; tom mary tim
#2.map类型访问:M[key]
M 为 map 类型,key 为 map 中的 key 值说明:返回 map 类型 M 中,key 值为指定值的 value 值
复杂类型长度统计函数
#1.Map类型长度函数:size() 返回map的长度
#2.array类型长度函数:size() 返回array长度
#3.类型转换函数 cast
cast(expr as <type>)
select cast('123' as Int) 将字符123转为int类型
炸开函数与侧视图
explod()与split()与lateral view三者结合使用,将一行数据炸开成多行。
select lineno,loc from mydemo.word lateral view explod(split(words," "))a as loc
字符串集合函数
collect_set(col)表示将一行数据拼接成集合,并且去重,collect_list(col) 不去重