This topic describes how to create a Python function.
Prerequisites
You have created a business process. For more information, see Create and manage business processes.
Background information
In DataService Studio, you can create, test, and publish functions. After a function is published, you can associate it with an API. The function can act as a filter to process the request parameters or response data of the API. This enables complex queries and accommodates various business scenarios.
Limitations
-
You must purchase DataWorks Professional Edition or a more advanced edition to use Python functions and the filter feature.
-
Python functions must run in the shared resource group for DataService Studio.
-
Python functions are being refactored. You cannot create, clone, or publish new Python functions. If you need to use the function feature, we recommend using an Aviator function. For more information, see Create an Aviator function.
-
When you use a function as an API filter, both the pre-processing and the post-processing filter must be Python functions.
-
The following limitations apply when you use a Python function as a filter:
-
Filters support only Python 3.0 syntax.
-
Filters support only the following modules:
import json,time,random,pickle,re,math. -
Filters require the function signature
def handler(event,context):.
-
Function structure
# -*- coding: utf-8 -*-
# event (str): The API result if used as a filter, or your parameters in other cases.
# context: Contains environment information. This parameter is currently not in use.
# Supported import modules: json, time, random, pickle, re, math.
# Do not modify the function name.
import json
def handler(event,context):
# Load the string into a JSON object.
obj = json.loads(event)
# Add your code here.
# End of custom code.
return obj
You can modify this function template and change the input parameter names based on your requirements.
Parameter 1 [context]: Contains the API execution context. This parameter is not currently used.
Parameter 2 [event]: Contains the API request parameters or the result from the previous filter.
-
The system converts the API request parameters or the output from the previous filter into a JSON string and passes it to the event parameter. Each value in the JSON object must be a string.
-
A pre-processing filter outputs a key-value map that the subsequent SQL statement uses as an input parameter. Therefore, the map must have a depth of 1 (a flat map).
Go to DataService Studio
Log on to the DataWorks console. In the target region, click in the left-side navigation pane. Select a workspace from the drop-down list and click Go to DataService Studio.
Create a function
-
In the Service Development pane, hover over the
icon and click .Alternatively, find the target business process, right-click Function, and then select .
-
In the Create Python Function dialog box, configure the parameters.
Parameter
Description
Function Name
The function name. The name must be 256 characters or fewer.
Function Template
Only the Python3 Standard v1 template is supported.
Runtime Environment
Only Python 3.0 is supported.
Function Description
The function description. The description must be 512 characters or fewer.
Location
The folder where the function will be stored.
-
Click Confirm.
-
Configure the function on its editing page.
-
In the Edit Code section, enter the function code.
-
In the Environment Configuration section, set the Memory and Timeout.
-
-
Click the
Save icon in the toolbar.
Next steps
-
After you create the function, test and deploy it. For more information, see Test, deploy, and use a function and Deploy a function.
-
Associate the deployed function with an API and use it as a pre-filter or post-filter to process request parameters and returned results. For more information, see Use a function as an API filter.