El contenido no se encuentra disponible en el idioma seleccionado. Estamos trabajando continuamente para agregar más idiomas. Gracias por su apoyo.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Creating a Function

Updated on 2024-07-04 GMT+08:00

Function

DLI allows you to create and use user-defined functions (UDF) and user-defined table functions (UDTF) in Spark jobs.

For details about the custom functions, see Calling UDFs in Spark SQL Jobs and Calling UDTFs in Spark SQL Jobs.

Syntax

1
2
3
4
5
CREATE FUNCTION [db_name.]function_name AS class_name
  [USING resource,...]

resource: 
  : JAR file_uri

Or

1
2
3
4
5
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 exists in the database, the system reports an error.
  • Only the Hive syntax can be used to create functions.
  • If you specify the same class name for two UDFs, the functions conflict though the package names are different. Avoid this problem because it causes failure of job execution.
  • To enable UDF hot loading, submit a service ticket to request whitelisting of your account.

Keywords

  • USING <resources>: resources to be loaded. It can be a list of JARs, files, or URIs.
  • OR REPLACE: Whether to reload function resources
    • The following table describes scenarios where you do not need OR REPLACE.
      Table 1 Scenarios where OR REPLACE is not specified

      No.

      Description

      Examples

      Take Effect When

      Operation Impact

      1

      You modify the implementation of the original class and specify the original JAR package name and class name for a new function.

      1. UDF F1 created for a Spark SQL queue is contained in C1 in the JAR package J1.
      2. You modify the implementation of function in the J1 package, save the function as UDF F2, and specify its class as C1 and the JAR package as J1.
        NOTE:

        Note that if UDF names conflict, the function will fail to be created. In this case, you can use OR REPLACE to replace F1 in all jobs with F2.

      The Spark SQL queue is restarted.

      1. Running jobs are interrupted because the Spark SQL queue must be restarted.
      2. After the queue is restarted, original UDF F1 becomes the same as F2.

      2

      You create a new class in the original package, specify the new class to the UDF you created, and retain the original package name.

      1. UDF F1 created for a Spark SQL queue is contained in C1 in the JAR package J1.
      2. You add class C2 to the JAR package J1. C1 remains unchanged. You create UDF F2 and specify its class name to C2 and its JAR package to J1.

      The Spark SQL queue is restarted.

      1. Running jobs are interrupted because the Spark SQL queue must be restarted.
      2. After the queue is restarted, F1 remains unchanged.

      3

      You keep the implementation of the original functions unchanged, and repack the program. You specify a new JAR package for the new function and retain the original class name.

      1. UDF F1 created for a Spark SQL queue is contained in C1 in the JAR package J1.
      2. You repack the JAR package as J2. The function logic remains unchanged. You specify the class of the created UDF to C1, and the JAR package name to J2.

      UDF F2 is created.

      None

    • If OR REPLACE is used when you create a UDF, the existing function will be replaced and the replacement takes effect immediately.
      CAUTION:
      • To use the OR REPLACE keyword, you need to submit a service ticket.
      • To make the replacement take effect immediately on all SQL queues, run the statement for each SQL queue. The replacement may take effect 0 to 12 hours later on the queues where the statement is not executed.
      • If you modify the J1 package (for example, CREATE OR REPLACE FUNCTION F1) in the middle of job execution, jobs being executed run the original UDF F1 (of the C1 class in the J1 package) and jobs that starts after the modification run the new F1 logic.
      Table 2 Scenarios where OR REPLACE is used

      No.

      Description

      Examples

      Take Effect When

      Operation Impact

      1

      You modify the implementation of the original class and specify the original JAR package name and class name for a new function.

      1. UDF F1 created for a Spark SQL queue is contained in C1 in the JAR package J1.
      2. You modify the implementation of a function in the J1 package, save the function as UDF F1, and specify its class as C1 and the JAR package as J1.

      Immediately

      F1 is changed.

      2

      You create a new class in the original package, specify the new class to the UDF you created, and retain the original package name.

      1. UDF F1 created for a Spark SQL queue is contained in C1 in the JAR package J1.
      2. You add class C2 to the JAR package J1. C1 remains unchanged. You create UDF F2 and specify its class name to C2 and its JAR package to J1. F2 does not take effect immediately because it is the first function specified to the C2 class.

      Either of the following happens:

      • CREATE OR REPLACE F2 FUNCTION is executed again.
      • The Spark SQL queue is restarted.

      If you restart the Spark SQL queue, running jobs will be affected.

      3

      You change the class name and specify the new class name to the new UDF. The package name remains unchanged.

      1. UDF F1 created for a Spark SQL queue is contained in C1 in the JAR package J1.
      2. You change C1 in the JAR package of J1 to C2, create the UDF F2, specify its class name to C2, and its JAR package name to J1.

      Immediately

      F1 is replaced by F2 immediately.

      4

      You keep the implementation of the original functions unchanged, and repack the program. You specify a new JAR package for the new function and retain the original class name.

      1. UDF F1 created for a Spark SQL queue is contained in C1 in the JAR package J1.
      2. You repack the JAR package as J2. The function logic remains unchanged. You specify the class of the created UDF to C1, and the JAR package name to J2.

      Immediately

      None

Example

Create the mergeBill function.

1
2
CREATE FUNCTION mergeBill AS 'com.xxx.hiveudf.MergeBill'
  using jar 'obs://onlyci-7/udf/MergeBill.jar';

Utilizamos cookies para mejorar nuestro sitio y tu experiencia. Al continuar navegando en nuestro sitio, tú aceptas nuestra política de cookies. Descubre más

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback