hive常用內部函數
hive是一種典型的數據倉庫分析工具,常用語編寫hql語句進行指標分析。在編寫hql的過程中無疑會用到很多的函數,哪本章來編寫一些常見的函數。常見函數很多,不同常見不同人員,使用不一樣,不喜勿噴。
1、隨機函數rand()
格式:rand([int seed])
返回:double
select rand();
select rand(100);
2、切分函數split()
格式:split(str,spliter)
返回:array
select split(rand()*100,'\\.')[0];
3、字符串截取函數substring() 或 substr()
格式:substring(str,start,length) substr(str,start,length)
返回:string
select substring(rand()*100,0,2);
select substr(rand()*100,0,2);
4、判斷函數if()
格式:if(condition,true,false)
返回:true或者flase部分的值
select
s.id,
s.name,
if(s.sex = 1,'男','女')
from s_tmp s
;
select
s.id,
s.name,
if(s.id = 1,'男',if(s.id = 2,'女','妖'))
from s_tmp s
;
5、選擇函數case when
類似於java中的swith。比if函數更加的具有擴展性。
格式:
case 值
when 1 then ''
...
else
end
返回:then或者else後的值
格式2:
case
when 值=1 then ''
...
else
end
返回:then或者else後的值
select
s.id,
s.name,
case s.sex
when 1 then '男'
when 2 then '女'
else '妖'
end
from s_tmp s
;
select
s.id,
s.name,
case
when s.sex=1 then '男'
when s.sex=2 then '女'
else '妖'
end
from s_tmp s
;
6、正在替換函數regexp_replace()
格式:regexp_replace(str,old_string,new_str)
返回:string
select regexp_replace('1.png','.png','.jpg');
select
s.id,
regexp_replace(s.name,'zhangsan','lisi')
from s_tmp s
;
7、類型轉換 函數cast()
格式: cast(x as type)
返回:type類型
select cast(1.0 as int);
select cast(rand()*100 as int);
8、四捨五入函數round()
格式:round(double,保留位數)
返回:double
select round(rand()*100);
select round(rand()*100,2);
9、連接函數concat() 或者 concat_ws()
格式:concat(str1,str2...) 或者 concat_ws(split_str,str1,str2....)
返回:string
select concat("1","2");
select concat_ws("|","1","3","2");
select
concat_ws('|',cast(s.id as string),s.name,cast(s.sex as string))
from s_tmp s
;
10、字符串長度函數length()
格式:length(str)
返回:int
select
length(s.name)
from s_tmp s
;