柚子快報(bào)激活碼778899分享:Hive SQL常用函數(shù)
柚子快報(bào)激活碼778899分享:Hive SQL常用函數(shù)
一、日期函數(shù)
1、將時(shí)間戳轉(zhuǎn)化為日期
from_unixtime(bigint unixtime,string format)
舉例:from_unixtime(1237573801,‘yyyy-MM-dd HH:mm:ss’)
常用string format 格式
‘yyyy-MM-dd HH:mm:ss’ 年月日時(shí)分秒格式‘yyyy-MM-dd HH:mm’ 年月日時(shí)分格式‘yyyy-MM-dd HH’ 年月日時(shí)格式‘yyyy-MM-dd’ 年月日格式
2、將日期轉(zhuǎn)化為時(shí)間戳
unix_timestamp(stringdate)
舉例:unix_timestamp(‘2020-01-01 06:06:00’,‘yyyy-MM-dd HH:mm:ss’)
3、日期比較函數(shù)
datediff(string enddate,string startdate) 日期間隔天數(shù),結(jié)束日期和開始日期間隔天數(shù)
舉例:datediff(‘2020-02-02’,‘2020-02-01’)
結(jié)果:1
4、日期增加函數(shù)
date_add(string startdate,int days) 返回開始日期startdate增加days天后的日期。
舉例:date_add(‘2020-02-12’,10)
結(jié)果:2020-02-22
5、日期減少函數(shù)
date_sub(string startdate,int days) 返回開始日期startdate減少days天后的日期。
舉例:date_sub(‘2020-02-12’,10)
結(jié)果:2020-02-02
6、返回日期時(shí)間字段中的日期部分
to_date(‘yyyy-MM-dd HH:mm:ss’)
二、條件函數(shù)
1、if函數(shù)
if(條件表達(dá)式,結(jié)果1,結(jié)果2) 當(dāng)條件為true時(shí),返回結(jié)果1,否則結(jié)果2
舉例:if(2 > 1,‘是’,‘否’)
結(jié)果:是
2、條件判斷函數(shù)case when
case 表達(dá)式 when 條件1 then 結(jié)果1 when 條件2 then 結(jié)果2 else 結(jié)果3 end as 字段名
舉例:
3、非空查找函數(shù)
coalesce(T v1,T v2) 返回參數(shù)中的第一個(gè)非空值;如果所有值都為NULL,那么返回NULL
舉例:coalesce(a,b,c) 如果a為null,則選擇b,如果b為null,則選擇c;
? 如果a不為null,則選擇a;
? 如果a b c都均為null,則返回null。
三、窗口函數(shù)
1、累計(jì)計(jì)算窗口函數(shù)
sum(A) over (partition by B order by C rows between D1 and D2)
avg(A) over (partition by B order by C rows between D1 and D2)
rows between unbounded preceding and current row 包括本行和之前所有的行
rows between current row preceding and unbounded following 包括本行和之后所有的行
rows between 3 preceding and current row 包括本行以內(nèi)和前三行
rows between 3 preceding and 1 following 從前三行到下一行(5行)
2、分區(qū)排序窗口函數(shù)
row_number() over (partition by A order by B)
依次排序,序號(hào)不會(huì)重復(fù)
rank() over (partition by A order by B)
字段值相同,序號(hào)相同,跳躍排序,如果有兩個(gè)第一名,接下來是第三名。
dense_rank() over (partition by A order by B)
字段值相同,序號(hào)相同,連續(xù)排序,如果有兩個(gè)第一名,接下來是第二名。
3、分組排序窗口函數(shù)
ntile(n) over (partition by A order by B)
n為要分組的數(shù)量
4、偏移分析窗口函數(shù)
lag(exp_str,offset,defval) over (partition by A order by B)
同一次查詢中取出同一字段的前N行數(shù)據(jù)
lead(exp_str,offset,defval) over (partition by A order by B)
同一次查詢中取出同一字段的后N行數(shù)據(jù)
exp_str:字段名稱
offset:偏移量,即上一個(gè)或者上N個(gè)的值,以lag函數(shù)為列子,假設(shè)當(dāng)前行再表中排在第5行,則offset為3,則表示我們要找的數(shù)據(jù)行就是表中的第二行(即5-3=2),offset默認(rèn)值為1。
defval:當(dāng)兩個(gè)函數(shù)取上N/下N個(gè)值,當(dāng)在表中從當(dāng)前行位置向前數(shù)N行已經(jīng)超出了表的范圍時(shí),函數(shù)會(huì)將defval這個(gè)參數(shù)作為函數(shù)的返回值,若沒有指定默認(rèn)值,則返回null。
四、聚合函數(shù)
sum(): 求和avg(): 平均值max(): 最大值min(): 最小值count(): 計(jì)數(shù)count(distinct …) 去重計(jì)數(shù)
五、hive解析json字符串
get_json_object(string json_string,string path)
string json_string:填寫json對(duì)象變量
sting path:第二個(gè)參數(shù)使用
表示
j
s
o
n
變量標(biāo)識(shí),通常為
表示json變量標(biāo)識(shí),通常為
表示json變量標(biāo)識(shí),通常為.key形式
舉例:假設(shè)fruit為fruits表中的字段,其結(jié)構(gòu)為fruit:{“type”:“apple”,“weight”:2,“price”:8,“color”:“red”} select get_json_object(fruit,‘$.type’)
? from fruits
結(jié)果:apple
六、截取字符串函數(shù)
substr(string,int start,int len)
舉例:substr(‘2020-01-01’,1,7)
t":2,“price”:8,“color”:“red”} select get_json_object(fruit,‘$.type’)
? from fruits
結(jié)果:apple
六、截取字符串函數(shù)
substr(string,int start,int len)
舉例:substr(‘2020-01-01’,1,7)
結(jié)果:2020-01
柚子快報(bào)激活碼778899分享:Hive SQL常用函數(shù)
相關(guān)閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。