DLI allows you to use Hive user-defined functions (UDFs) to query data. UDFs take effect only on a single row of data and are applicable to inserting and deleting a single data record.
Log in to the DLI console and choose Data Management > Package Management. On the displayed page, select your UDF Jar package and click Manage Permissions in the Operation column. On the permission management page, click Grant Permission in the upper right corner and select the required permissions.
Before you start, set up the development environment.
Item |
Description |
---|---|
OS |
Windows 7 or later |
JDK |
JDK 1.8. |
IntelliJ IDEA |
This tool is used for application development. The version of the tool must be 2019.1 or other compatible versions. |
Maven |
Basic configurations of the development environment. Maven is used for project management throughout the lifecycle of software development. |
No. |
Phase |
Software Portal |
Description |
---|---|---|---|
1 |
Create a Maven project and configure the POM file. |
IntelliJ IDEA |
Write UDF code by referring the steps in Procedure. |
2 |
Write UDF code. |
||
3 |
Debug, compile, and pack the code into a Jar package. |
||
4 |
Upload the Jar package to OBS. |
OBS console |
Upload the UDF Jar file to an OBS directory. |
5 |
Create the UDF on DLI. |
DLI console |
Create a UDF on the SQL job management page of the DLI console. |
6 |
Verify and use the UDF on DLI. |
DLI console |
Use the UDF in your DLI job. |
<dependencies> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>1.2.1</version> </dependency> </dependencies>
Set the package name as you need. Then, press Enter.
Create a Java Class file in the package path. In this example, the Java Class file is SumUdfDemo.
For details about how to implement the UDF, see the following sample code:
package com.demo; import org.apache.hadoop.hive.ql.exec.UDF; public class SumUdfDemo extends UDF { public int evaluate(int a, int b) { return a + b; } }
After the compilation is successful, click package.
The generated JAR package is stored in the target directory. In this example, MyUDF-1.0-SNAPSHOT.jar is stored in D:\DLITest\MyUDF\target.
The region of the OBS bucket to which the Jar package is uploaded must be the same as the region of the DLI queue. Cross-region operations are not allowed.
CREATE FUNCTION TestSumUDF AS 'com.demo.SumUdfDemo' using jar 'obs://dli-test-obs01/MyUDF-1.0-SNAPSHOT.jar';
Use the UDF created in 6 in the SELECT statement as follows:
select TestSumUDF(1,2);
If the UDF is no longer used, run the following statement to delete it:
Drop FUNCTION TestSumUDF;