Data Studio: For developers

更新时间:
复制 MD 格式

This topic demonstrates how to create a scheduled task in Data Studio by using a MaxCompute job as an example. This guide helps you quickly get started with the basic features of Data Studio.

Prerequisites

Note

This topic uses an ODPS SQL node as an example. Therefore, the workspace must be bound to a MaxCompute data source.

Background

DataWorks Data Studio provides a visual development interface for various compute engines, such as MaxCompute, Hologres, EMR, and CDH. It supports intelligent code development, data cleansing, data processing, and standardized task deployment, ensuring efficient and stable data development. For more information, see Data Studio (legacy).

Typically, the process of writing raw business data to DataWorks and processing it into a final result table includes the following steps:

  1. Create multiple data tables in DataWorks. For example:

    • A source table to store data synchronized from other data sources.

    • A result table to store data that has been cleansed and processed by DataWorks.

  2. Create a synchronization task to sync business data to the source table.

  3. Create a compute node to cleanse and process the data from the source table in layers and write the result of each layer to the corresponding result table.

Alternatively, after you create a table, you can upload local data directly to the source table in DataWorks. You can then use a compute node to cleanse and process the data and store the results in the result table. This topic's example involves uploading local data and cleansing it with a compute node.

Go to Data Studio

Log on to the DataWorks console. In the target region, click Data Development and O&M > Data Development in the left-side navigation pane. Select a workspace from the drop-down list and click Go to Data Development.

Procedure

  1. Step 1: Create a workflow.

    Data development is organized around workflows. You must create a workflow before you can begin developing code.

  2. Step 2: Create tables.

    DataWorks allows you to create tables visually, which are then displayed in a directory structure. Before you begin data development, you must create a table in the compute engine to store data cleansing results.

  3. Step 3: Create a node.

    DataWorks encapsulates tasks from different compute engines into different node types. You can select the appropriate node type based on your business needs.

  4. Step 4: Edit the node.

    On the node editing page, write your business logic using the syntax of the node's database engine.

  5. Step 5: Configure schedule settings for the node.

    Define the scheduling properties for the node to run it periodically.

  6. Step 6: Debug the code.

    DataWorks provides three methods to debug your code and verify its logic: Quick run for code snippets, Run, and Advanced run.

  7. Step 7: Save and submit the node.

    After you debug the node, you must save and submit it.

  8. Step 8: Run smoke testing.

    To ensure that production tasks run efficiently and to avoid wasting compute resources, run smoke testing on your task in the development environment to verify its correctness before deployment.

  9. Step 9: Deploy the task.

    DataWorks only runs scheduled tasks that are deployed to the production environment. After the smoke testing is successful, you must deploy the task to the production scheduling system.

Step 1: Create a workflow

DataWorks organizes data development around workflows. Workflows use container-like panels for development nodes, grouping related tools, optimizations, and management actions around a central object. This approach simplifies development and management. You can group related business tasks into a single workflow to meet your requirements.

  1. Log on to the DataWorks console. In the target region, click Data Development and O&M > Data Development in the left-side navigation pane. Select a workspace from the drop-down list and click Go to Data Development.

  2. Create a workflow.

    You can create a workflow in one of the following two ways:

    • Method 1: Hover over the 新建 icon and click Create Workflow.

    • Method 2: In the left-side navigation tree of Data Studio, right-click Workflow and select Create Workflow.

  3. Enter a name and description for the workflow and click Create.

    This tutorial creates a workflow named Create your first scheduled task. In a real-world scenario, you should plan your workflows based on your business requirements.

    Note

    For more information about workflows, see Create and manage workflows.

Step 2: Create tables

