Create Function
Function
The platform supports creating custom functions such as user-defined functions (UDFs) and user-defined table functions (UDTFs) for use in Spark job development.
Syntax
CREATE FUNCTION [db_name.]function_name AS class_name [USING resource,...] resource: : JAR file_uri
or
CREATE OR REPLACE FUNCTION [db_name.]function_name AS class_name [USING resource,...] resource: : JAR file_uri
Precautions
- If a function with the same name already exists in the database, the system reports an error.
- Only Hive syntax is supported for creating functions.
- Be aware of the following scenario: If a custom function F1 is created specifying class C1 and package JAR1, and another custom function F2 is created also specifying class C1 but package JAR2, the functions will conflict because F2 and F1 use the same class name, affecting job execution.
- The UDF hot-reload feature is in a restricted usage phase. To use it, submit a service ticket to request activation.
Keywords
- USING: The resources to be loaded. Can be a list of JARs, files, or URIs.
- OR REPLACE: Supports the custom function hot-reload feature.
- If OR REPLACE is not used when creating a custom function, pay attention to the following scenarios:
Table 1 Scenarios without OR REPLACE Scenario
Example
Effective Mechanism
Operational Impact
Scenario 1: The implementation logic of the original package class is modified. The JAR package name and class name specified for the newly created function remain the same as the original.
UDF F1 has been created under a Spark SQL endpoint, specifying class name C1 and JAR file name J1. The function implementation in the J1 package is later modified. A new function F2 is created, specifying class name C1 and JAR package name J1. Note: If step 2 continues to use the function name F1, creation will fail due to duplicate function name. In this case, consider using OR REPLACE, or replacing all occurrences of function F1 in jobs with F2.
The Spark SQL endpoint needs to be restarted for the newly created UDF F2 to take effect.
The Spark SQL endpoint needs to be restarted, affecting currently running jobs. After the endpoint is restarted, the original functionality of F1 is affected, and F1's functionality becomes the same as F2.
Scenario 2: A new class is added to the original package. The newly created function specifies the new class, and the package name remains unchanged.
UDF F1 has been created under a Spark SQL endpoint, specifying class name C1 and JAR file name J1. A new class C2 is added to the J1 JAR package, while C1 remains unchanged. A new UDF F2 is created, specifying class name C2 and JAR package name J1.
The Spark SQL endpoint needs to be restarted for the newly created UDF F2 to take effect.
The Spark SQL endpoint needs to be restarted, affecting currently running jobs. After the endpoint is restarted, the functionality of F1 remains unchanged.
Scenario 3: The implementation logic of the original package class remains unchanged, but the package is repackaged with a new name. The newly created function specifies the new JAR package name, and the class name remains unchanged.
UDF F1 has been created under a Spark SQL endpoint, specifying class name C1 and JAR file name J1. The JAR package is repackaged as J2 with unchanged logic. A new UDF F2 is created, specifying class name C1 and JAR package name J2.
The newly created UDF F2 takes effect immediately.
No impact.
- If OR REPLACE is used when creating a custom function, it indicates that the existing function content needs to be functionally replaced and take effect in real-time. This feature is in a restricted usage phase. To use it, submit a service ticket to request activation.
- To make it take effect immediately on all Spark SQL endpoints, you need to execute CREATE OR REPLACE xxx FUNCTION ... on each Spark SQL endpoint separately. Otherwise, endpoints where it was not executed may experience a delay of 0–12 hours before taking effect.
- If a running job uses UDF F1, which specifies class name C1 and package name J1, and after the job has run halfway, the logic of the J1 package is modified and CREATE OR REPLACE FUNCTION F1 is executed, subsequent jobs will use the new F1 function logic, but the job that has already executed halfway will use the old function logic.
Table 2 Scenarios with OR REPLACE Scenario
Example
Effective Mechanism
Operational Impact
Scenario 1: The implementation logic of the original package class is modified. The JAR package name and class name specified for the newly created function remain the same as the original.
UDF F1 has been created under a Spark SQL endpoint, specifying class name C1 and JAR file name J1. The function implementation in the J1 package is later modified. A new function F1 is created, specifying class name C1 and JAR package name J1.
Takes effect immediately.
The functionality of F1 becomes the newly modified logic.
Scenario 2: A new class is added to the original package. The newly created function specifies the new class, and the package name remains unchanged.
UDF F1 has been created under a Spark SQL endpoint, specifying class name C1 and JAR file name J1. A new class C2 is added to the J1 JAR package, while C1 remains unchanged. A new UDF F2 is created, specifying class name C2 and JAR package name J1. Since this is the first time creating a function specifying C2, function F2 cannot take effect immediately.
Two ways to take effect: 1. Execute the CREATE OR REPLACE FUNCTION F2 statement again. 2. Restart the Spark SQL endpoint.
If the Spark SQL endpoint is restarted, currently running jobs will be affected.
Scenario 3: The class name in the original package is modified. The newly created function specifies the new class name, and the package name remains unchanged.
UDF F1 has been created under a Spark SQL endpoint, specifying class name C1 and JAR file name J1. The original C1 in the J1 JAR package is modified to C2, and C1 no longer exists. A new UDF F2 is created, specifying class name C2 and JAR package name J1.
Takes effect immediately.
The functionality of F1 becomes invalid, and the functionality of F2 takes effect immediately.
Scenario 4: The implementation logic of the original package class remains unchanged, but the package is repackaged with a new name. The newly created function specifies the new JAR package name, and the class name remains unchanged.
UDF F1 has been created under a Spark SQL endpoint, specifying class name C1 and JAR file name J1. The JAR package is repackaged as J2 with unchanged logic. A new UDF F2 is created, specifying class name C1 and JAR package name J2.
Takes effect immediately.
No impact.
- If OR REPLACE is not used when creating a custom function, pay attention to the following scenarios:
Examples
Create the function mergeBill.
CREATE FUNCTION mergeBill AS 'com.xxx.hiveudf.MergeBill' using jar 'obs://onlyci-7/udf/MergeBill.jar';
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