All Products
Search
Document Center

DataWorks:Script template management

Last Updated:Mar 05, 2025

An SQL code process can be abstracted as a script template to allow the reuse of SQL code. Before you use a script template, you must create a script template that meets your business processing requirements. This topic describes how to create a script template in the SQL SCRIPT TEMPLATES pane of the Data Studio page in the DataWorks console. This topic also describes how to share and upgrade a script template and view the reference records of a script template on the configuration tab of the script template.

Introduction

In actual business scenarios of MaxCompute, a large number of SQL code processes are similar. The input tables or output tables of these processes may have the same schema or compatible data types but different table names. In this case, developers can abstract an SQL code process as a script template to reuse the SQL code. The script template extracts input parameters from input tables and output parameters from output tables.

When you create an SQL Script Template node, you need to only select an existing script template from the script template list based on your business process and configure specific parameters for input tables and output tables in your business for the selected script template. This way, you do not need to repeatedly modify the code. This prevents repeated operations during development and improves development efficiency. You can deploy and run a created SQL Script Template node in the same manner in which you deploy and run other SQL nodes.

Note

Only MaxCompute compute engines support SQL script templates.

Permissions

To create and use a script template, you must be assigned the Development role. For more information, see Manage permissions on workspace-level services.

Script template types

Script templates are classified into workspace-level and public script templates. You can specify the type of a script template when you create the script template.

  • Workspace-level script template: After a script template of this type is deployed, only the members in the current DataWorks workspace can use the script template. To use this type of script template, you must be a member in the current DataWorks workspace. For more information, see Manage permissions on workspace-level services.

  • Public script template: The developers of this type of script template can go to the Public SQL Script Templates section and make a general-purpose script template public within the current tenant. This way, all users within the current tenant can use the script template.

Go to the SQL SCRIPT TEMPLATES pane

  1. Go to the Workspaces page in the DataWorks console. In the top navigation bar, select a desired region. Find the desired workspace and choose Shortcuts > Data Studio in the Actions column.

  2. In the left-side navigation pane of the Data Studio page, click the image icon to go to the SQL SCRIPT TEMPLATES pane.

Use a script template

Step 1: Define a script template

In the SQL SCRIPT TEMPLATES pane of Data Studio, developers can abstract an SQL code process and define input and output parameters. This way, the SQL code process can process a specific input table based on the input parameters to generate an output table with business value based on output parameters. The input and output parameters are configured in the @@{Parameter name} format.

  1. In the Workspace SQL Script Templates section, click the image icon and select Create SQL Script Template or Create Directory. If you select Create Directory, create a directory, right-click the directory name, and then select Create SQL Script Template. In the Create SQL Script Template dialog box, configure the parameters and click OK.

    Note
    • The script templates created by the members of the current workspace are displayed in the Workspace SQL Script Templates section.

    • After you make the script templates public, the script templates are displayed in the Public SQL Script Templates section.

  2. Configure the script template.

    1. Configure an SQL code process.

      An SQL code process defines the code that implements the features of a script template. You can define an abstract SQL code process to introduce input parameters and output parameters in the @@{Parameter name} format to a script template. This way, the script template can process an input table based on input parameters to generate an output table with business value. When you use the script template, you need to only configure specific input and output parameters to generate executable SQL code.

    2. Configure input parameters.

      In the right-side navigation pane of the configuration tab of the script template, click Parameter Configuration. On the Parameter Configurations tab, click the image icon to the right of Input Parameters. In the Input Parameters section, define input parameters for the SQL code process. The input parameters of the Table or String type are supported, which indicates that an input parameter of the SQL code process is a table or specific string.

      Note

      You can also click Parse I/O Parameters in the top toolbar of the configuration tab of the script template. This way, the system automatically identifies input and output parameters from code by using the automatic parsing feature and configures the input and output parameters for the SQL code process.

      Table

      Use scenarios

      If you want the output results to be generated after you use the script template to process source table data are of a single type, you can use this type.

      Key parameter and configuration description

      • Key parameter: Parameter Definition

      • Configuration description: The value of the Parameter Definition parameter is a text definition of the schema of an input table, including the field names, field types, and field descriptions. When you use the script template in subsequent operations, you must specify an input table that contains the same number of fields and compatible field types that are defined for the table-type parameter. Otherwise, an error is returned when the script template is run.

        Note

        The parameter definition is for reference only and does not immediately trigger a forcible check.

      • Example:

        We recommend that you define parameters in the following format:

        Name of Field 1 Type of Field 1 Description of Field 1 
        Name of Field 2 Type of Field 2 Description of Field 2 
        ...
        Name of Field n Type of Field n Description of Field n

        Sample configuration:

        area_id STRING 'Region ID' 
        city_id STRING 'City ID' 
        order_amt DOUBLE 'Order amount' 

      String

      Use scenarios

      If you need to use variables to control the values of input parameters when you use the script template to process source table data, you can use this type.

      Key parameter and configuration description

      • Key parameter: Default Value

      • Configuration description: You can specify a value as the default value of a string-type parameter in the Default Value field. The default value is used by default when the script template is used.

      • Example:

        • Scenario 1: To export only the sales amount of the top N cities in each region in an output table of an SQL code process, you can use a string-type parameter to specify the value of N.

        • Scenario 2: To export the total sales amount of a province in an output table of an SQL code process, you can use a string-type parameter to specify the name of the province and obtain the sales data of the specified province.

    3. Configure output parameters.

      In this step, you can configure output parameters to define a table that you want to generate in an SQL code process. To facilitate the use of a script template, you can specify the schema of an output table for reference.

      In the right-side navigation pane of the configuration tab of the script template, click Parameter Configuration. On the Parameter Configurations tab, click the image icon to the right of Output Parameters. In the Output Parameters section, define the output parameters for the SQL code process.

      Note

      You can also click Parse I/O Parameters in the top toolbar of the configuration tab of the script template. This way, the system automatically identifies input and output parameters from code by using the automatic parsing feature and configures the input and output parameters for the SQL code process.

      • Key parameter: Parameter Definition

      • Configuration description: The value of the Parameter Definition parameter is a text definition of the schema of an output table, including the field names, field types, and field descriptions. When you use the script template in subsequent operations, you must specify an output table that contains the same number of fields and compatible field types that are defined for the table-type parameter. Otherwise, an error is returned.

        Note

        The parameter definition is for reference only and does not immediately trigger a forcible check.

      • Example:

        We recommend that you define parameters in the following format:

        Name of Field 1 Type of Field 1 Description of Field 1 
        Name of Field 2 Type of Field 2 Description of Field 2 
        ...
        Name of Field n Type of Field n Description of Field n

        In addition, you can add fields that indicate aggregated output results to the parameter definition based on your business requirements. For example, you can add a ranking field or a revenue field. Sample configuration:

        area_id STRING 'Region ID' 
        city_id STRING 'City ID' 
        order_amt DOUBLE 'Order amount'
        rank BIGINT 'Ranking'
  3. In the top toolbar of the configuration tab, click Save and Commit.

    After the script template is created, you can reference the script template in an SQL Script Template node to generate the required table for your business. For more information, see the Step 2: Reference a script template section in this topic.

  4. Optional. In the top toolbar of the configuration tab, click Make SQL Script Template Public to make the script template public within the current tenant. Then, the script template is displayed in the Public SQL Script Templates section. This way, all users within the current tenant can use the script template.

