A script template is a reusable template for an SQL code process that includes input and output parameters. You can use script templates to process data from source tables by performing operations like filtering, joining, and aggregation to generate output tables. This topic introduces script templates and explains how to create and use them.
Overview of script templates
Introduction
In many data processing scenarios, you may write similar SQL code where only the names of the input and output tables differ, while their schemas remain identical or compatible. To avoid redundant work, you can encapsulate this logic into a script template. By defining variable table names as input and output parameters, you can reuse the same SQL code for different data sources.
After creating an SQL Snippet node, you can deploy and schedule it just like any other SQL node. This approach significantly improves development efficiency by promoting code reuse.
Permissions
You must have the Development role in a DataWorks workspace to create and use script templates. For more information about how to assign roles, see Add a workspace member and assign roles to the member.
Limitations
-
The SQL Snippet node feature is available only in DataWorks Standard Edition and later. For more information, see DataWorks editions and features.
-
Script templates created by members of the current workspace are displayed under the Component tab.
-
Script templates created by tenant members are displayed under the Public tab.
Script template types
Script templates are categorized as workspace-level or public. The developer who creates a script template can define its type.
-
Workspace-level script template: After a workspace-level script template is published, it can be used only by users within the same DataWorks workspace. To use this type of template, you must be a member of the workspace. For more information, see Add a workspace member and assign roles to the member.
-
Public script template: A developer can use the Publish Snippet feature to make a general-purpose script template available to the entire tenant. After it is published, the script template is available to all users within the tenant.
Workflow
Component created in DataWorks must be used with SQL Snippet. The workflow is as follows:
-
In the Component Management pane of DataStudio, a developer defines the SQL code process and its input and output parameters. This abstract SQL process takes specified input tables as input parameters, processes them, and generates output tables. In the code, input and output parameters use the format
@@{ParameterName}.-
The following types of input parameters are supported:
-
Table: Use this type for table-based inputs.
-
String: Use this type to pass variable values, such as filter conditions, into your SQL.
-
-
In practice, output parameters should be configured as the Table type.
-
-
On the DataStudio page, a user creates an SQL Snippet node, references the required script template, and configures the input and output parameters to reuse the code.
Define a script template
Go to the Snippets page
-
Go to the DataStudio page.
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 Data Development.
-
In the left-side navigation pane, click Component Management to go to the snippet management page.
NoteIf Component Management is not displayed in the left-side navigation pane, click the
icon in the lower-left corner and add it in the Module Management section.
Create and configure a script template
This section describes how a developer can create a script template in Component Management to abstract a specific SQL process into a reusable template. A script template typically consists of an SQL code process, input parameters, and output parameters. The SQL code process defines the implementation code for the template's functionality. Within this process, you use the @@{VariableName} format to define placeholders for input tables, strings, and output tables. These placeholders serve as the template's input and output parameters, enabling SQL code reuse.
A script template can have multiple input and output parameters.
For example, the process body code can be INSERT OVERWRITE TABLE @@{my_output} SELECT ... FROM @@{my_input} WHERE col1 = @@{my_input_parameter1} AND col2 = @@{my_input_parameter2}, where @@{my_output} is the output table parameter, @@{my_input} is the input table parameter, and @@{my_input_parameter1} and @@{my_input_parameter2} are input string parameters.
Step 1: Create a script template
On the Snippets page, you can create a script template in one of the following ways:
-
Method 1: In the left-side Snippets panel, right-click the Snippets node and choose New > Snippet.
-
Method 2: In the toolbar at the top of the Snippets panel, click the New icon and choose New > Snippet.
-
Script templates created by members of the current workspace are displayed under the Component tab.
-
Script templates created by tenant members are displayed under the Public tab.
Step 2: Configure the script template
Step 3: Save and commit the script template
Click the
icon to save the script template, and then click the
icon to commit it. After the script template is created, you can reference it in an SQL Snippet node to quickly generate the tables required for your business. For more information, see Reference a script template.
Reference a script template
Prerequisites
-
A script template is created and ready to use. For more information, see Define a script template.
-
Create an SQL Snippet node. For more information, see Create and manage MaxCompute nodes.
Reference the script template
On the configuration tab of the SQL Snippet node, reference a script template. In this example, select the get_top_n (V1) script template and configure the parameter values in the right-side Parameter Configuration panel:
-
Input parameter myinputtable: type is table, value is
company_sales_record -
Input parameter topn: type is string, value is
10 -
Output parameter myoutput: type is table, value is
company_sales_top_n
-
Select the script template that you want to use.
If no script templates are available, create one. For more information, see Define a script template.
-
If a newer version of the selected script template exists, you can click Update Code to use the latest version.
-
Click Edit Snippet to view the details of the script template.
-
-
Configure the parameter values for the script template based on your use case.
Next steps
After you finish developing the node, you can perform the following operations:
-
Configure scheduling properties: If the node requires periodic execution, configure its scheduling properties, such as rerun settings and dependencies. For more information, see Overview of task scheduling configuration.
-
Perform node debugging: Run the node to test its code and verify the logic. For more information, see Node debugging process.
-
Deploy the task: After you finish development, deploy the task. Once deployed, the node runs periodically according to its scheduling configuration. For more information, see Deploy tasks.
Manage script templates
Publish and view usage
Upgrade a script template
References
UI features
Best practices
Prerequisites
-
An SQL Snippet node is created. For more information, see Create and manage MaxCompute nodes.
-
Create an input table and an output table in an ODPS SQL node.