Data Studio nodes cleanse and process your source data. Therefore, you must first create tables in the compute engine to store the data cleansing results and define the table structure.

  1. Create the tables.

    1. In the workflow you created in Step 1, expand the sub-directory, right-click MaxCompute > Table, and select Create Table.

    2. Configure the table name, engine instance, and other information.

    In this tutorial, the following two tables are created.

    Table name

    Description

    bank_data

    Stores raw business data.

    result_table

    Stores the cleansed data results.

    Note
    • For the table creation statements, see Table creation statements.

    • For more information about visually creating tables for various engines, such as creating MaxCompute or EMR tables, see Create a table.

  2. Configure the table structure.

    Go to the table editing page, switch to DDL mode, and use DDL statements to configure the table structure. After the table structure is generated, enter a Display Name for the table in the General section. Then, submit it to both the development environment and the production environment. After you submit the table, you can view it in the data source project of the corresponding environment. To view the bound data source information for each environment, see Bind MaxCompute compute resources.

    Note
    • Table operations such as creation and updates take effect in the compute engine only after you submit them to the corresponding environment.

    • You can also configure the table structure visually based on your business needs and on-screen instructions. For more information about visually creating tables, see Create and use MaxCompute tables.

    The following statement is a reference for generating the bank_data table structure.

    CREATE TABLE IF NOT EXISTS bank_data
    (
     age             BIGINT COMMENT 'Age',
     job             STRING COMMENT 'Job type',
     marital         STRING COMMENT 'Marital status',
     education       STRING COMMENT 'Education level',
     default         STRING COMMENT 'Whether the user has a credit card',
     housing         STRING COMMENT 'Housing loan',
     loan            STRING COMMENT 'Personal loan',
     contact         STRING COMMENT 'Contact method',
     month           STRING COMMENT 'Month of the year',
     day_of_week     STRING COMMENT 'Day of the week',
     duration        STRING COMMENT 'Last contact duration, in seconds',
     campaign        BIGINT COMMENT 'Number of contacts performed during this campaign',
     pdays           DOUBLE COMMENT 'Days since the last contact from a previous campaign',
     previous        DOUBLE COMMENT 'Number of contacts performed before this campaign',
     poutcome        STRING COMMENT 'Outcome of the previous marketing campaign',
     emp_var_rate    DOUBLE COMMENT 'Employment variation rate',
     cons_price_idx  DOUBLE COMMENT 'Consumer price index',
     cons_conf_idx   DOUBLE COMMENT 'Consumer confidence index',
     euribor3m       DOUBLE COMMENT 'Euribor 3-month rate',
     nr_employed     DOUBLE COMMENT 'Number of employees',
     y               BIGINT COMMENT 'Whether the client has subscribed to a term deposit'
    );

    The following statement is a reference for generating the result_table table structure.

    CREATE TABLE IF NOT EXISTS result_table
    (
    education STRING COMMENT 'Education level',
    num BIGINT COMMENT 'Number of people'
    )
    PARTITIONED BY
    (
    day STRING,
    hour STRING
    );
  3. Upload data.

    Store your raw business data in a DataWorks table. In this example, the local file banking.txt is uploaded to the bank_data table in DataWorks to simulate a real-world data write scenario. In the data import wizard, first select the target table bank_data, confirm that the table schema includes fields such as age, job, marital, education, default, and housing, and then set the partition parameters. Then, upload the local file banking.txt, set the file format to CSV, the delimiter to a comma, the character set to GBK, and the import start row to 1, and select the 'First row is header' checkbox. Finally, select Match by Name as the field mapping method, confirm that the mapping between the source and target fields is correct, and then click Import Data to complete the operation. For more information about uploading data, see Upload local data to the bank_data table.

Step 3: Create a node

Select an appropriate node type for development based on your business requirements.

Note

DataWorks nodes are categorized as data synchronization nodes and compute nodes. In a typical development process, you first use a batch synchronization task to sync data from business databases to a data warehouse. Then, you use DataWorks compute nodes to cleanse and process the table data in the warehouse.

  1. Create a node.

    You can create a node in one of the following two ways:

    • Method 1: Create from the navigation tree.

      1. In the navigation tree, find the workflow you created in Step 1 under Workflow.

      2. Right-click the desired engine and select an appropriate node under Create Node.

    • Method 2: Create from the workflow panel.

      1. In the navigation tree, find the workflow you created in Step 1 under Workflow.

      2. Double-click the workflow to open its panel.

      3. In the left-side navigation pane of the panel, click the desired node or drag it onto the canvas.

  2. Configure the node's engine instance, name, and other settings.

    This tutorial creates an ODPS SQL node named result_table, which is the same as the name of the result table created in Step 2.

    Note

    When you use DataWorks nodes for data development, you cleanse data with a development node and store the results in a result table. Name the node after the result table it populates to make it easier to locate the table data produced by the node.

    In the Common Nodes section of the Data Studio panel, drag the ODPS SQL node onto the canvas on the right to create it.

Step 4: Edit the node

Find the node you created in Step 3 in the workflow navigation tree or on the workflow panel. Double-click the node to open the editor. Based on the node type, write your business logic using the syntax of the corresponding database.

In this tutorial, the result_table node writes data from a specific partition of the bank_data table to the corresponding partition of the result_table table. The target partition is defined using the variables day and hour.

Note
  • During code development, if you need to dynamically replace parameters in a scheduling context, you can define variables in your code using the format ${your_variable_name}. You can then assign a value to this variable when you configure the schedule settings in Step 5.

  • For more information about scheduling parameters, see Supported formats for scheduling parameters.

  • For more information about the development syntax for different node types, see Create and use nodes.

--@exclude_output=xc_DPE_E2.result_table
--@exclude_input=bank_data
--odps sql
--***********************************************************--
--author:xxx
--create time:2022-08-11 14:33:23
--***********************************************************--
INSERT OVERWRITE TABLE result_table partition (day='${day}', hour='${hour}')
SELECT education
    , COUNT(marital) AS num
FROM bank_data
WHERE  day='${day}' and hour='${hour}'
GROUP BY education;

The following code is provided for your reference.

INSERT OVERWRITE TABLE result_table partition (day='${day}', hour='${hour}')
SELECT education
, COUNT(marital) AS num
FROM bank_data
GROUP BY education;

Step 5: Configure schedule settings

Configure the scheduling properties to run the node periodically. On the node editor page, click Scheduling in the right-side navigation pane and configure the properties based on your business requirements.

Parameter

Description

Basic properties

DataWorks automatically displays the node's name, ID, type, and owner. These properties do not require separate configuration.

Note
  • The owner defaults to the current user. You can change it as needed. Only members of the current workspace can be selected as the owner.

  • DataWorks automatically generates an ID after you submit the node.

Parameters

Define the parameters used for node scheduling.

DataWorks provides built-in and custom parameters for dynamic parameter assignment during task scheduling. If you defined variables in the code in Step 4, you can assign values to them here.

This tutorial assigns values to the variables from Step 4 to write hourly data from the bank_data table for the previous day (business date) to the corresponding hourly partition in the result_table table.

  • Assign ${yyyymmdd} to the day variable.

  • Assign $[hh24] to the hour variable.

After you configure these settings in the Parameters section of the Scheduling pane, the values assigned to day and hour will automatically replace the ${day} and ${hour} variables in the SQL code during a scheduled run.

Time properties

The time properties define how and when the node runs. Use these settings to configure periodic instance generation, scheduling cycle, execution time, rerun support, and the timeout period for automatic task termination.

Note
  • You must configure the Rerun attribute for the node before you can submit it.

  • Even if a node is scheduled to run before its upstream node, it will not start until the upstream node has run successfully.

In this tutorial, the result_table node is set to start running at 00:00 and is scheduled hourly. This means every hour, it writes data from the bank_data table for the previous day (business date) to the corresponding hourly partition of the result_table table. In the Time Properties section of the Scheduling pane, set Instance Generation to Immediately After Deployment, Schedule Type to Normal Scheduling, and Recurrence to Hour. In the detailed recurrence settings, set the Start Time to 00:00, the Time Interval to 1 hour, and the End Time to 23:59. The corresponding cron expression is 00 00 00-23/1 * * ?.

Resource properties

Configure the scheduling resource group to be used when the task is deployed to the production environment for scheduling. This tutorial uses the default serverless resource group provided when DataWorks was activated. For more information, see Use a serverless resource group.

Dependencies

Define the upstream and downstream dependencies for node scheduling. Set dependencies based on data lineage. This ensures that the current node only runs after the upstream tasks that produce its required data have completed successfully, allowing it to query the upstream table data correctly.

Note
  • If a node uses a select statement to query table data that is not produced by a DataWorks scheduled node, you can set Auto Parse to No and use the workspace root node to schedule the current node's run.

  • If the current node uses a select statement to query table data produced by another task, you can configure the dependency on the upstream node (which produces the data) in one of the following ways, so that the upstream node triggers the current node to run:

    • If the upstream node is not in the current workflow or workspace: In the Parent Nodes section of the current node, enter the Output Name of the upstream node.

    • If the upstream node is in the current workflow: Configure the dependency by dragging a connection line between the nodes on the workflow panel.

In this tutorial, assume that a task outside the current workflow produces the bank_data table that the result_table node queries. In this case, you need to configure the workspace root node as an upstream dependency for the result_table node. This configuration makes the root node trigger the result_table node to run.

