教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

什么是內(nèi)置函數(shù)?什么是用戶定義函數(shù)?

更新時間:2023年01月03日14時46分 來源:傳智教育 瀏覽次數(shù):

好口碑IT培訓(xùn)

Hive的函數(shù)分為兩大類:內(nèi)置函數(shù)(Built-in Functions)、用戶定義函數(shù)UDF(User-Defined Functions):

 內(nèi)置函數(shù)可分為:數(shù)值類型函數(shù)、日期類型函數(shù)、字符串類型函數(shù)、集合函數(shù)、條件函數(shù)等;

 用戶定義函數(shù)根據(jù)輸入輸出的行數(shù)可分為3類:UDF、UDAF、UDTF。

1672386890978_81.png

用戶定義函數(shù)UDF分類標(biāo)準(zhǔn), 可以根據(jù)函數(shù)輸入輸出的行數(shù)劃分:

UDF(User-Defined-Function)普通函數(shù),一進(jìn)一出。

UDAF(User-Defined Aggregation Function)聚合函數(shù),多進(jìn)一出。

UDTF(User-Defined Table-Generating Functions)表生成函數(shù),一進(jìn)多出。

UDF分類標(biāo)準(zhǔn)本來針對的是用戶自己編寫開發(fā)實(shí)現(xiàn)的函數(shù)。UDF分類標(biāo)準(zhǔn)可以擴(kuò)大到Hive的所有函數(shù)中:包括內(nèi)置函數(shù)和用戶自定義函數(shù)。

因?yàn)椴还苁鞘裁搭愋偷暮瘮?shù),一定滿足于輸入輸出的要求,那么從輸入幾行和輸出幾行上來劃分沒有任何問題。千萬不要被UD(User-Defined)這兩個字母所迷惑,照成視野的狹隘。比如Hive官方文檔中,針對聚合函數(shù)的標(biāo)準(zhǔn)就是內(nèi)置的UDAF類型。

1672387136499_83.png

內(nèi)置函數(shù)(build-in)指的是Hive開發(fā)實(shí)現(xiàn)好,直接可以使用的函數(shù),也叫做內(nèi)建函數(shù)。內(nèi)置函數(shù)根據(jù)應(yīng)用歸類整體可以分為8大種類型,我們將列舉其中重要的,使用頻率高的函數(shù)的進(jìn)行詳細(xì)講解。

(1)String Functions 字符串函數(shù)

•字符串長度函數(shù):length
•字符串反轉(zhuǎn)函數(shù):reverse
•字符串連接函數(shù):concat
•帶分隔符字符串連接函數(shù):concat_ws
•字符串截取函數(shù):substr,substring
------------String Functions 字符串函數(shù)------------
select length("itcast");
select reverse("itcast");
select concat("angela","baby");
--帶分隔符字符串連接函數(shù):concat_ws(separator, [string | array(string)]+)
select concat_ws('.', 'www', array('itcast', 'cn'));
--字符串截取函數(shù):substr(str, pos[, len]) 或者substring(str, pos[, len])
select substr("angelababy",-2); --pos是從1開始的索引,如果為負(fù)數(shù)則倒著數(shù)select substr("angelababy",2,2);
--分割字符串函數(shù): split(str, regex)
select split('apache hive', ' ');

(2)Date Functions 日期函數(shù)

-----------Date Functions 日期函數(shù)-----------------
--獲取當(dāng)前日期: current_date
select current_date();
--獲取當(dāng)前UNIX時間戳函數(shù): unix_timestamp
select unix_timestamp();
--日期轉(zhuǎn)UNIX時間戳函數(shù): unix_timestamp
select unix_timestamp("2011-12-07 13:01:03");
--指定格式日期轉(zhuǎn)UNIX時間戳函數(shù): unix_timestamp
select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss');
--UNIX時間戳轉(zhuǎn)日期函數(shù): from_unixtime
select from_unixtime(1618238391);
select from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');
--日期比較函數(shù): datediff  日期格式要求'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'select datediff('2012-12-08','2012-05-09');
--日期增加函數(shù): date_add
select date_add('2012-02-28',10);
--日期減少函數(shù): date_sub
select date_sub('2012-01-1',10);
----Mathematical Functions 數(shù)學(xué)函數(shù)-------------
--取整函數(shù): round  返回double類型的整數(shù)值部分(遵循四舍五入)
select round(3.1415926);
--指定精度取整函數(shù): round(double a, int d) 返回指定精度d的double類型select round(3.1415926,4);
--取隨機(jī)數(shù)函數(shù): rand 每次執(zhí)行都不一樣返回一個0到1范圍內(nèi)的隨機(jī)數(shù)select rand();
--指定種子取隨機(jī)數(shù)函數(shù): rand(int seed) 得到一個穩(wěn)定的隨機(jī)數(shù)序列
select rand(3);

(4)Conditional Functions 條件函數(shù)

主要用于條件判斷、邏輯判斷轉(zhuǎn)換這樣的場合

-----Conditional Functions 條件函數(shù)------------------
--使用之前課程創(chuàng)建好的student表數(shù)據(jù)
select * from student limit 3;
--if條件判斷: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
select if(1=2,100,200);
select if(sex ='男','M','W') from student limit 3;
--空值轉(zhuǎn)換函數(shù): nvl(T value, T default_value)
select nvl("allen","itcast");
select nvl(null,"itcast");
--條件轉(zhuǎn)換函數(shù): CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END 
select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end;
select case sex when '男' then 'male' else 'female' end from student limit 3;

0 分享到:
和我們在線交談!