因为Hive本身是Java开发的,所以我们可以使用Java定义函数供Hive SQL使用。
我们通过定义一个将输入字符串转换成反向输出的案例来探究UDF函数的自定义。
在idea中使用maven开发,项目结构如下:
pom.xml:
<?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">
<parent>
<artifactId>bigdata</artifactId>
<groupId>com.alan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>testHive</artifactId>
<name>testHive</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.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.alan.WordCountDriver</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
实现一个字符串的反转输出:
Java代码:
package com.alan;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
@SuppressWarnings("deprecation")
public class RevertString extends UDF {
public Text evaluate(Text line) {
if (null != line && !line.toString().equals("")) {
String str = line.toString();
char[] rs = new char[str.length()];
for (int i = str.length() - 1; i >= 0; i--) {
rs[str.length() - 1 - i] = str.charAt(i);
}
line.set(new String(rs));
return line;
}
line.set("");
return line;
}
}
打成包含依赖的jar包:
上传到linux虚拟机中
添加到hive中:
add jar /apps/testHive-v2.jar;
设置函数名,关联类名
create temporary function my_revert_v2 as 'com.alan.RevertString';
测试:
select my_revert_v2('abc');