系统内置函数
查看系统内置函数
hive> show functions;
显示内置函数用法
hive> desc function 函数名;
详细显示内置函数用法
hive> desc function extended 函数名;
自定义函数
自定义函数分为三个类别:
UDF(User Defined Function):一进一出
UDAF(User Defined Aggregation Function):聚集函数,多进一出(例如count/max/min)
UDTF(User Defined Table Generating Function):一进多出,如lateral view explode()
使用自定义函数
添加jar:add jar jar的路径;
创建函数:create [temporary] function [dbname.]函数名 AS 类全限定名;
删除函数:drop [temporary] function [if exists] [dbname.]函数名;
- 大概依赖如下
<properties>
<hive.version>1.1.0-cdh5.7.0</hive.version>
</properties>
<!-- 添加Hive依赖 -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${hive.version}</version>
</dependency>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
- 编写代码
编译-打包-上传
import org.apache.hadoop.hive.ql.exec.UDF;
// 继承UDF
public class HiveUDF extends UDF {
// 需要实现evaluate函数,支持重载
// 可以返回null,但不可以发挥void
public String evaluate(final String s) {
if (s == null) {
return null;
}
return s.toLowerCase();
}
}
添加函数
hive> add jar /home/user000/doc/hadoop-learning-1.0-SNAPSHOT.jar;
hive> create temporary function myLower as 'HiveUDF';测试函数
hive> select dept_no from emp_gender;
+----------+--+
| dept_no |
+----------+--+
| A |
| A |
| B |
| A |
| B |
| B |
+----------+--+
hive> select myLower(dept_no) from emp_gender;
+------+--+
| _c0 |
+------+--+
| a |
| a |
| b |
| a |
| b |
| b |
+------+--+删除函数
drop temporary function if exists myLower;