Hive UDFs for Interconnecting with HetuEngine
You can customize functions to extend SQL statements to meet personalized requirements. These functions are called user-defined functions (UDFs).
This section describes how to develop and use Hive UDFs.
The development must be based on JDK 21.0.2 or later for MRS 3.2.1 or later. This topic uses MRS 3.6.0 as an example.
Developing Hive UDFs
This sample implements one Hive UDF described in the following table.
| Parameter | Description |
|---|---|
| AutoAddOne | Adds 1 to the input value and returns the result. |
- A common Hive UDF must be inherited from org.apache.hadoop.hive.ql.exec.UDF.
- A common Hive UDF must implement at least one evaluate(). The evaluate function supports overloading.
- Currently, only the following data types are supported:
- boolean, byte, short, int, long, float, and double
- Boolean, Byte, Short, Int, Long, Float, and Double
- List and Map
UDFs, UDAFs, and UDTFs currently do not support complex data types other than the preceding ones.
- Currently, Hive UDFs support only five or fewer input parameters. UDFs with more than five input parameters will fail during registration.
- If a Hive UDF input parameter is null, the call returns null immediately without executing the UDF logic. Consequently, the UDF result may be inconsistent with the standard Hive execution result.
- To add the hive-exec-3.1.1 dependency package to your Maven project, you can obtain the package from the Hive installation directory.
- (Optional) If the Hive UDF depends on a configuration file, you are advised to save the configuration file as a resource file in the resources directory so that it can be included in the Hive UDF function package.
- Create a Maven project. Set groupId to com.test.udf and artifactId to udf-test. The two values can be customized based on the site requirements.
- Modify the pom.xml file as follows:
<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.test.udf</groupId> <artifactId>udf-test</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>3.1.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/</outputDirectory> <resources> <resource> <directory>src/main/resources/</directory> <filtering>false</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> - Create the implementation class for the Hive UDF.
import org.apache.hadoop.hive.ql.exec.UDF; /** * AutoAddOne * * @since 2020-08-24 */ public class AutoAddOne extends UDF { public int evaluate(int data) { return data + 1; } } - Package the Maven project. The udf-test-0.0.1-SNAPSHOT.jar file in the target directory is the Hive UDF function package.
You need to pack all dependencies into a JAR package.
Configuring Hive UDFs
In configuration file udf.properties, add registration information in the "Function_name Class_path" format to each line to register the Hive UDF.
The following is an example of registering four Hive UDFs in the udf.properties configuration file:
booleanudf io.hetu.core.hive.dynamicfunctions.examples.udf.BooleanUDF shortudf io.hetu.core.hive.dynamicfunctions.examples.udf.ShortUDF byteudf io.hetu.core.hive.dynamicfunctions.examples.udf.ByteUDF intudf io.hetu.core.hive.dynamicfunctions.examples.udf.IntUDF
- If the added Hive UDF registration information is incorrect, for example, an invalid format or a non-existent class path, the system ignores the information and records a corresponding log.
- If duplicate Hive UDFs are registered, the system performs only one registration and ignores all subsequent duplicates.
- If a Hive UDF being registered matches one already registered in the system, the system throws an exception and fails to start. To resolve this issue, you need to delete the Hive UDF registration information.
Deploying Hive UDFs
To use an existing Hive UDF in HetuEngine, you need to upload the UDF function package, udf.properties file, and any required UDF configuration files to the specified HDFS directory, for example, /user/hetuserver/udf/, and then restart the HetuEngine compute instance.
- Create the /user/hetuserver/udf/data/externalFunctions directory, save the udf.properties file in the /user/hetuserver/udf directory, save the UDF function package in the /user/hetuserver/udf/data/externalFunctions directory, and save any required UDF configuration files in the /user/hetuserver/udf/data directory.
- Upload the files on the HDFS page.
- Log in to FusionInsight Manager as a HetuEngine user and choose Cluster > Services > HDFS.
- In the Basic Information area on the Dashboard page, click the link next to NameNode WebUI.
- Choose Utilities > Browse the file system and click
to create the /user/hetuserver/udf/data/externalFunctions directory. - Go to the /user/hetuserver/udf directory and click
to upload the udf.properties file. - Go to the /user/hetuserver/udf/data/ directory and click
to upload the required UDF configuration files. - Go to the /user/hetuserver/udf/data/externalFunctions directory and click
to upload the UDF function package.
- Use the HDFS CLI to upload the files.
- Log in to the node where the HDFS service client is located and switch to the client installation directory, for example, /opt/client.
cd /opt/client
- Run the following command to configure environment variables:
- If the cluster is in security mode, run the following command to authenticate the user. Skip user authentication for the cluster in normal mode.
Enter the password as prompted.
- Run the following commands to create directories and upload the prepared UDF function package, the udf.properties file, and all required UDF configuration files to the target directories:
hdfs dfs -mkdir -p /user/hetuserver/udf/data/externalFunctions
hdfs dfs -put ./Required UDF configuration files /user/hetuserver/udf/data
hdfs dfs -put ./udf.properties/user/hetuserver/udf
hdfs dfs -put ./UDF function package/user/hetuserver/udf/data/externalFunctions
- Log in to the node where the HDFS service client is located and switch to the client installation directory, for example, /opt/client.
- Upload the files on the HDFS page.
- Restart the HetuEngine compute instance.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot