1.查看系統自帶的函數java
hive> show functions;node
2.顯示自帶的函數的用法linux
hive> desc function upper;apache
3.詳細顯示自帶的函數的用法編程
hive> desc function extended upper;服務器
0: jdbc:hive2://node01:10000> desc function upper; +--------------------------------------------------------------------+--+ | tab_name | +--------------------------------------------------------------------+--+ | upper(str) - Returns str with all characters changed to uppercase | +--------------------------------------------------------------------+--+ 1 row selected (0.028 seconds) 0: jdbc:hive2://node01:10000> desc function extended upper; +--------------------------------------------------------------------+--+ | tab_name | +--------------------------------------------------------------------+--+ | upper(str) - Returns str with all characters changed to uppercase | | Synonyms: ucase | | Example: | | > SELECT upper('Facebook') FROM src LIMIT 1; | | 'FACEBOOK' | +--------------------------------------------------------------------+--+ 5 rows selected (0.03 seconds)
1)Hive 自帶了一些函數,好比:max/min等,可是數量有限,本身能夠經過自定義UDF來方便的擴展。函數
2)當Hive提供的內置函數沒法知足你的業務處理須要時,此時就能夠考慮使用用戶自定義函數(UDF:user-defined function)。oop
3)根據用戶自定義函數類別分爲如下三種:ui
(1)UDF(User-Defined-Function)lua
一進一出
(2)UDAF(User-Defined Aggregation Function)
彙集函數,多進一出
相似於:count/max/min
(3)UDTF(User-Defined Table-Generating Functions)
一進多出
如lateral view explore()
4)官方文檔地址
https://cwiki.apache.org/confluence/display/Hive/HivePlugins
5)編程步驟:
(1)繼承org.apache.hadoop.hive.ql.UDF
(2)須要實現evaluate函數;evaluate函數支持重載;
(3)在hive的命令行窗口建立函數
a)添加jar
add jar linux_jar_path
b)建立function,
create [temporary] function [dbname.]function_name AS class_name;
(4)在hive的命令行窗口刪除函數
Drop [temporary] function [if exists] [dbname.]function_name;
6)注意事項
(1)UDF必需要有返回類型,能夠返回null,可是返回類型不能爲void;
1.建立一個Maven工程Hive
2.導入依賴
<dependencies> <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec --> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>1.2.1</version> </dependency> </dependencies> |
3.建立一個類
package com.atguigu.hive; import org.apache.hadoop.hive.ql.exec.UDF;
public class Lower extends UDF {
public String evaluate (final String s) {
if (s == null) { return null; }
return s.toLowerCase(); } } |
4.打成jar包上傳到服務器/opt/module/jars/udf.jar
5.將jar包添加到hive的classpath
hive (default)> add jar /opt/module/datas/udf.jar;
6.建立臨時函數與開發好的java class關聯
hive (default)> create temporary function mylower as "com.atguigu.hive.Lower";
7.便可在hql中使用自定義的函數strip
hive (default)> select ename, mylower(ename) lowername from emp;