Hive3 detailed tutorial (IX) hive3 custom UDF function (idea Maven version)

Miss Zhu 2022-02-13 08:38:41 阅读数:165

hive3 hive detailed tutorial ix

because Hive Itself is Java Developed , So we can use it Java Define functions for Hive SQL Use .
Let's explore by defining a case of converting an input string into reverse output UDF Function customization .
stay idea Use in maven Development , The project structure is as follows :
 Insert picture description here
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>

Realize the inverse output of a string :

Java Code :

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;
}
}

Type as including dependencies jar package :
 Insert picture description here

Upload to linux In the virtual machine
 Insert picture description here

Add to hive in :

add jar /apps/testHive-v2.jar;

Set the function name , Associated class name

create temporary function my_revert_v2 as 'com.alan.RevertString';

test :

select my_revert_v2('abc');

 Insert picture description here

copyright:author[Miss Zhu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130838388216.html