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

Running the Example with AuraJobV2

This example demonstrates how to register a UDF with LakeFormation using the SDK, and then use the AuraJobV2 engine to read data columns from a specified database table and perform calculations using the UDF.

Prerequisites

Example

# 1. Import dependencies.
import os
from aidatalake_catalog import Parameter, Signature
from aidatalake_catalog.client import AIDatalake_Catalog_Client
from aura_frame.multimodal import ai_lake

# 2. Define a UDF with type annotations.
class Multiplier:
    """Class-defined UDF: Multiplies by a configurable factor (default: 2)."""
    def __init__(self, factor: int = 2):
        self._factor = factor
    def __call__(self, x: int) -> int:
        return x * self._factor

# 3. Define the function signature. (Optional. If not passed, the SDK automatically infers it based on the type annotations.)
sig = Signature(
    parameters=[Parameter(name="x", annotation=int)],
    return_annotation=int,
)
# 4. Initialize the Catalog client. (You are advised to read the AK/SK from environment variables to avoid hardcoding.)
client = AIDatalake_Catalog_Client(
    ak=os.getenv("XXX_SDK_AK"),
    sk=os.getenv("XXX_SDK_SK"),
    security_token=None,                       # Used for temporary credentials
    obs_endpoint="obs.cn-north-4.myhuaweicloud.com",
    lakeformation_endpoint="https://lakeformation.cn-north-4.myhuaweicloud.com",
    lakeformation_instanceid="your-instance-id",
    catalog_name="your-catalog-name",
    default_database="your_default_db",
    obs_path="your-bucket/udf/",              # bucket_name/dir/subdir
    project_id="your-project-id",
    ignore_ssl_verification=False,
)

# 5. Register the UDF.
func = client.create_udf(
    Multiplier,
    name="class_multiplier_def",
    signature=sig,
    description="Class-defined UDF: Multiplies by a factor (default: 2)."
)

# 6. Use the Aura engine to execute the function.
"""
|  id  | value|
|------|------|
|  1   |  10  |
|  2   |  20  |
|  3   |  30  |
"""
con = ai_lake.connect(
        aura_endpoint=os.getenv("aura_endpoint"),
        aura_endpoint_name=os.getenv("aura_endpoint_name"),
        aura_workspace_id=os.getenv("aura_workspace_id"),
        lf_catalog_name=os.getenv("lf_catalog_name"),
        access_key=os.getenv("access_key"),
        secret_key=os.getenv("secret_key"),
        default_database=target_database,
        use_single_cn_mode=True
)

ds = con.load_dataset("table_1")  # table_1 must be registered with LakeFormation in advance.
ds = ds.map(fn=f'{database}.class_multiplier_def', on=['value'], as_col='mul_result')
df = ds.execute().sort_values('id').reset_index(drop=True)

print(f"  {'ID':<5} {'Original value':<10} {'mul_result':<15}")
print("-" * 35)
for _, r in df.iterrows():
    print(f"{int(r['id']):<5} {int(r['value']):<10} {int(r['mul_result']):<15}")

Execution result:

  ID   Original value   mul_result
-----------------------------------
  1     10         20             
  2     20         40             
  3     30         60