Updated on 2026-11-06 GMT+08:00

Directly Using a Registered Scalar UDF

When a UDF has already been registered in the database by another user or script, you can directly retrieve the registered UDF operator via get_function and call it in the DataFrame without needing to re-register it.

Scenario

In collaborative team development, UDF registration and usage are often separated: some team members are responsible for writing and registering UDFs, while others are responsible for calling the registered UDFs in the data processing workflows. This example demonstrates how to view the list of registered UDFs in the database, retrieve a specified UDF operator, and directly use it in DataFrame expressions.

Notes and Constraints

  • Ensure that the target UDF is already registered in the specified database before use. Function names are stored in lowercase in the database.
  • Both scalar UDFs and class UDFs are called using the map method, with the on parameter passing the input columns of __call__.
  • For class UDFs, __init__ parameters are passed via keyword arguments in the map method (such as model_file and eos).

Prerequisites

Example

This example demonstrates how to view the list of registered UDFs in the database, retrieve the registered scalar UDF and class UDF operators, and directly call them in Aura DataFrame without re-registration.

  1. Connect to the Aura service and set the UDF workspace.
  2. View the list of registered UDF names in the target database.
  3. If calc_final_price exists, retrieve the scalar UDF operator, load the sales_line_items dataset, and calculate the final_price column using the map method.
  4. If spmanager exists, retrieve the class UDF operator, load the text_samples dataset, and perform tokenization on the content column using the map method (passing the model_file and eos parameters).
  5. Disable the Aura connection.
import os
from aura_frame.multimodal import ai_lake

target_database = "test"  #Create a database in LakeFormation, for example, test. For details, see Creating a DataArts Lake Formation Instance and Planning Metadata.
#Connect to the AuraJobV2 endpoint. For details about the parameters, see Establishing a Connection with the AuraJobV2 Endpoint.
con = ai_lake.connect(
    aura_endpoint=os.getenv("aura_endpoint"),         # Endpoint where the AuraJobV2 endpoint is located
    aura_endpoint_id=os.getenv("aura_endpoint_id"),    # AuraJobV2 endpoint ID
    aura_endpoint_name=os.getenv("aura_endpoint_name"),# AuraJobV2 endpoint name
    aura_workspace_id=os.getenv("aura_workspace_id"),  # Workspace ID
    lf_catalog_name=os.getenv("lf_catalog_name"),      # LakeFormation Catalog name
    access_key=os.getenv("access_key"),                # Access Key ID (AK)
    secret_key=os.getenv("secret_key"),                # Secret Key (SK)
    default_database=target_database,                       # LakeFormation database name
    use_single_cn_mode=True,   # Whether to enable the single-CN mode
    logging_level=logging.INFO # Set the log level.
)
#Configure the UDF deployment workspace. For details about the parameters, see Configuring the UDF Deployment Workspace.
con.set_function_staging_workspace(
    obs_directory_base=os.getenv("obs_directory_base"),  # Path for storing UDFs in the parallel file system.
    obs_bucket_name=os.getenv("obs_bucket_name"),       # Name of the parallel file system.
    obs_server=os.getenv("obs_server"),                  # OBS endpoint
    access_key=os.getenv("access_key"),                  # Access Key ID (AK)
    secret_key=os.getenv("secret_key"),                  # Secret Key (SK)
)
try:
    # View the names of all registered UDFs.
    udf_names = con._conn.udf.names(database=target_database)
    print(f"Registered UDFs: {udf_names}")
    if "calc_final_price" in udf_names:
        # Retrieve the registered scalar UDFs.
        calc_final_price_udf = con.get_function("calc_final_price", database=target_database)
        ds = con.load_dataset("sales_line_items", database=target_database)
        # Directly use the registered UDFs in the DataFrame.
        ds = ds.map(
            fn=calc_final_price_udf,
            on=[ds.unit_price, ds.discount_rate, ds.tax_rate],
            as_col="final_price",
        )
        ds = ds.select_columns(ds.unit_price, ds.discount_rate, ds.tax_rate, ds.final_price)
        df = ds.execute()
        print(df)
    if "spmanager" in udf_names:
        # Retrieve the registered class UDFs.
        sp_udf = con.get_function("spmanager", database=target_database)
        ds = con.load_dataset("text_samples", database=target_database)
        # Perform tokenization on the content column via the map method.
        ds = ds.map(
            fn=sp_udf, on=[ds.content], as_col="pieces_column",
            model_file="spiece.model", eos=True,
        )
        ds = ds.select_columns(ds.content, ds.pieces_column)
        df = ds.execute()
        print(df)
finally:
    con.close()