Calling UDAFs in Spark SQL Jobs
Scenario
AI DataLake allows you to use a Hive User Defined Aggregation Function (UDAF) to process multiple rows of data. Hive UDAF is usually used together with groupBy. It is similar to SUM() and AVG() functions commonly used in SQL.
Constraints
If you use a static class or interface in a UDF, add try catch to capture exceptions. Otherwise, package conflicts may occur.
Environment Preparations
Before you start, set up the development environment.
| Preparation Item | Description |
|---|---|
| Operating system (OS) | Windows 7 or a later version is required. |
| JDK | JDK 1.8 is required. For details, visit Java Downloads. |
| IntelliJ IDEA | IntelliJ IDEA 2019.1 or later is required. |
| Maven | Basic configurations of the development environment. For details about how to get started, see Downloading Apache Maven and Installing Apache Maven. Maven is used for project management throughout the lifecycle of software development. |
Development Process
The UDAF development process is as follows:
| No. | Step | Operation Interface | Description |
|---|---|---|---|
| 1 | Create a Maven project and configure the POM file. | IntelliJ IDEA | Write the UDAF code by referring to the Procedure. |
| 2 | Write the UDAF code. | ||
| 3 | Debug, compile, and pack the code into a JAR package. | ||
| 4 | Upload the JAR package to OBS. | OBS console | Upload the UDAF JAR file to an OBS path. |
| 5 | Create the UDAF. | DataArts Studio console | Create a UDAF on the Develop Script page of the DataArts Studio console. |
| 6 | Verify and use the UDAF. | DataArts Studio console | On the Develop Script page of the DataArts Studio console, use the created UDAF in a Spark SQL job. |
Procedure
- Create a Maven project and configure the POM file. This step uses IntelliJ IDEA 2020.2 as an example.
- Start IntelliJ IDEA and choose File > New > Project. Figure 2 Creating a project
- Choose Maven, set Project SDK to 1.8, and click Next. Figure 3 Configuring the project SDK
- Specify the project name and the project path, and click Create. In the displayed page, click Finish. Figure 4 Setting project information
- Add the following content to the pom.xml file.
<dependencies> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>1.2.1</version> </dependency> </dependencies>Figure 5 Adding configurations to the POM file
- Choose src > main and right-click the java folder. Choose New > Package to create a package and a class file. Set Package as required. In this example, set Package to com.aidatalake.demo.Figure 6 Creating a package
Create a Java class file in the package path. In this example, the Java Class file is AvgFilterUDAFDemo.
Figure 7 Creating a class
- Start IntelliJ IDEA and choose File > New > Project.
- Write the UDAF code. Pay attention to the following requirements when you implement the UDAF:
- The UDAF class must inherit from org.apache.hadoop.hive.ql.exec.UDAF and org.apache.hadoop.hive.ql.exec.UDAFEvaluator classes. The function class must inherit from the UDAF class, and the Evaluator class must implement the UDAFEvaluator interface.
- The Evaluator class must implement the init, iterate, terminatePartial, merge, and terminate functions of UDAFEvaluator.
- The init function overrides the init function of the UDAFEvaluator interface.
- The iterate function receives input parameters for internal iteration.
- The terminatePartial function has no parameter. It returns the data obtained after the iterate traversal is complete. terminatePartial is similar to Hadoop Combiner.
- The merge function receives the return values of terminatePartial.
- The terminate function returns the aggregated result.
For details about how to implement the UDAF, see the following sample code:
package com.aidatalake.demo; import org.apache.hadoop.hive.ql.exec.UDAF; import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; /*** * @jdk jdk1.8.0 * @version 1.0 ***/ public class AvgFilterUDAFDemo extends UDAF { /** * Defines the static inner class AvgFilter. */ public static class PartialResult { public Long sum; } public static class VarianceEvaluator implements UDAFEvaluator { // Initializes the PartialResult object. private AvgFilterUDAFDemo.PartialResult partial; // Declares a VarianceEvaluator constructor that has no parameters. public VarianceEvaluator(){ this.partial = new AvgFilterUDAFDemo.PartialResult(); init(); } /** * Initializes the UDAF, which is similar to a constructor. */ @Override public void init() { // Sets the initial value of sum. this.partial.sum = 0L; } /** * Receives input parameters for internal iteration. * @param x * @return */ public void iterate(Long x) { if (x == null) { return; } AvgFilterUDAFDemo.PartialResult tmp9_6 = this.partial; tmp9_6.sum = tmp9_6.sum | x; } /** * Returns the data obtained after the iterate traversal is complete. * terminatePartial is similar to Hadoop Combiner. * @return */ public AvgFilterUDAFDemo.PartialResult terminatePartial() { return this.partial; } /** * Receives the return values of terminatePartial and merges the data. * @param * @return */ public void merge(AvgFilterUDAFDemo.PartialResult pr) { if (pr == null) { return; } AvgFilterUDAFDemo.PartialResult tmp9_6 = this.partial; tmp9_6.sum = tmp9_6.sum | pr.sum; } /** * Returns the aggregated result. * @return */ public Long terminate() { if (this.partial.sum == null) { return 0L; } return this.partial.sum; } } }Figure 8 Writing UDAF code
- Use IntelliJ IDEA to compile the code and pack it into the JAR package.
- Click Maven in the tool bar on the right, and click clean and compile to compile the code.
After the compilation is successful, click package.
Figure 9 Exporting the JAR file
- The generated JAR package is stored in the target directory. In this example, the generated file is named MyUDAF-1.0-SNAPSHOT.jar and stored in the D:\AIDataLakeTest\MyUDAF\target directory.
- Click Maven in the tool bar on the right, and click clean and compile to compile the code.
- Log in to the OBS console and upload the JAR file to the OBS path.
The region of the OBS bucket to which the JAR file is uploaded must be the same as the region of the AI DataLake workspace. Cross-region operations are not allowed.
- Create the UDAF.
- On the AI DataLake console, click DataArts Studio in the lower left corner.
- On the Develop Script page, create a Serverless Spark SQL script and select the endpoint, catalog, and database. Figure 10 Creating a Serverless Spark SQL script
- In the SQL editing area, enter the following command for creating the UDAF:
CREATE FUNCTION AvgFilterUDAFDemo AS 'com.aidatalake.demo.AvgFilterUDAFDemo' using jar 'obs://aidatalake-test-obs01/MyUDAF-1.0-SNAPSHOT.jar';
orCREATE OR REPLACE FUNCTION AvgFilterUDAFDemo AS 'com.aidatalake.demo.AvgFilterUDAFDemo' using jar 'obs://aidatalake-test-obs01/MyUDAF-1.0-SNAPSHOT.jar';
If the reloading function of the UDAF is enabled, the create statement changes.
- Click Execute.
- Use the UDAF.
Use the UDAF in the SELECT statement as follows:
select AvgFilterUDAFDemo(real_stock_rate) AS show_rate FROM dw_ad_estimate_real_stock_rate LIMIT 1000;
- (Optional) Delete the UDAF.
If the UDAF is no longer used, run the following statement to delete it:
Drop FUNCTION AvgFilterUDAFDemo;
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