Hive 內(nèi)置函數(shù)
Date Functions
Conditional Functions
Misc. Functions
Hive自定義函數(shù)
-
UDF
(User-Defined-Function) 一進(jìn)一出 -
UDAF
(User- Defined Aggregation Funcation) 聚集函數(shù),多進(jìn)一出腹侣。Count/max/min -
UDTF
(User-Defined Table-Generating Functions) 一進(jìn)多出,如explode()
使用方式 :在HIVE會(huì)話中add 自定義函數(shù)的jar文件,然后創(chuàng)建function繼而使用函數(shù)
UDF 開(kāi)發(fā)
- UDF函數(shù)可以直接應(yīng)用于select語(yǔ)句焰情,對(duì)查詢結(jié)構(gòu)
做格式化處理
后,再輸出內(nèi)容 - 編寫UDF函要注意以下幾點(diǎn):
a. 自定義UDF需要繼承org.apache.hadoop.hive.ql.exec.UDF
b. 需要實(shí)現(xiàn)evaluate函數(shù)嗤锉,evaluate函數(shù)支持重載 - 步驟
a. 把程序打包放到目標(biāo)機(jī)器上去源祈;
b. 進(jìn)入hive客戶端悲立,添加jar包:hive>add jar /run/jar/udf_test.jar;
c. 創(chuàng)建臨時(shí)函數(shù):hive>CREATE TEMPORARY FUNCTION add_example AS 'hive.udf.Add';
d. 銷毀臨時(shí)函數(shù):hive>DROP TEMPORARY FUNCTION add_example;
e. 查詢HQL語(yǔ)句:
SELECT add_example(8, 9) FROM scores;
SELECT add_example(scores.math, scores.art) FROM scores;
SELECT add_example(6, 7, 8, 6.8) FROM scores;
Hive的UDF開(kāi)發(fā)只需要重構(gòu)UDF類的evaluate函數(shù)即可
package com.hrj.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
public class helloUDF extends UDF {
public String evaluate(String str) {
try {
return "HelloWorld " + str;
} catch (Exception e) {
return null;
}
}
}
Hive 自定義函數(shù)調(diào)用
將該java文件編譯成helloudf.jar
hive> add jar helloudf.jar;
hive> create temporary function helloworld as 'com.hrj.hive.udf.helloUDF';
hive> select helloworld(t.col1) from t limit 10;
hive> drop temporary function helloworld;
1.helloworld為臨時(shí)的函數(shù),所以每次進(jìn)入hive都需要add jar以及create temporary操作
2.UDF只能實(shí)現(xiàn)一進(jìn)一出的操作新博,如果需要實(shí)現(xiàn)多進(jìn)一出薪夕,則需要實(shí)現(xiàn)UDAF
Hive復(fù)合數(shù)據(jù)類型
Hive操作復(fù)合類型