博学谷学习记录】超强总结,用心分享 | hive的内置函数

发布时间 2023-06-01 10:15:52作者: 牛牛牛牛牛牛牛牛

【博学谷IT技术支持】

操作

内置函数

数学函数

  • 取整函数round
-- 取整函数 第一个为值,第二个为保留几位小数,取证规则四舍五入
select round(2.2222); -- 2
select round(2.5); --- 3
select round(2.555,2); -- 2.56
  • 向下取整函数: floor
-- 向下取整函数: floor
-- 取整规则:舍弃小数位
select floor(3.1415926); -- 3
select floor(3.6); --- 3
  • 向上取整函数: ceil
-- 向上取整函数: ceil
--- 取整规则:小数点向前进
select ceil(3.1); --- 4
  • 取随机数函数: rand
-- 取随机数函数: rand
--- 随机范围 0 到 1
select rand(); -- 0.38443140253164065
select rand() * 100; --- 32.94192228893596
  • 幂运算函数: pow
--- 幂运算函数: pow
---- 第一个数是幂运算的底数,第二个是幂运算的指数
select pow(1,2); -- 2
select pow(2,4); -- 16
  • 绝对值函数: abs
-- 绝对值函数: abs
-- 返回绝对值
select abs(-1.2); -- 1.2
select abs(2.1); -- 2.1

字符串函数

  • 字符串长度函数:length
-- 字符串长度函数:length
--- 返回字符串的长度
select length('abcde'); -- 5
select length('     '); -- 5
  • 字符串反转函数:reverse
-- 字符串反转函数:reverse
-- 返回字符串的相反结果, 数字会自动转化成字符串
select reverse("hello world"); -- dlrow olleh
select reverse(12345); --- 54321
  • 字符串连接函数:concat
-- 字符串连接函数:concat
-- 直接将字符串拼接在一起
select concat("hello","world"); -- helloworld
  • 字符串连接函数-带分隔符:concat_ws
-- 字符串连接函数-带分隔符:concat_ws
-- 第一个参数为拼接字符串  剩下的是参数
select concat_ws(" ","hello","world"); --- hello world
  • 字符串截取函数:substr,substring
-- 字符串截取函数:substr,substring

-- substr 第一个参数为字符串,第二个参数是截取的起始位置,位置从1开始,为负数时,倒着截取
-- 第三位数是截取的长度,负数时长度要大于截取的值才有效
select substr("abcdef",3); -- cdef
select substr("abcdef",-1); -- f
select substr("abcdef",3,2); -- cd
select substr("abcdef",-1, 3); -- f

select substring("abcdef",3); -- cdef
select substring("abcdef",-1); -- f
select substring("abcdef",3,2); -- cd
select substring("abcdef",-1, 3); -- f
  • 字符串转大写函数:lower,ucase
-- 字符串转大写函数:lower,ucase
-- 值都转大写
select upper("AbCd"); -- ABCD
select ucase("abcD"); -- ABCD
  • 字符串转小写函数:lower,lcase
-- 字符串转小写函数:lower,lcase
-- 值都转小写
select lower("ADCF"); ---adcf
select lcase("ABCDE"); ---abcde
  • 去空格函数:trim
-- 去空格函数:trim
-- 去除字符串两边的空格
--- ltrim 去除字符串左边的空格
--- rtrim 去除字符串右边的空格
select trim("  abc  d   "); -- abc  d
  • URL解析函数:parse_url
-- URL解析函数:parse_url
-- 返回URL中指定的部分。
-- partToExtract的有效值为:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.

select parse_url('http://www.baidu.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST');  -- www.baidu.com
select parse_url('http://www.baidu.com/path1/p.php?k1=v1&k2=v2#Ref1', 'PATH'); --- /path1/p.php
select parse_url('http://www.baidu.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY','k1'); -- v1
  • 分割字符串函数: split
-- 分割字符串函数: split
select split("abcbdbeb","b"); -- ["a","c","d","e",""]