溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Hive UDF的示例分析

發(fā)布時間:2021-12-10 10:07:45 來源:億速云 閱讀:150 作者:小新 欄目:云計算

這篇文章給大家分享的是有關(guān)Hive UDF的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

Hive UDF整理

字符串函數(shù)

字符串長度函數(shù):length

語法: length(string A)

返回值: int

說明:返回字符串A的長度

舉例:

hive> select length(‘a(chǎn)bcedfg’) from dual;

7

字符串反轉(zhuǎn)函數(shù):reverse

語法: reverse(string A)

返回值: string

說明:返回字符串A的反轉(zhuǎn)結(jié)果

舉例:

hive> select reverse(‘a(chǎn)bcedfg’) from dual;

gfdecba

字符串連接函數(shù):concat

語法: concat(string A, string B…)

返回值: string

說明:返回輸入字符串連接后的結(jié)果,支持任意個輸入字符串

舉例:

hive> select concat(‘a(chǎn)bc’,'def’,'gh’) from dual;

abcdefgh

帶分隔符字符串連接函數(shù):concat_ws

語法: concat_ws(string SEP, string A, string B…)

返回值: string

說明:返回輸入字符串連接后的結(jié)果,SEP表示各個字符串間的分隔符

舉例:

hive> select concat_ws(‘,’,'abc’,'def’,'gh’) from dual;

abc,def,gh

字符串截取函數(shù):substr,substring

語法: substr(string A, int start),substring(string A, int start)

返回值: string

說明:返回字符串A從start位置到結(jié)尾的字符串

舉例:

hive> select substr(‘a(chǎn)bcde’,3) from dual;

cde

hive> select substring(‘a(chǎn)bcde’,3) from dual;

cde

hive>  select substr(‘a(chǎn)bcde’,-1) from dual;  (和ORACLE相同)

e

字符串截取函數(shù):substr,substring

語法: substr(string A, int start, int len),substring(string A, int start, int len)

返回值: string

說明:返回字符串A從start位置開始,長度為len的字符串

舉例:

hive> select substr(‘a(chǎn)bcde’,3,2) from dual;

cd

hive> select substring(‘a(chǎn)bcde’,3,2) from dual;

cd

hive>select substring(‘a(chǎn)bcde’,-2,2) from dual;

de

字符串轉(zhuǎn)大寫函數(shù):upper,ucase

語法: upper(string A) ucase(string A)

返回值: string

說明:返回字符串A的大寫格式

舉例:

hive> select upper(‘a(chǎn)bSEd’) from dual;

ABSED

hive> select ucase(‘a(chǎn)bSEd’) from dual;

ABSED

字符串轉(zhuǎn)小寫函數(shù):lower,lcase

語法: lower(string A) lcase(string A)

返回值: string

說明:返回字符串A的小寫格式

舉例:

hive> select lower(‘a(chǎn)bSEd’) from dual;

absed

hive> select lcase(‘a(chǎn)bSEd’) from dual;

absed

去空格函數(shù):trim

語法: trim(string A)

返回值: string

說明:去除字符串兩邊的空格

舉例:

hive> select trim(‘ abc ‘) from dual;

abc

左邊去空格函數(shù):ltrim

語法: ltrim(string A)

返回值: string

說明:去除字符串左邊的空格

舉例:

hive> select ltrim(‘ abc ‘) from dual;

abc

右邊去空格函數(shù):rtrim

語法: rtrim(string A)

返回值: string

說明:去除字符串右邊的空格

舉例:

hive> select rtrim(‘ abc ‘) from dual;

abc

正則表達式替換函數(shù):regexp_replace

語法: regexp_replace(string A, string B, string C)

返回值: string

說明:將字符串A中的符合java正則表達式B的部分替換為C。注意,在有些情況下要使用轉(zhuǎn)義字符

舉例:

hive> select regexp_replace(‘foobar’, ‘oo|ar’, ”) from dual;

fb

正則表達式解析函數(shù):regexp_extract

語法: regexp_extract(string subject, string pattern, int index)

返回值: string

說明:將字符串subject按照pattern正則表達式的規(guī)則拆分,返回index指定的字符。注意,在有些情況下要使用轉(zhuǎn)義字符

舉例:

hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 1) from dual;

the

hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 2) from dual;

bar

hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 0) from dual;

foothebar

URL解析函數(shù):parse_url

語法: parse_url(string urlString, string partToExtract [, string keyToExtract])

返回值: string

說明:返回URL中指定的部分。partToExtract的有效值為:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.

舉例:

hive> select parse_url(‘http://facebook.com/path2/p.php?k1=v1&k2=v2#Ref1′, ‘HOST’) from dual;

facebook.com

hive> select parse_url(‘http://facebook.com/path2/p.php?k1=v1&k2=v2#Ref1′, ‘QUERY’, ‘k1′) from dual;

v1

json解析函數(shù):get_json_object

語法: get_json_object(string json_string, string path)

返回值: string

說明:解析json的字符串json_string,返回path指定的內(nèi)容。如果輸入的json字符串無效,那么返回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 dual;

amy

空格字符串函數(shù):space

語法: space(int n)

返回值: string

說明:返回長度為n的字符串

舉例:

hive> select space(10) from dual;

hive> select length(space(10)) from dual;

10

重復(fù)字符串函數(shù):repeat

語法: repeat(string str, int n)

返回值: string

說明:返回重復(fù)n次后的str字符串

舉例:

hive> select repeat(‘a(chǎn)bc’,5) from dual;

abcabcabcabcabc

首字符ascii函數(shù):ascii

語法: ascii(string str)

返回值: int

說明:返回字符串str第一個字符的ascii碼

舉例:

hive> select ascii(‘a(chǎn)bcde’) from dual;

97

左補足函數(shù):lpad

語法: lpad(string str, int len, string pad)

返回值: string

說明:將str進行用pad進行左補足到len位

舉例:

hive> select lpad(‘a(chǎn)bc’,10,’td’) from dual;

tdtdtdtabc

與GP,ORACLE不同,pad 不能默認(rèn)

右補足函數(shù):rpad

語法: rpad(string str, int len, string pad)

返回值: string

說明:將str進行用pad進行右補足到len位

舉例:

hive> select rpad(‘a(chǎn)bc’,10,’td’) from dual;

abctdtdtdt

分割字符串函數(shù): split

語法:  split(string str, string pat)

返回值:  array

說明: 按照pat字符串分割str,會返回分割后的字符串?dāng)?shù)組

舉例:

hive> select split(‘a(chǎn)btcdtef’,'t’) from dual;

["ab","cd","ef"]

集合查找函數(shù): find_in_set

語法: find_in_set(string str, string strList)

返回值: int

說明: 返回str在strlist第一次出現(xiàn)的位置,strlist是用逗號分割的字符串。如果沒有找該str字符,則返回0(只能是逗號分隔,不然返回0)

舉例:

hive> select find_in_set(‘a(chǎn)b’,'ef,ab,de’) from dual;

2

hive> select find_in_set(‘a(chǎn)t’,'ef,ab,de’) from dual;

0

感謝各位的閱讀!關(guān)于“Hive UDF的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI