UDF函数在Hive中的使用
文章目录
UDF介绍
官网:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
UDF: User-Defined Function用户自定义函数。
在Hive中,有很多的内置函数,它自带的,但是呢,就算有很多内置函数,它也满足不了众多业务逻辑的需求。所以呢,在工作中我们还是需要去自己开发UDF函数来实现我们想要的功能。
#执行命令会显示很多函数(不一定是内置的哦)
hive (default)> show functions;
#查看函数的简单说明
hive (default)> desc function substr;
#查看函数的更详细的说明(经常用)
hive (default)> desc function extended substr;
在Hive中,我们用Java来去开发。
在Hive里面。UDF分为三大类。
①UDF: one-to-one row mapping ,一行进来一行出去,比如:upper substr
②UDAF: Aggregation聚合,Many-to-one row mapping,多进一出,比如:sum/min
③UDTF: Table-generating one-to-many,一行进来多行出去,比如:lateral view explode()
Hive中的内置函数
要知道Hive中有哪些内置函数以及如何使用?
去hive官网wiki里去查找:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
比如这些:
Date Function
to_date(from_unixtime(unix_timestamp()))
unixtime(unix_timestamp())
cast cast(value as TYPE)
isnull
isnotnull
assert_true
current_database
assert_true
开发一个UDF函数
①创建一个普通的maven工程
然后确定完成,第一次建立这个项目的时候会比较慢,因为要下载很多包什么的。
②pom文件中添加hive的依赖
pom文件如下:主要添加properties、repositories、dependency三个地方。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ruozedata.hadoop</groupId>
<artifactId>g6-hadoop-UDF</artifactId>
<version>1.0</version>
<name>g6-hadoop-UDF</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
<hive.version>1.1.0-cdh5.7.0</hive.version>
</properties>
<!--添加CDH的仓库-->
<repositories>
<repository>
<id>nexus-aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--添加Hadoop的依赖-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<!--添加hive依赖-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${hive.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
要下载很多东西,下载依赖的时候会非常慢。
③开发UDF代码
在com.ruozedata.hadoop下面建个udf包,udf包下面建一个HelloUDF的Java类。
开发代码如下:
package com.ruozedata.hadoop.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
/**
* 需求:开发一个普通的UDF函数
* input输入是UDF
* 输出:Hello:UDF
*/
/**
* 要继承UDF类,里面要重写它的方法
*/
public class HelloUDF extends UDF {
public String evaluate(String input) {
return "hello:" + input;
}
}
public static void main(String[] args) {
HelloUDF udf = new HelloUDF();
String output = udf.evaluate("wsk");
System.out.println(output);
}
④打jar包
打包成功:
⑤jar包上传服务器
Hive中添加UDF函数
官网:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateFunction
创建UDF函数有三种方式
- 创建临时的UDF函数,只对当前session生效
- 创建永久的UDF函数
- 编译hive源码支持UDF函数,对所有部署编译后的hive生效。
创建临时的UDF函数
生产不推荐