Create a Python function
This topic describes how to create a Python function.
Prerequisites
A business process has been created. For details, see Create and manage business processes.
Background information
In DataService Studio, you can create, test, and publish functions. You can then associate a published function with an API as a filter that processes the API's request parameters or response results. This supports complex API queries and lets you adapt to different business scenarios.
Limits
-
To use Python functions and filters, you must purchase DataWorks Professional Edition or later.
-
Python functions must run on a shared resource group for DataService Studio.
-
Python functions are being redesigned. You cannot currently create, clone, or publish new Python functions. If you need function capabilities, we recommend using Aviator functions. For details, see Create an Aviator function.
-
When you use functions as API filters, the pre-filter and post-filter must be of the same function type. Select Python functions for both. You cannot currently use different function types for the pre-filter and post-filter of the same API.
-
The following limits apply when a Python function is used as a filter:
-
The filter supports only Python 3.0 syntax.
-
The filter supports only
import json,time,random,pickle,re,math. -
The filter requires the function name and signature
def handler(event,context):.
-
Function structure
# -*- coding: utf-8 -*-
# event (str) : in filter it is the API result, in other cases, it is your param
# context : some environment information, temporarily useless
# import module limit: json,time,random,pickle,re,math
# do not modify function name
import json
def handler(event,context):
# load str to json object
obj = json.loads(event)
# add your code here
# end add
return obj
You can modify this function and rename its input parameters as needed.
Parameter 1 [context]: A string that contains the API execution context. It is currently empty and not in use.
Parameter 2 [event]: A string that contains the API request parameters or the output of the previous filter.
-
The system converts API request parameters or the previous filter's output from a key-value map to JSON and passes that JSON to the event parameter. Values in the JSON value field must be strings.
-
The pre-filter outputs a key-value map. Because this output is used as input to the SQL statement, only a key-value map with a depth of 1 is supported.
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
-
On the Service Development page, move the pointer over the
icon and click .Alternatively, find the business process, right-click Function, and select .
-
In the Create Python Function dialog box, configure the parameters.
Parameter
Description
Function Name
The name of the custom function. It can contain up to 256 characters.
Function Template
Only the Python3 Standard v1 template is supported.
Runtime Environment
Only Python 3.0 is supported.
Function Description
A brief description of the function. It can contain up to 512 characters.
Location
The directory where the function is stored.
-
Click Confirm.
-
Configure the function on the function editing page.
-
Enter the function code in the Edit Code area.
-
In the Environment Configuration area, set Memory and Timeout.
-
-
Click the
icon in the toolbar.
Next steps
-
After you create the function, you can test and deploy it. For more information, see Test, deploy, and use a function and Deploy a function.
-
After you deploy the function, you can associate it with an API and use it as a pre-filter or post-filter to process request parameters and response data. For more information, see Use a function as an API filter.