Usage Guide: External Function (Remote Function)
Document Objective
Through this usage guide, you will be able to:
- Call an offline Java NLP model (see GitHub) to analyze the sentiment of strings in Singdata Lakehouse tables
- Call Alibaba Cloud Vision Intelligence Open Platform services (see documentation) to parse image data pointed to by URLs in Singdata Lakehouse tables
(The environment used in this Best Practices guide is Singdata Lakehouse based on Alibaba Cloud.)
Operation Steps
Step 0: Preparation (Authorization)
The goal of this step is to allow the Singdata Lakehouse cluster to access the customer's Alibaba Cloud Function Compute (FC) and Object Storage Service (OSS). To accomplish this, you need to create a Role and let Singdata Lakehouse assume this Role to access Alibaba Cloud FC and OSS services.
1. Alibaba Cloud Console: Create a Permission Policy (e.g., CzUdfOssAccess) in Alibaba Cloud Access Control (RAM):
- Go to the Alibaba Cloud RAM Console.
- In the left navigation bar, go to Permission Management -> Permission Control. On the Permission Control page, click Create Permission Policy.
- On the Create Permission Policy page, select the Script Edit tab (replace the bucket name inside
[]below).
2. Alibaba Cloud Console: Create a Role in Alibaba Cloud RAM (e.g., CzUDFRole):
- In the RAM Console left navigation bar, go to Identity Management -> Roles, then click Create Role.
- On the Create Role page, select the type as Alibaba Cloud Account. In the role configuration, enter a custom Role Name (e.g., CzUDFRole). Under Select Trusted Cloud Account, select Other Cloud Account and enter: 1384322691904283 (the Singdata Lakehouse Shanghai primary cloud account), then click Finish.
- Edit the AliyunFCFullAccess permission policy and add the
"acs:Service": "fc.aliyuncs.com"section below.
- After creation, click Grant Permissions for Role:
- Under System Policies, grant the AliyunFCFullAccess policy to the role CzUDFRole.
- Under Custom Policies, grant the policy just created (CzUdfOssAccess) to the role.
3. On the CzUDFRole detail page, obtain the RoleARN for this role:
- Modify the Trust Policy of CzUDFRole:
Scenario 1: Call a Java NLP Offline Model
1. Write the Code
- Write a UDF based on the Hive UDF API. The following is sample code implementing uppercase conversion:
- Compile the code to generate a JAR package and other dependency files, then package them into a ZIP archive.
2. Upload the Function Package to the Specified Path
For example: oss://hz-oss-lakehouse/functions/sentiment/UDF_code/SentimentAnalysis.zip
Main class: com.singdata.nlp.GenericUDFSentiment
There are two ways to upload the file to the specified path:
- Upload directly via the OSS client.
- Use the PUT command in the Lakehouse JDBC client (PUT command via Lakehouse Web UI is not supported) to upload the package to a Volume object, and reference the Volume path in the function creation DDL. For example:
You can also specify an internal volume. Although you can use an internal volume, the code_bucket parameter in the API CONNECTION creation must be filled with an external address.
- User Volume format address:
volume:user://~/upper.jar-
userindicates the User Volume protocol. -
~indicates the current user, a fixed value. -
upper.jaris the target filename.
-
- Table Volume format address:
volume:table://table_name/upper.jartableindicates the Table Volume protocol.table_nameis the table name; fill in the actual name.upper.jaris the target filename.
3. Create a Connection
Parameter descriptions:
-
api_connection: Creates an API-type Connection for calling third-party service interfaces.
-
type: Connection type is cloud function:
cloud_function. Specific properties include:
- provider: Cloud function provider, e.g.,
aliyun - region: Region where the cloud function is located, e.g.,
cn-shanghai - role_arn: The Role assumed when creating the cloud function, e.g.,
acs:ram::12228000000000000:role/czudfrole - code_bucket: The object storage bucket name where the cloud function program files are located
4. Create an External Function in Lakehouse
Parameter descriptions:
- as: Followed by the main class name of the Java function.
- using: Only compiled Java programs are supported. Must be followed by the parameter archive (indicating the package is a ZIP-format file) or jar (indicating a Java JAR file). You can reference the OSS path of the file directly; if the file has been uploaded to a Volume object via the PUT command, you can also reference the function file via the Volume path, e.g.,
USING ARCHIVE 'volume://fc_volume/udfs/SentimentAnalysis.zip' - connection: The connection object used in the program, e.g.,
udf_sentiment_bj. Properties:
- remote.udf.api: For Java UDFs, fill in
java8.hive2.v0
5. Run Sentiment Analysis
Construct test data:
Run sentiment analysis:
Scenario 2: Python UDF — Call a Third-Party Visual Processing Platform API for Image Parsing
1. Code file: video_contents.py
import sys:
to_text = image_to_text():
for url in sys.argv[1:]:
print(f"{to_text.evaluate(url)}"):
2. Reuse the connection from Scenario 1
3. Create a New External Function
Parameter descriptions:
asis followed by the Python module name + main class name. If the main program file isvideo_contents.pyand the main class name isimage_to_text, the parameter afterasis'video_contents.image_to_text'.- using archive / file: Python files must be packaged as a ZIP-format file; single-file scripts are also supported using the
fileparameter.
-
connection: The connection object used in the program, e.g.,
udf_sentiment_bj. Properties:- remote.udf.api: For Python language functions, fill in
python3.mc.v0
- remote.udf.api: For Python language functions, fill in
4. Create Test Data to Verify
Import the following 4 images into OSS, generate public URLs, and store them in a Lakehouse table. You can use the following SQL directly to construct the test table:
Run the query:
