A script template defines an SQL code process that includes multiple input and output parameters. You can introduce one or more source tables to an SQL code process and use the SQL code process to filter source table data, join source tables, and aggregate data to generate an output table required for business. This topic provides an overview of a script template and describes the definition and usage of a script template.
Overview of a script template
Introduction
In actual business scenarios, 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 create a script template based on an SQL code process to reuse SQL code. The script template extracts input parameters from input tables and output parameters from output tables.
When you create an SQL Snippet 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 edit the code. This helps prevent repeated operations during development and improve development efficiency. You can deploy and run a created SQL Snippet node in the same manner in which you deploy and run other SQL nodes.
Permissions
To create and use a script template, you must be assigned the Development role. For information about how to assign a role, see the Add a RAM user to a workspace as a member and assign roles to the member section of the "Manage permissions on workspace-level services" topic.
Limits
Only DataWorks Standard Edition or a more advanced edition supports SQL Snippet nodes. For more information, see Differences among DataWorks editions.
The script templates that are created by using member accounts in the current workspace are available on the Workspace-Specific tab.
The script templates that are created within tenants are available on the Public tab.
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 this type of script template is deployed, only the members in the current DataWorks workspace can use this type of script template. To use this type of script template, you must be a member in the current DataWorks workspace. For more information, see the Add a RAM user to a workspace as a member and assign roles to the member section of the "Manage permissions on workspace-level services" topic.
Public script template: The developers of this type of script template can go to the Public tab 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.
Use a script template
Script templates that are created in DataWorks must work together with SQL Snippet nodes.
In the Snippets pane of DataStudio, 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 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.The following types of input parameters are supported:
Table: Use this type of input parameters if output results are of a single type.
String: Use this type of input parameters if the values of input parameters are controlled based on variables.
The type of an output parameter must be Table.
Reference the script template.
On the DataStudio page, you can create an SQL Snippet node to reference the desired script template and replace input and output parameters in the script template to reuse code.
Define a script template
Go to the Snippets pane
Go to the DataStudio page.
Log on to the DataWorks console. In the top navigation bar, select the desired region. In the left-side navigation pane, choose . On the page that appears, select the desired workspace from the drop-down list and click Go to Data Development.
In the left-side navigation pane of the DataStudio page, click Snippets.
NoteIf the Snippets module is not displayed in the left-side navigation pane, click the
icon in the lower-left corner and follow the instructions that are described in Configure settings in the DataStudio Modules section to add the module.
Create and configure a script template
This section describes how to create a script template in the Snippets pane. In most cases, a script template consists of input parameters, output parameters, and an SQL code process. An SQL code process defines the code that implements the features of a script template. In an SQL code process, variable input tables or input character strings are abstracted as input parameters of the script template, and variable output tables are abstracted as output parameters of the script template. This allows you to reuse SQL code. Input parameters and output parameters are configured in the @@{Variable name}
format.
A script template can contain multiple input parameters and output parameters. You can configure the parameters based on your business requirements.
Step 1: Create a script template
In the Snippets pane, you can use one of the methods shown in the following figures to create and name a script template.
The script templates that are created by using member accounts in the current workspace are available on the Workspace-Specific tab.
The script templates that are created within tenants are available on the Public tab.
Step 2: Configure the script template
Step 3: Save and commit the script template
In the top toolbar on the configuration tab of the script template, click the icon to save the script template. Then, click the
icon to commit the script template. After the script template is created, you can reference the script template in an SQL Snippet node. This helps generate a table required for your business. For more information, see the Reference a script template section in this topic.
Reference a script template
Prerequisites
A script template is created. For more information, see the Define a script template section in this topic.
An SQL Snippet node is created. For more information, see Create and manage ODPS nodes.
Reference a script template
On the configuration tab of the SQL Snippet node, follow the steps that are shown in the following figure to reference a script template.
Select the script template that you want to reference.
If no script templates are available, create a script template. For more information, see the Define a script template section in this topic.
If the script template that you selected has the latest version, determine whether to reference the latest version of the script template based on your business requirements. To reference the latest version of the script template, click Update Code.
Click Edit Snippet to view the details of the script template.
Configure parameters in the script template based on your business requirements.
What to do next
After you complete the development of a task by using the created node, you can perform the following operations:
Configure scheduling properties: You can configure properties for periodic scheduling of the node. If you want the system to periodically schedule and run the task on the node, you must configure items for the node, such as rerun settings and scheduling dependencies. For more information, see Overview.
Debug the node: You can debug and test the code of the node to check whether the code logic meets your expectations. For more information, see Debugging procedure.
Deploy the node: After you complete all development operations, you can deploy the node. After the node is deployed, the system periodically schedules the node based on the scheduling properties of the node. For more information, see Deploy nodes.
Manage script templates
Share a script template and view the reference records of the script template
Upgrade a script template
More references
Features on the configuration tab of a script template
Best practices
Prerequisites
An SQL Snippet node is created. For more information, see Create and manage ODPS nodes.
An input table and an output table are created in an ODPS SQL node.