(Optional) Node context

Defines how parameters are passed between upstream and downstream nodes. A downstream node can use this feature to retrieve values from an upstream node.

Note

This feature is typically used with assignment nodes or assignment parameters.

Step 6: Debug the code

You can use the following methods to debug your code logic and ensure it is correct.

Method

Description

Recommendation

Code line: Quick run

Quickly runs a selected code snippet.

Use this method when you need to quickly run a piece of code.

Toolbar: Run (Run)

Assigns constant values to variables for a test run.

Note

The first time you click Run on a new node, you must manually assign constant values to the code variables in the dialog box. This assignment is saved and does not need to be repeated for subsequent runs.

Use this method if you need to frequently debug the entire code.

Toolbar: Advanced Run (Advanced Run)

Requires you to assign constant values to variables for each test run.

Use this method if you need to change the variable assignments for a test run.

This tutorial uses Advanced run to test the results of the run for 2022.09.07 14:00. In the Parameters dialog box, select a Scheduling Resource Group (for example, Public Scheduling Resource Group). In the Custom Parameters section, set the parameter values (for example, day=20220907 and hour=14). Click OK to execute the code. The run log shows that the execution was successful.

Step 7: Save and submit the node

After you have configured and tested the node, you must save the configuration and submit the node to the development environment.

Note

You can submit the node only after you have configured the Rerun attribute and Parent Nodes in Step 5.

  1. Click the 保存 icon on the toolbar to save the node configuration.

  2. Click the 提交 icon on the toolbar to submit the node to the development environment.

Step 8: Run smoke testing

Perform smoke testing before deploying a task to ensure that production tasks run efficiently and to avoid wasting compute resources. Smoke testing runs in the development environment, so you must first submit the node to that environment. After submission:

  1. Click the 冒烟测试 icon in the toolbar and configure the business date in the smoke testing dialog box.

  2. After the smoke test is complete, click the 查看冒烟测试结果 icon in the toolbar to view the test results.

This tutorial tests whether the scheduling parameter configuration meets expectations. The result_table node is scheduled to run every hour from 00:00 to 23:59. Therefore, the configuration in the following figure will generate two hourly instances, with scheduled run times of 00:00 and 01:00 respectively.

Note
  • An instance is a snapshot of a task that is generated when a periodic task runs according to its schedule.

  • Because the result_table node is set as an hourly task, you must not only configure the business date for smoke testing but also select the time range of instances to run.

  • For more information about smoke testing, see Run smoke testing.

Click the Smoke testing (☑) icon in the top toolbar. In the smoke testing panel that appears, set the Business Date (for example, 2022-09-01), Start Time (00:00), and End Time (01:00).

Step 9: Deploy the task

In a basic mode workspace, tasks are scheduled periodically after submission. In a standard mode workspace, a submitted task enters a "to be deployed" state. You must then deploy the task to the production environment to enable periodic scheduling.

Note
  • DataWorks only supports automatic scheduling for tasks that are deployed to the production environment. After the smoke testing is successful, you must deploy the task to the production scheduling system to enable periodic scheduling.

  • For more information about the differences between workspace modes, see Differences between workspace modes.

In a standard mode workspace, any changes you submit in Data Studio, such as creating, updating, or deleting nodes, resources, and functions, are sent to the task deployment page to await deployment. To apply these changes, you must navigate to Task Deployment > Create Deploy Task and create a deployment package to release them to the production environment. The changes become active only after a successful deployment. For more information, see Deploy tasks.

The following table describes the deployment process.

Deployment topic

Description

Deployment process control

Role permissions and process controls constrain deployment operations. After you perform a deployment, ensure that the status of the deployment package is "successful."

Note
  • After deployment, check the status of the deployment package on the Deployment Packages page.

  • Developers can only create deployment packages. The actual deployment requires O&M permissions.

Deployment effective time control

For deployments performed during the daily instance generation window (22:00 to 24:00), the changes will only apply to scheduled instances starting from the day after tomorrow.

Note

This restriction affects both the Next Day and Immediately After Deployment instance generation modes. For more information about these modes, see Instance generation modes.

Next steps

You can go to Operation Center > Auto Triggered Task O&M to view the batch scheduling tasks deployed to the production environment and perform related O&M operations. For more information, see Basic O&M for periodic tasks.