hive的函数

目录

关系运算

数学运算

逻辑运算

数值计算

日期函数

条件函数

字符串函数

复杂类型访问操作

复杂类型长度统计函数

炸开函数与侧视图

字符串集合函数


关系运算

#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)  不去重

 

一、关系运算: 4 1. 等值比较: = 4 2. 不等值比较: 4 3. 小于比较: < 4 4. 小于等于比较: 5 6. 大于等于比较: >= 5 7. 空值判断: IS NULL 5 8. 非空判断: IS NOT NULL 6 9. LIKE比较: LIKE 6 10. JAVA的LIKE操作: RLIKE 6 11. REGEXP操作: REGEXP 7 二、数学运算: 7 1. 加法操作: + 7 2. 减法操作: - 7 3. 乘法操作: * 8 4. 除法操作: / 8 5. 余操作: % 8 6. 位与操作: & 9 7. 位或操作: | 9 8. 位异或操作: ^ 9 9.位反操作: ~ 10 三、逻辑运算: 10 1. 逻辑与操作: AND 10 2. 逻辑或操作: OR 10 3. 逻辑非操作: NOT 10 四、数值计算 11 1. 函数: round 11 2. 指定精度函数: round 11 3. 向下函数: floor 11 4. 向上函数: ceil 12 5. 向上函数: ceiling 12 6. 随机数函数: rand 12 7. 自然指数函数: exp 13 8. 以10为底对数函数: log10 13 9. 以2为底对数函数: log2 13 10. 对数函数: log 13 11. 幂运算函数: pow 14 12. 幂运算函数: power 14 13. 开平方函数: sqrt 14 14. 二进制函数: bin 14 15. 十六进制函数: hex 15 16. 反转十六进制函数: unhex 15 17. 进制转换函数: conv 15 18. 绝对值函数: abs 16 19. 正函数: pmod 16 20. 正弦函数: sin 16 21. 反正弦函数: asin 16 22. 余弦函数: cos 17 23. 反余弦函数: acos 17 24. positive函数: positive 17 25. negative函数: negative 17 五、日期函数 18 1. UNIX时间戳转日期函数: from_unixtime 18 2. 获当前UNIX时间戳函数: unix_timestamp 18 3. 日期转UNIX时间戳函数: unix_timestamp 18 4. 指定格式日期转UNIX时间戳函数: unix_timestamp 18 5. 日期时间转日期函数: to_date 19 6. 日期转年函数: year 19 7. 日期转月函数: month 19 8. 日期转天函数: day 19 9. 日期转小时函数: hour 20 10. 日期转分钟函数: minute 20 11. 日期转秒函数: second 20 12. 日期转周函数: weekofyear 20 13. 日期比较函数: datediff 21 14. 日期增加函数: date_add 21 15. 日期减少函数: date_sub 21 六、条件函数 21 1. If函数: if 21 2. 非空查找函数: COALESCE 22 3. 条件判断函数:CASE 22 4. 条件判断函数:CASE 22 七、字符串函数 23 1. 字符串长度函数:length 23 2. 字符串反转函数:reverse 23 3. 字符串连接函数:concat 23 4. 带分隔符字符串连接函数:concat_ws 23 5. 字符串截函数:substr,substring 24 6. 字符串截函数:substr,substring 24 7. 字符串转大写函数:upper,ucase 24 8. 字符串转小写函数:lower,lcase 25 9. 去空格函数:trim 25 10. 左边去空格函数:ltrim 25 11. 右边去空格函数:rtrim 25 12. 正则表达式替换函数:regexp_replace 26 13. 正则表达式解析函数:regexp_extract 26 14. URL解析函数:parse_url 26 15. json解析函数:get_json_object 27 16. 空格字符串函数:space 27 17. 重复字符串函数:repeat 27 18. 首字符ascii函数:ascii 28 19. 左补足函数:lpad 28 20. 右补足函数:rpad 28 21. 分割字符串函数: split 28 22. 集合查找函数: find_in_set 29 八、集合统计函数 29 1. 个数统计函数: count 29 2. 总和统计函数: sum 29 3. 平均值统计函数: avg 30 4. 最小值统计函数: min 30 5. 最大值统计函数: max 30 6. 非空集合总体变量函数: var_pop 30 7. 非空集合样本变量函数: var_samp 31 8. 总体标准偏离函数: stddev_pop 31 9. 样本标准偏离函数: stddev_samp 31 10.中位数函数: percentile 31 11. 中位数函数: percentile 31 12. 近似中位数函数: percentile_approx 32 13. 近似中位数函数: percentile_approx 32 14. 直方图: histogram_numeric 32 九、复合类型构建操作 32 1. Map类型构建: map 32 2. Struct类型构建: struct 33 3. array类型构建: array 33 十、复杂类型访问操作 33 1. array类型访问: A[n] 33 2. map类型访问: M[key] 34 3. struct类型访问: S.x 34 十一、复杂类型长度统计函数 34 1. Map类型长度函数: size(Map) 34 2. array类型长度函数: size(Array) 34 3. 类型转换函数 35
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值