A system built-in function
1) Check the system comes with a function hive> show functions;
2) a function of displaying the usage hive comes> desc function upper;
3) the use of a detailed display function comes hive> desc function extended upper;
II. Customizing functions
1. Category
1.1 User-defined functions are divided into the following three categories:
(1) UDF (User-Defined-Function) into a one
(2) UDAF (User-Defined Aggregation Function) aggregate function, into a plurality
Similar to: count / max / min
(3) UDTF (User-Defined Table-Generating Functions) a Multiple-Out
The lateral view explore ()
1.2 official document address
https://cwiki.apache.org/confluence/display/Hive/HivePlugins
1.3 Programming Steps
(1) inherited org.apache.hadoop.hive.ql.UDF
(2) the need to achieve evaluate function; evaluate support function overloading;
(3) create a function in hive command line window
a) 添加 jar add jar linux_jar_path
b)创建 function create [temporary] function [dbname.]function_name AS class_name;
(4) Delete function in the hive command line window
Drop [temporary] function [if exists] [dbname.]function_name;
Note: UDF must have a return type, can return null, but can not return type is void;
2. Custom UDF function
1) create a Maven project Hive
2) introducing dependent
<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) Create a class
package com.atguigu.hive; import org.apache.hadoop.hive.ql.exec.UDF; public class Lower extends UDF { public String evaluate (String s) { if (s == null) { return null; } return s.toLowerCase(); } }
4) labeled jar package uploaded to the server /opt/module/datas/udf.jar
5) adding the hive to jar package classpath hive (default)> add jar /opt/module/datas/udf.jar;
6) Create a temporary function associated with good java class and Development
hive (default)> create temporary function mylower as "com.atguigu.hive.Lower";
7) a function to use the custom in hql
hive (default)> select ename, mylower(ename) lowername from emp;
3. Custom UDTF function