Step 2: Reference a script template

You can use one of the following methods to reference a script template.

Reference a script template in an SQL Script Template node

In the DATASTUDIO pane, you can create an SQL Script Template node to reference the desired script template and replace the input and output parameters in the script template to reuse code.

  1. In the left-side navigation pane of the Data Studio page, click the image icon to go to the DATASTUDIO pane.

  2. In the Workspace Directories section, find the desired directory in which you want to create SQL Script Template node, right-click the directory name or click the image icon, and then choose Create Node > MaxCompute > SQL Script Template. In the Create Node dialog box, enter a name in the Name field based on your business requirements.

  3. In the right-side navigation pane of the configuration tab of the SQL Script Template node, click Settings for SQL Script Template and configure the SQL Script Template parameter.

  4. After you select a script template, view the script template code that is automatically displayed on the configuration tab of the SQL Script Template node, and configure the parameters on the Settings for SQL Script Template tab.

Reference a script template in the Workspace SQL Script Templates section

In the SQL SCRIPT TEMPLATES pane, you can find the desired script template, create an SQL Script Template node by referencing the script template, and replace the input and output parameters in the script template to reuse code.

  1. In the left-side navigation pane of the Data Studio page, click the image icon to go to the SQL SCRIPT TEMPLATES pane.

  2. In the Workspace SQL Script Templates section, find the desired script template, right-click the name of the script template, and then select Reference SQL Script Template.

  3. Follow the on-screen instructions to select the location in which you want to create an SQL Script Template node and configure the name of the node.

  4. After the SQL Script Template node is created, view the script template code that is automatically displayed on the configuration tab of the node, and configure the parameters on the Settings for SQL Script Template tab.

Reference a script template in the Public SQL Script Templates section

In the SQL SCRIPT TEMPLATES pane, you can find a script template that is made public by another tenant, create an SQL Script Template node by referencing the script template, and replace the input and output parameters in the script template to reuse code.

  1. In the left-side navigation pane of the Data Studio page, click the image icon to go to the SQL SCRIPT TEMPLATES pane.

  2. In the Public SQL Script Templates section, find the script template and click Reference.

  3. Follow the on-screen instructions to select the location in which you want to create an SQL Script Template node and configure the name of the node.

  4. After the SQL Script Template node is created, view the script template code that is automatically displayed on the configuration tab of the node, and configure the parameters on the Settings for SQL Script Template tab.

Upgrade a script template

Upgrade operation: Developer

You can modify the code of a script template that you developed and the parameter settings of the script template based on your business requirements. After the modifications are saved and the script template is recommitted, the script template is upgraded to a new version. On the Version tab in the right-side navigation pane of the configuration tab of the script template, you can view the details of each version. The Versions tab is marked as 5 in the following figure.

Use of the upgraded script template: User

If a script template is upgraded and you want to reference the script template in your SQL Script Template node, you can determine whether to use the latest version of the script template.

  • If you do not want to use the latest version of the script template, you can select the original version of the script template.

  • If you want to use the latest version of the script template, check whether the parameter settings in the latest version of the script template take effect for your SQL Script Template node and modify configurations based on the template description. Then, commit and deploy the SQL Script Template node. You can commit and deploy the SQL Script Template node in the same manner in which you commit and deploy other SQL nodes.

Sample scenario of using an upgraded script template

A developer creates a script template whose version is V1, and a user uses the script template. In subsequent operations, the developer upgrades the version of the script template to V2. The user finds that the script template has the latest version V2 during the script template usage. The user can open the script template and view the details about the latest version. If the latest version of the script template can provide a better business effect, the user can modify the configurations to use the latest version.

View the reference records of a script template

In the right-side navigation pane of the configuration tab of a script template, you can click Reference Records to view the nodes that reference the script template. This helps you evaluate the impacts of modifying the script template.