In DataService Studio, a workflow is represented as a directed acyclic gragh (DAG). By dragging and dropping nodes to a DAG, you can arrange APIs and functions in a serial, parallel, or branch structure based on the business logic. The whole workflow is a composite API service. The workflow feature is also called the service orchestration feature. This topic describes the benefits of workflows and how to use workflows.

When you run a workflow to call APIs, DataWorks runs the nodes in the workflow in sequence, passes parameters among the nodes, and automatically changes the status of each node. The workflow feature simplifies the process of calling multiple APIs or functions and reduces the cost of development and maintenance. This enables you to focus on business development.
Note Currently, the workflow feature is only supported by DataWorks Enterprise Edition in the China (Shanghai) region. For more information about DataWorks Enterprise Edition, see DataWorks advanced editions.

Benefits

  • Reduced cost of combining multiple APIs

    By dragging and dropping nodes to a DAG, you can arrange APIs and functions in a serial, parallel, or branch structure without writing code. This reduces the cost of combining multiple APIs.

  • Higher performance in calling APIs and functions

    A workflow allows you to call multiple APIs and functions in a container. Compared with writing code to combine APIs and functions, the workflow feature reduces the latency of calling APIs and functions.

  • Serverless architecture

    The workflow feature adopts a serverless architecture. A serverless architecture supports automatic resource scaling based on business needs. You can focus on the business logic, without worrying about the runtime environment.

Obtain values of request and response parameters

DataService Studio uses JSONPath to obtain parameter values. JSONPath is a query language that allows you to extract data from JSON files. You can use JSONPath expressions to pass parameters in DataService Studio.

For example, three nodes are run in the following order: A, B, and then C. Node C needs to use the response parameters of nodes A and B.
  • Response parameter of node A: {"namea":"valuea"}

    Expression for obtaining the value of the response parameter of node A: ${A.namea}

  • Response parameter of node B: {"nameb":"valueb"}

    Expression for obtaining the value of the response parameter of node B: $.nameb or ${B.nameb}

The built-in start node provides request parameters for the whole workflow. Assume that a request parameter of a workflow is {"namewf":"valuewf"}. All nodes of the workflow can obtain the value of the request parameter through the ${START.namewf} expression.

Set parameters

Request parameters:
  • If you do not specify a value for a request parameter of a node, DataService Studio obtains the value of the same parameter in the first layer of the JSON string returned by the parent node, and assigns the value to the request parameter. If no value is specified for a request parameter of the first node, DataService Studio obtains the value of the same parameter in the request parameters of the workflow.
  • If you specify a value for a request parameter, DataService Studio uses the value that you set.
  • If you want to use the value of the specified parameter returned by the specified ancestor node, obtain the value through a JSONPath expression.
Common JSONPath expressions for obtaining parameter values:
  • $.: obtains response parameters of the parent node.
  • $.param: obtains the value of the param parameter returned by the parent node. To allow you to obtain response parameters of any ancestor nodes, DataService Studio enhances JSONPath expressions.
  • ${NODEID1}: obtains response parameters of the node whose ID is NODEID1.
  • ${START}: obtains request parameters of the workflow, that is, response parameters of the start node.
  • ${NODEID1.param}: obtains the value of the param parameter returned by the node whose ID is NODEID1.
JSONPath expressions for setting response parameters of a node:
  • $.: obtains response parameters of the current node.
  • $.param: obtains the value of the param parameter returned by the current node.
  • ${NODEID1.param}: obtains the value of the param parameter returned by the node whose ID is NODEID1.

Example

Configure the connection before you create and use a workflow. For more information, see Configure the Data Source. In this example, a MySQL connection is used.
  1. Register an API.

    In this example, register an API to generate a new API. For more information, see Register API.

  2. Create a function.

    In this example, create a Python function as the branch node to process the result data of the parent node.

    The code of the function is as follows.
    # -*- 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
    import json
    def handler(event,context):
        # load str to json object
        obj = json.loads(event)
        # add your code here
        # end add
        return obj
  3. On the left-side navigation submenu of the DataService Studio page, click the Service Development icon. On the Service Development tab, move the pointer over the Create icon and select Workflow.
  4. In the New Workflow dialog box that appears, set relevant parameters. For example:
    • Select HTTP and HTTPS for the Protocol parameter.
    • Select GET or POST from the Request Method drop-down list.
    • Select JSON from the Response Content drop-down list.
  5. After the workflow is created, drag and drop nodes to the DAG and connect the nodes, as shown in the following figure.
  6. Double-click the API1 node to edit the node. Select the API that you registered earlier as the API to be called in the node.

    Select set output results and enter {"user_id":"$.data[0].id"}.Use JSONPath expressions to set response parameters. The syntax for obtaining the value of a parameter is ${NodeA.namea}, which is the same as the for setting request parameters. {" user_id":"$.data[0].id"} assigns the value of the id parameter of the first element in thedata array to the user_id parameter. Then, the API1 node returns {"user_id":"value"} in JSON format.

  7. Double-click the PYTHON1 node to edit the node. Select the function that you created earlier as the function to be called in this node.
  8. Double-click the SWITCH2 node to edit the node. On the right-side pane that appears, click Set branch conditions. In the Set branch conditions dialog box that appears, enter conditional expressions based on the response parameter of the parent node. For example, you can enter expressions in the ${Node ID. Parameter}>1 or $. Parameter>1 format. Conditional expressions support the following operators: ==, ! =, >=, >, <=, <, &&, !, (), +, -, *, /, and %.

    In this example, the user_id parameter is the response parameter of the API1 node and is used as the request parameter of the SWITCH2 node.

    Branch node 1: $.user_id != 1, indicating that the branch node 1 is run if the value of the user_id parameter is not 1.
    Branch node 2: $.user_id == 1, indicating that the branch node 2 is run if the output value of the user_id parameter is 1.
  9. Double-click the end node and then click the Response Parameters tab on the right side.
  10. Close the Response Parameters tab and then click Test in the upper-right corner.

    In the Test dialog box that appears, set the request parameter. After the test succeeds, you can view the test result.