前言
本文不定期更新贯卦,記錄工作中接觸使用過的Hive函數(shù)
常用函數(shù)
- get_json_object(string json_string,string path)
該函數(shù)的第一個參數(shù)是json對象變量,第二個參數(shù)使用$表示json變量標(biāo)識,然后用.或[]讀取對象或數(shù)組
select get_json_object(pricecount,'$.buyoutRoomRequest') new_id,pricecount
from table_sample a
where d='2018-08-31' limit 100
- json_tuple(string json_string,string k1,string k2...)
該函數(shù)的第一個參數(shù)是json對象變量,之后的參數(shù)是不定長參數(shù),是一組鍵k1,k2...,返回值是元組嘶是,該方法比get_json_object高效,因為可以在一次調(diào)用中輸入多個鍵值
select m.*,n.pricecount
from (select
from table_sample a
where d='2018-08-31' limit 100)n
lateral view json_tuple(pricecount,'paymentType','complete') m as f1,f2
- split(str,regex)
Splits str arround occourances that match regex.該函數(shù)第一個參數(shù)是字符串蛛碌,第二個參數(shù)是設(shè)定的分隔符聂喇,通過第二個參數(shù)把第一個參數(shù)做拆分,返回一個數(shù)組
select split('123,3455,2568',',')
select split('sfas:sdfs:sf',':')
- explode()
explode takes an array (or a map) as an input and outputs the elements of the array (map) as separate rows蔚携;該函數(shù)接收一個參數(shù)希太,參數(shù)類型需是array或者map類型,該函數(shù)的輸出是把輸入?yún)?shù)的每個元素拆成獨立的一行記錄酝蜒。
select explode(split('123,3455,2568',','))
- lateral view()
lateral view udtf(expression) tableAlias as columnAlias (',' columnAlias);Lateral View 一般與用戶自定義表生成函數(shù)(如explode())結(jié)合使用誊辉。UDTF為每個輸入行生成零個或多個輸出行,Lateral view首先將UDTF應(yīng)用于基表的每一行亡脑,然后將結(jié)果輸出行連接到輸入行堕澄,已形成具有提供的表別名的虛擬表。
select j.nf,p.* from (
select m.*,n.pricecount
from (select * from ods_htl_htlinfogoverndb.buyout_appraise a where d = '${zdt.format("yyyy-MM-dd")}' limit 100)n
lateral view json_tuple(pricecount,'paymentType','complete') m as f1,f2 )p
lateral view explode(split(regexp_replace(p.f1,'\\[|\\]',''),',')) j as nf
- from_unixtime(int/bigint timestamp,string format)
該函數(shù)第一個參數(shù)接收int/bigint類型的10位時間戳變量霉咨,帶毫秒的13位時間戳需要做截取蛙紫,第二個參數(shù)是返回的日期的格式,可以不設(shè)置躯护,默認(rèn)是格式:yyyy-MM-dd HH:mm:ss
select from_unixtime(1000000000);
select from_unixtime(1000000000,'yyyy-MM-dd HH');
- unix_timestamp(string date,string format)
該函數(shù)有兩個參數(shù),但兩個參數(shù)都是可選參數(shù)丽涩,具體區(qū)別如下:
unix_timestamp():不帶參數(shù)時返回當(dāng)前時間戳棺滞,current_timestamp()有同樣功能
unix_timestamp(string date):只帶第一個參數(shù)時,返回date對應(yīng)的時間戳矢渊,date的格式必須為yyyy-MM-dd HH:mm:ss
unix_timestamp(string date,string format):返回date對應(yīng)的時間戳继准,date格式由format指定
select unix_timestamp();
select unix_timestamp('2018-09-05 10:24:36');
select unix_timestamp('2018-09-05 10','yyyy-MM-dd HH');
- str_to_map(String text,String delimiter1,String delimiter2)
使用兩個分隔符將文本拆分成鍵值對。Delimiter1將文本分成k-v對矮男,Delimiter2分割每個k-v對移必。對于delimiter1的默認(rèn)值是',',delimiter2的默認(rèn)值是'='.
select str_to_map('abc:11&bcd:22', '&', ':')
- collect_set()
該函數(shù)只接受基本的數(shù)據(jù)類型毡鉴,主要作用是將某字段的值進(jìn)行去重匯總崔泵,返回值是array類型字段
with t as (
select 1 id,123 value
union all
select 1 id,234 value
union all
select 2 id,124 value
)
select t.id,collect_set(t.value)
from t
group by t.id
- collect_list()
該函數(shù)功能等同于collect_set秒赤,唯一的區(qū)別在于collect_set會去除重復(fù)元素,collect_list不去除重復(fù)元素憎瘸,示例sql如下
with t as (
select 1 id,123 value
union all
select 1 id,234 value
union all
select 2 id,124 value
union all
select 2 id,124 value
)
select t.id,collect_set(t.value),collect_list(t.value)
from t
group by t.id
concat_ws(seperator,String s1,String s2...)
該函數(shù)通過分割符seperator將字符串拼接起來入篮,通常配合group by和collect_set使用array_contains(Array<T>,value)
該函數(shù)的用來判斷Arrary<T>中是否包含元素value,返回值是boolean
select array_contains(array(1,2,3,4,5),3)
true
percentile(expr,pc)
該函數(shù)用來計算參數(shù)expr的百分位數(shù)幌甘,expr:字段類型必須是INT潮售,否則報錯;pc:百分位數(shù)锅风,已數(shù)值形式傳入percentile_approx(expr,pc,[nb])
該函數(shù)也是用來計算參數(shù)expr的百分位數(shù)酥诽,但數(shù)據(jù)類型要求沒有percentile嚴(yán)格,該函數(shù)數(shù)值類似類型都可以皱埠;pc:百分位數(shù)肮帐,可以以數(shù)組形式傳入,因此可以一次性查看多個指定百分位數(shù)漱逸;[nb]:控制內(nèi)存消耗的精度泪姨,選填regexp_replace
該函數(shù)用來進(jìn)行字符串替換,下面實例是用來替換某些特殊字符
\t:tab \r:回車 \n:換行
select regexp_replace('string','\n|\t|\r|','')