聚合函數(shù)
hive支持 count(),max(),min(),sum(),avg() 等常用的聚合函數(shù)
注意:
聚合操作時(shí)要注意null值
count(*) 包含null值,統(tǒng)計(jì)所有行數(shù)
count(id) 不包含null值
min 求最小值是不包含null,除非所有值都是null
avg 求平均值也是不包含null
- 非空集合總體變量函數(shù): var_pop
語(yǔ)法: var_pop(col)
返回值: double
說(shuō)明: 統(tǒng)計(jì)結(jié)果集中col非空集合的總體變量(忽略null)
- 非空集合樣本變量函數(shù): var_samp
語(yǔ)法: var_samp (col)
返回值: double
說(shuō)明: 統(tǒng)計(jì)結(jié)果集中col非空集合的樣本變量(忽略null)
- 總體標(biāo)準(zhǔn)偏離函數(shù): stddev_pop
語(yǔ)法: stddev_pop(col)
返回值: double
說(shuō)明: 該函數(shù)計(jì)算總體標(biāo)準(zhǔn)偏離,并返回總體變量的平方根,其返回值與VAR_POP函數(shù)的平方根相同
- 中位數(shù)函數(shù): percentile
語(yǔ)法: percentile(BIGINT col, p)
返回值: double
說(shuō)明: 求準(zhǔn)確的第pth個(gè)百分位數(shù),p必須介于0和1之間,但是col字段目前只支持整數(shù),不支持浮點(diǎn)數(shù)類型
關(guān)系運(yùn)算
支持:等值(=)、不等值(!= 或 <>)、小于(<)、小于等于(<=)、大于(>)、大于等于(>=)
空值判斷(is null)、非空判斷(is not null)
- LIKE比較: LIKE
語(yǔ)法: A LIKE B
操作類型: strings
描述: 如果字符串A或者字符串B為NULL,則返回NULL;如果字符串A符合表達(dá)式B 的正則語(yǔ)法,則為TRUE;否則為FALSE。B中字符”_”表示任意單個(gè)字符,而字符”%”表示任意數(shù)量的字符。
- JAVA的LIKE操作: RLIKE
語(yǔ)法: A RLIKE B
操作類型: strings
描述: 如果字符串A或者字符串B為NULL,則返回NULL;如果字符串A符合JAVA正則表達(dá)式B的正則語(yǔ)法,則為TRUE;否則為FALSE。
- REGEXP操作: REGEXP
語(yǔ)法: A REGEXP B
操作類型: strings
描述: 功能與RLIKE相同
示例:select 1 from tableName where 'footbar' REGEXP '^f.*r$';
結(jié)果:1
數(shù)學(xué)運(yùn)算
支持所有數(shù)值類型:加(+)、減(-)、乘(*)、除(/)、取余(%)、位與(&)、位或(|)、位異或(^)、位取反(~)
邏輯運(yùn)算
支持:邏輯與(and)、邏輯或(or)、邏輯非(not)
數(shù)值運(yùn)算
- 取整函數(shù): round
語(yǔ)法: round(double a)
返回值: BIGINT
說(shuō)明: 返回double類型的整數(shù)值部分 (遵循四舍五入)
示例:select round(3.1415926) from tableName;
結(jié)果:3
- 指定精度取整函數(shù): round
語(yǔ)法: round(double a, int d)
返回值: DOUBLE
說(shuō)明: 返回指定精度d的double類型
hive> select round(3.1415926,4) from tableName;
3.1416
- 向下取整函數(shù): floor
語(yǔ)法: floor(double a)
返回值: BIGINT
說(shuō)明: 返回等于或者小于該double變量的最大的整數(shù)
hive> select floor(3.641) from tableName;
3
- 向上取整函數(shù): ceil
語(yǔ)法: ceil(double a)
返回值: BIGINT
說(shuō)明: 返回等于或者大于該double變量的最小的整數(shù)
hive> select ceil(3.1415926) from tableName;
4
- 取隨機(jī)數(shù)函數(shù): rand
語(yǔ)法: rand(),rand(int seed)
返回值: double
說(shuō)明: 返回一個(gè)0到1范圍內(nèi)的隨機(jī)數(shù)。如果指定種子seed,則會(huì)等到一個(gè)穩(wěn)定的隨機(jī)數(shù)序列
hive> select rand() from tableName; -- 每次執(zhí)行此語(yǔ)句得到的結(jié)果都不同
0.5577432776034763
hive> select rand(100) ; -- 只要指定種子,每次執(zhí)行此語(yǔ)句得到的結(jié)果一樣的
0.7220096548596434
- 自然指數(shù)函數(shù): exp
語(yǔ)法: exp(double a)
返回值: double
說(shuō)明: 返回自然對(duì)數(shù)e的a次方
hive> select exp(2) ;
7.38905609893065
- 以10為底對(duì)數(shù)函數(shù): log10
語(yǔ)法: log10(double a)
返回值: double
說(shuō)明: 返回以10為底的a的對(duì)數(shù)
hive> select log10(100) ;
2.0
此外還有:以2為底對(duì)數(shù)函數(shù): log2()、對(duì)數(shù)函數(shù): log()
- 冪運(yùn)算函數(shù): pow
語(yǔ)法: pow(double a, double p)
返回值: double
說(shuō)明: 返回a的p次冪
hive> select pow(2,4) ;
16.0
- 開平方函數(shù): sqrt
語(yǔ)法: sqrt(double a)
返回值: double
說(shuō)明: 返回a的平方根
hive> select sqrt(16) ;
4.0
- 二進(jìn)制函數(shù): bin
語(yǔ)法: bin(BIGINT a)
返回值: string
說(shuō)明: 返回a的二進(jìn)制代碼表示
hive> select bin(7) ;
111
十六進(jìn)制函數(shù): hex()、將十六進(jìn)制轉(zhuǎn)化為字符串函數(shù): unhex()
進(jìn)制轉(zhuǎn)換函數(shù): conv(bigint num, int from_base, int to_base) 說(shuō)明: 將數(shù)值num從from_base進(jìn)制轉(zhuǎn)化到to_base進(jìn)制
此外還有很多數(shù)學(xué)函數(shù):絕對(duì)值函數(shù): abs()、正取余函數(shù): pmod()、正弦函數(shù): sin()、反正弦函數(shù): asin()、余弦函數(shù): cos()、反余弦函數(shù): acos()、positive函數(shù): positive()、negative函數(shù): negative()
條件函數(shù)
- If函數(shù): if
語(yǔ)法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
返回值: T
說(shuō)明: 當(dāng)條件testCondition為TRUE時(shí),返回valueTrue;否則返回valueFalseOrNull
hive> select if(1=2,100,200) ;
200
hive> select if(1=1,100,200) ;
100
- 非空查找函數(shù): coalesce
語(yǔ)法: coalesce(T v1, T v2, …)
返回值: T
說(shuō)明: 返回參數(shù)中的第一個(gè)非空值;如果所有值都為NULL,那么返回NULL
hive> select coalesce(null,'100','50') ;
100
- 條件判斷函數(shù):case when (兩種寫法,其一)
語(yǔ)法: case when a then b [when c then d]* [else e] end
返回值: T
說(shuō)明:如果a為TRUE,則返回b;如果c為TRUE,則返回d;否則返回e
hive> select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end from tableName;
mary
- 條件判斷函數(shù):case when (兩種寫法,其二)
語(yǔ)法: case a when b then c [when d then e]* [else f] end
返回值: T
說(shuō)明:如果a等于b,那么返回c;如果a等于d,那么返回e;否則返回f
hive> Select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end from tableName;
mary
日期函數(shù)
注:以下SQL語(yǔ)句中的 from tableName 可去掉,不影響查詢結(jié)果
- 獲取當(dāng)前UNIX時(shí)間戳函數(shù): unix_timestamp
語(yǔ)法: unix_timestamp()
返回值: bigint
說(shuō)明: 獲得當(dāng)前時(shí)區(qū)的UNIX時(shí)間戳
hive> select unix_timestamp() from tableName;
1616906976
- UNIX時(shí)間戳轉(zhuǎn)日期函數(shù): from_unixtime
語(yǔ)法: from_unixtime(bigint unixtime[, string format])
返回值: string
說(shuō)明: 轉(zhuǎn)化UNIX時(shí)間戳(從1970-01-01 00:00:00 UTC到指定時(shí)間的秒數(shù))到當(dāng)前時(shí)區(qū)的時(shí)間格式
hive> select from_unixtime(1616906976,'yyyyMMdd') from tableName;
20210328
- 日期轉(zhuǎn)UNIX時(shí)間戳函數(shù): unix_timestamp
語(yǔ)法: unix_timestamp(string date)
返回值: bigint
說(shuō)明: 轉(zhuǎn)換格式為"yyyy-MM-dd HH:mm:ss"的日期到UNIX時(shí)間戳。如果轉(zhuǎn)化失敗,則返回0。
hive> select unix_timestamp('2021-03-08 14:21:15') from tableName;
1615184475
- 指定格式日期轉(zhuǎn)UNIX時(shí)間戳函數(shù): unix_timestamp
語(yǔ)法: unix_timestamp(string date, string pattern)
返回值: bigint
說(shuō)明: 轉(zhuǎn)換pattern格式的日期到UNIX時(shí)間戳。如果轉(zhuǎn)化失敗,則返回0。
hive> select unix_timestamp('2021-03-08 14:21:15','yyyyMMdd HH:mm:ss') from tableName;
1615184475
- 日期時(shí)間轉(zhuǎn)日期函數(shù): to_date
語(yǔ)法: to_date(string timestamp)
返回值: string
說(shuō)明: 返回日期時(shí)間字段中的日期部分。
hive> select to_date('2021-12-28 14:03:01') from tableName;
2021-12-28
- 日期轉(zhuǎn)年函數(shù): year
語(yǔ)法: year(string date)
返回值: int
說(shuō)明: 返回日期中的年。
hive> select year('2021-12-28 10:03:01') from tableName;
2021
hive> select year('2021-12-28') from tableName;
2021
- 日期轉(zhuǎn)月函數(shù): month
語(yǔ)法: month (string date)
返回值: int
說(shuō)明: 返回日期中的月份。
hive> select month('2020-12-28 12:03:01') from tableName;
12
hive> select month('2021-03-08') from tableName;
8
- 日期轉(zhuǎn)天函數(shù): day
語(yǔ)法: day (string date)
返回值: int
說(shuō)明: 返回日期中的天。
hive> select day('2020-12-08 10:03:01') from tableName;
8
hive> select day('2020-12-24') from tableName;
24
- 日期轉(zhuǎn)小時(shí)函數(shù): hour
語(yǔ)法: hour (string date)
返回值: int
說(shuō)明: 返回日期中的小時(shí)。
hive> select hour('2020-12-08 10:03:01') from tableName;
10
- 日期轉(zhuǎn)分鐘函數(shù): minute
語(yǔ)法: minute (string date)
返回值: int
說(shuō)明: 返回日期中的分鐘。
hive> select minute('2020-12-08 10:03:01') from tableName;
3
- 日期轉(zhuǎn)秒函數(shù): second
語(yǔ)法: second (string date)
返回值: int
說(shuō)明: 返回日期中的秒。
hive> select second('2020-12-08 10:03:01') from tableName;
1
- 日期轉(zhuǎn)周函數(shù): weekofyear
語(yǔ)法: weekofyear (string date)
返回值: int
說(shuō)明: 返回日期在當(dāng)前的周數(shù)。
hive> select weekofyear('2020-12-08 10:03:01') from tableName;
49
- 日期比較函數(shù): datediff
語(yǔ)法: datediff(string enddate, string startdate)
返回值: int
說(shuō)明: 返回結(jié)束日期減去開始日期的天數(shù)。
hive> select datediff('2020-12-08','2012-05-09') from tableName;
213
- 日期增加函數(shù): date_add
語(yǔ)法: date_add(string startdate, int days)
返回值: string
說(shuō)明: 返回開始日期startdate增加days天后的日期。
hive> select date_add('2020-12-08',10) from tableName;
2020-12-18
- 日期減少函數(shù): date_sub
語(yǔ)法: date_sub (string startdate, int days)
返回值: string
說(shuō)明: 返回開始日期startdate減少days天后的日期。
hive> select date_sub('2020-12-08',10) from tableName;
2020-11-28
字符串函數(shù)
- 字符串長(zhǎng)度函數(shù):length
語(yǔ)法: length(string A)
返回值: int
說(shuō)明:返回字符串A的長(zhǎng)度
hive> select length('abcedfg') from tableName;
7
- 字符串反轉(zhuǎn)函數(shù):reverse
語(yǔ)法: reverse(string A)
返回值: string
說(shuō)明:返回字符串A的反轉(zhuǎn)結(jié)果
hive> select reverse('abcedfg') from tableName;
gfdecba
- 字符串連接函數(shù):concat
語(yǔ)法: concat(string A, string B…)
返回值: string
說(shuō)明:返回輸入字符串連接后的結(jié)果,支持任意個(gè)輸入字符串
hive> select concat('abc','def’,'gh')from tableName;
abcdefgh
- 帶分隔符字符串連接函數(shù):concat_ws
語(yǔ)法: concat_ws(string SEP, string A, string B…)
返回值: string
說(shuō)明:返回輸入字符串連接后的結(jié)果,SEP表示各個(gè)字符串間的分隔符
hive> select concat_ws(',','abc','def','gh')from tableName;
abc,def,gh
- 字符串截取函數(shù):substr,substring
語(yǔ)法: substr(string A, int start),substring(string A, int start)
返回值: string
說(shuō)明:返回字符串A從start位置到結(jié)尾的字符串
hive> select substr('abcde',3) from tableName;
cde
hive> select substring('abcde',3) from tableName;
cde
hive> select substr('abcde',-1) from tableName; (和ORACLE相同)
e
- 字符串截取函數(shù):substr,substring
語(yǔ)法: substr(string A, int start, int len),substring(string A, int start, int len)
返回值: string
說(shuō)明:返回字符串A從start位置開始,長(zhǎng)度為len的字符串
hive> select substr('abcde',3,2) from tableName;
cd
hive> select substring('abcde',3,2) from tableName;
cd
hive>select substring('abcde',-2,2) from tableName;
de
- 字符串轉(zhuǎn)大寫函數(shù):upper,ucase
語(yǔ)法: upper(string A) ucase(string A)
返回值: string
說(shuō)明:返回字符串A的大寫格式
hive> select upper('abSEd') from tableName;
ABSED
hive> select ucase('abSEd') from tableName;
ABSED
- 字符串轉(zhuǎn)小寫函數(shù):lower,lcase
語(yǔ)法: lower(string A) lcase(string A)
返回值: string
說(shuō)明:返回字符串A的小寫格式
hive> select lower('abSEd') from tableName;
absed
hive> select lcase('abSEd') from tableName;
absed
- 去空格函數(shù):trim
語(yǔ)法: trim(string A)
返回值: string
說(shuō)明:去除字符串兩邊的空格
hive> select trim(' abc ') from tableName;
abc
- 左邊去空格函數(shù):ltrim
語(yǔ)法: ltrim(string A)
返回值: string
說(shuō)明:去除字符串左邊的空格
hive> select ltrim(' abc ') from tableName;
abc
- 右邊去空格函數(shù):rtrim
語(yǔ)法: rtrim(string A)
返回值: string
說(shuō)明:去除字符串右邊的空格
hive> select rtrim(' abc ') from tableName;
abc
- 正則表達(dá)式替換函數(shù):regexp_replace
語(yǔ)法: regexp_replace(string A, string B, string C)
返回值: string
說(shuō)明:將字符串A中的符合java正則表達(dá)式B的部分替換為C。注意,在有些情況下要使用轉(zhuǎn)義字符,類似oracle中的regexp_replace函數(shù)。
hive> select regexp_replace('foobar', 'oo|ar', '') from tableName;
fb
- 正則表達(dá)式解析函數(shù):regexp_extract
語(yǔ)法: regexp_extract(string subject, string pattern, int index)
返回值: string
說(shuō)明:將字符串subject按照pattern正則表達(dá)式的規(guī)則拆分,返回index指定的字符。
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) from tableName;
the
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) from tableName;
bar
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) from tableName;
foothebar
strong>注意,在有些情況下要使用轉(zhuǎn)義字符,下面的等號(hào)要用雙豎線轉(zhuǎn)義,這是java正則表達(dá)式的規(guī)則。
select data_field,
regexp_extract(data_field,'.*?bgStart\\=([^&]+)',1) as aaa,
regexp_extract(data_field,'.*?contentLoaded_headStart\\=([^&]+)',1) as bbb,
regexp_extract(data_field,'.*?AppLoad2Req\\=([^&]+)',1) as ccc
from pt_nginx_loginlog_st
where pt = '2021-03-28' limit 2;
- URL解析函數(shù):parse_url
語(yǔ)法: parse_url(string urlString, string partToExtract [, string keyToExtract])
返回值: string
說(shuō)明:返回URL中指定的部分。partToExtract的有效值為:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
hive> select parse_url
('https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST')
from tableName;
www.tableName.com
hive> select parse_url
('https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY', 'k1')
from tableName;
v1
- json解析函數(shù):get_json_object
語(yǔ)法: get_json_object(string json_string, string path)
返回值: string
說(shuō)明:解析json的字符串json_string,返回path指定的內(nèi)容。如果輸入的json字符串無(wú)效,那么返回NULL。
hive> select get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}], "bicycle":{"price":19.95,"color":"red"} },"email":"amy@only_for_json_udf_test.net","owner":"amy"}','$.owner') from tableName;
- 空格字符串函數(shù):space
語(yǔ)法: space(int n)
返回值: string
說(shuō)明:返回長(zhǎng)度為n的字符串
hive> select space(10) from tableName;
hive> select length(space(10)) from tableName;
10
- 重復(fù)字符串函數(shù):repeat
語(yǔ)法: repeat(string str, int n)
返回值: string
說(shuō)明:返回重復(fù)n次后的str字符串
hive> select repeat('abc',5) from tableName;
abcabcabcabcabc
- 首字符ascii函數(shù):ascii
語(yǔ)法: ascii(string str)
返回值: int
說(shuō)明:返回字符串str第一個(gè)字符的ascii碼
hive> select ascii('abcde') from tableName;
97
- 左補(bǔ)足函數(shù):lpad
語(yǔ)法: lpad(string str, int len, string pad)
返回值: string
說(shuō)明:將str進(jìn)行用pad進(jìn)行左補(bǔ)足到len位
hive> select lpad('abc',10,'td') from tableName;
tdtdtdtabc
注意:與GP,ORACLE不同,pad 不能默認(rèn)
- 右補(bǔ)足函數(shù):rpad
語(yǔ)法: rpad(string str, int len, string pad)
返回值: string
說(shuō)明:將str進(jìn)行用pad進(jìn)行右補(bǔ)足到len位
hive> select rpad('abc',10,'td') from tableName;
abctdtdtdt
- 分割字符串函數(shù): split
語(yǔ)法: split(string str, string pat)
返回值: array
說(shuō)明: 按照pat字符串分割str,會(huì)返回分割后的字符串?dāng)?shù)組
hive> select split('abtcdtef','t') from tableName;
["ab","cd","ef"]
- 集合查找函數(shù): find_in_set
語(yǔ)法: find_in_set(string str, string strList)
返回值: int
說(shuō)明: 返回str在strlist第一次出現(xiàn)的位置,strlist是用逗號(hào)分割的字符串。如果沒有找該str字符,則返回0
hive> select find_in_set('ab','ef,ab,de') from tableName;
2
hive> select find_in_set('at','ef,ab,de') from tableName;
0
復(fù)合類型構(gòu)建操作
- Map類型構(gòu)建: map
語(yǔ)法: map (key1, value1, key2, value2, …)
說(shuō)明:根據(jù)輸入的key和value對(duì)構(gòu)建map類型
hive> Create table mapTable as select map('100','tom','200','mary') as t from tableName;
hive> describe mapTable;
t map<string ,string>
hive> select t from tableName;
{"100":"tom","200":"mary"}
- Struct類型構(gòu)建: struct
語(yǔ)法: struct(val1, val2, val3, …)
說(shuō)明:根據(jù)輸入的參數(shù)構(gòu)建結(jié)構(gòu)體struct類型
hive> create table struct_table as select struct('tom','mary','tim') as t from tableName;
hive> describe struct_table;
t struct<col1:string ,col2:string,col3:string>
hive> select t from tableName;
{"col1":"tom","col2":"mary","col3":"tim"}
- array類型構(gòu)建: array
語(yǔ)法: array(val1, val2, …)
說(shuō)明:根據(jù)輸入的參數(shù)構(gòu)建數(shù)組array類型
hive> create table arr_table as select array("tom","mary","tim") as t from tableName;
hive> describe tableName;
t array<string>
hive> select t from tableName;
["tom","mary","tim"]
復(fù)雜類型訪問操作
- array類型訪問: A[n]
語(yǔ)法: A[n]
操作類型: A為array類型,n為int類型
說(shuō)明:返回?cái)?shù)組A中的第n個(gè)變量值。數(shù)組的起始下標(biāo)為0。比如,A是個(gè)值為['foo', 'bar']的數(shù)組類型,那么A[0]將返回'foo',而A[1]將返回'bar'
hive> create table arr_table2 as select array("tom","mary","tim") as t
from tableName;
hive> select t[0],t[1] from arr_table2;
tom mary tim
- map類型訪問: M[key]
語(yǔ)法: M[key]
操作類型: M為map類型,key為map中的key值
說(shuō)明:返回map類型M中,key值為指定值的value值。比如,M是值為{'f' -> 'foo', 'b' -> 'bar', 'all' -> 'foobar'}的map類型,那么M['all']將會(huì)返回'foobar'
hive> Create table map_table2 as select map('100','tom','200','mary') as t from tableName;
hive> select t['200'],t['100'] from map_table2;
mary tom
- struct類型訪問: S.x
語(yǔ)法: S.x
操作類型: S為struct類型
說(shuō)明:返回結(jié)構(gòu)體S中的x字段。比如,對(duì)于結(jié)構(gòu)體struct foobar {int foo, int bar},foobar.foo返回結(jié)構(gòu)體中的foo字段
hive> create table str_table2 as select struct('tom','mary','tim') as t from tableName;
hive> describe tableName;
t struct<col1:string ,col2:string,col3:string>
hive> select t.col1,t.col3 from str_table2;
tom tim
復(fù)雜類型長(zhǎng)度統(tǒng)計(jì)函數(shù)
- Map類型長(zhǎng)度函數(shù): size(Map<k .V>)
語(yǔ)法: size(Map<k .V>)
返回值: int
說(shuō)明: 返回map類型的長(zhǎng)度
hive> select size(t) from map_table2;
2
- array類型長(zhǎng)度函數(shù): size(Array)
語(yǔ)法: size(Array<T>)
返回值: int
說(shuō)明: 返回array類型的長(zhǎng)度
hive> select size(t) from arr_table2;
4
- 類型轉(zhuǎn)換函數(shù) ***
類型轉(zhuǎn)換函數(shù): cast
語(yǔ)法: cast(expr as <type>)
返回值: Expected "=" to follow "type"
說(shuō)明: 返回轉(zhuǎn)換后的數(shù)據(jù)類型
hive> select cast('1' as bigint) from tableName;
1