Synchronize data across databases with task orchestration

更新时间:
复制 MD 格式

An e-commerce platform manager needs to view the daily order count and sales amount by product category. This topic shows how to create a cross-database Spark SQL task using task orchestration. This task periodically synchronizes the order and product tables from an online database to a data warehouse for analysis, and then writes the results back to the online database for the manager to query.

Prerequisites

  • A MySQL database is required to serve as the online database to store the order and product tables. You must have the permissions to modify this database. To request permissions, see Overview of access control.

  • An AnalyticDB for MySQL data warehouse is required for data processing. You must have the permissions to modify this data warehouse. To request permissions, see Overview of access control.

Background

An e-commerce platform generates a large volume of data. Analyzing this data directly in the online database can degrade its performance or even cause it to stop responding to business requests. Therefore, you typically synchronize online business data to a dedicated data warehouse for processing and analysis. The results are then written back to the online database, allowing the online application to provide data analysis and reporting services.

Procedure

  1. Prepare the data and tables.

  2. Create a cross-database Spark SQL task.

  3. Configure the cross-database Spark SQL task.

  4. Publish the task flow.

Prepare the data and tables

  1. Create the order, product, and reporting tables in your MySQL database.

    1. Log in to DMS 5.0.

    2. Move the pointer over the 2023-01-28_15-57-17.png icon in the upper-left corner of the DMS console and choose All Features > SQL Console > SQL Console.

      Note

      If you use the DMS console in normal mode, choose SQL Console > SQL Console in the top navigation bar.

    3. In the Please select the database first dialog box, search for and select your MySQL database, and then click Confirm.

    4. In the MySQL database, create the order table t_order, the product table t_product, and the reporting table t_order_report_daily to store synchronized analysis results.

      1. Create the order table named t_order. Paste the following SQL statement into the SQL editor and click Execute.

        SQL statement to create the t_order table:

        CREATE TABLE `t_order` (
          `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
          `product_id` bigint(20) NOT NULL COMMENT 'Product ID',
          `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
          `gmt_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
          `customer_id` bigint(20) NOT NULL COMMENT 'Customer ID',
          `price` decimal(14,2) NOT NULL COMMENT 'Price',
          `status` varchar(64) NOT NULL COMMENT 'Order status',
          `province` varchar(256) DEFAULT NULL COMMENT 'Transaction province',
          PRIMARY KEY (`id`),
          KEY `idx_product_id` (`product_id`),
          KEY `idx_customer_id` (`customer_id`),
          KEY `idx_status` (`status`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='Order table'
        ;

        The structure of the t_order table is as follows:

        The table has eight columns: id, product_id, gmt_create, gmt_modified, customer_id, price, status, and province. The id column is the auto-incrementing primary key. The province column can be null. The gmt_create and gmt_modified columns default to CURRENT_TIMESTAMP.

      2. Create the product table named t_product. Paste the following SQL statement into the SQL editor and click Execute.

        SQL statement to create the t_product table:

        CREATE TABLE `t_product` (
          `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
          `name` varchar(128) NOT NULL COMMENT 'Product name',
          `type` varchar(64) NOT NULL COMMENT 'Product category',
          `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
          `gmt_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time',
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Product table'
        ;

        The structure of the t_product table is as follows:

        The table has five columns: id, name, type, gmt_create, and gmt_modified. The id column is the auto-incrementing primary key. The gmt_create and gmt_modified columns default to CURRENT_TIMESTAMP.

      3. Create the reporting table named t_order_report_daily. Paste the following SQL statement into the SQL editor and click Execute.

        SQL statement to create the t_order_report_daily table:

        CREATE TABLE `t_order_report_daily` (
          `dt` varchar(64) NOT NULL COMMENT 'Statistics date',
          `product_type` varchar(64) NOT NULL COMMENT 'Product type',
          `order_cnt` bigint(64) NOT NULL COMMENT 'Number of orders',
          `order_amt` decimal(38,2) NOT NULL  COMMENT 'Total amount',
          PRIMARY KEY (`dt`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Reporting table'
        ;

        The structure of the t_order_report_daily table is as follows:

        The table has four columns: dt, product_type, order_cnt, and order_amt. The dt column is the primary key.

  2. In the AnalyticDB for MySQL data warehouse, create the order, product, and reporting tables to receive data and store analysis results.

    1. Go to the SQL editor page. In the Please select the database first dialog box, search for and select your AnalyticDB for MySQL data warehouse, and then click Confirm.

    2. In the AnalyticDB for MySQL data warehouse, create the t_order and t_product tables to receive synchronized data, and the t_order_report_daily table to store analysis results.

      Note

      For the SQL statements to create the t_order, t_product, and t_order_report_daily tables, see Step 4 in the preceding section.

    3. Populate the t_order and t_product tables with test data. You can use the test data generation feature. For more information, see Test data generation.

      • Generate 20 million rows of data for the t_order table.

        Note

        For an instance in Flexible Management mode, each ticket can generate a maximum of 1 million rows.

      • Generate 10,000 rows of data for the t_product table.

Create a cross-database Spark SQL task

  1. Log in to DMS 5.0.

  2. In the top navigation bar, choose Data+AI > Data+AI > Task Orchestration.

    Note

    If you use the DMS console in simple mode, move the pointer over the 2023-01-28_15-57-17.png icon in the upper-left corner and choose All Features > Data+AI > Data Development > Task Orchestration.

  3. Create a new task flow.

    1. Click Create Task Flow.

    2. In the New Task Flow dialog box, enter a Task Flow Name and Description, and then click Confirm.

  4. From the Task Type list on the left side of the canvas, drag the Cross-Database Spark SQL node onto the canvas.

Configure the cross-database Spark SQL task

  1. On the task flow details page, select the Cross-Database Spark SQL node.

  2. Configure the ${today} variable to represent the current date. For more information about variables, see Variables.

    1. Click the Variable Setting tab.

    2. Click the Node Variable tab.

    3. Enter the parameters for the node variable.

      Set Variable Name to today, Time Format to yyyy-MM-dd, and the variable rule to the current date plus 1 day.

  3. Add the MySQL database that contains the product and order data.

    1. In the Database Reference section, click Add Database Reference.

    2. Select the database type, search for and select the database, and then enter demo_id as the reference alias for the database in your Spark SQL statements. For details on the configuration items, see Database configuration table.

    3. Click Save.

  4. Add the AnalyticDB for MySQL data warehouse that is used for data processing.

    1. Click the 5加6 icon to the right of the demo_id database to add a new database reference.

    2. Select the database type, search for and select the database, and then enter company as the reference alias for the database in your Spark SQL statements.

    3. Click Save.

    After you complete the configuration, the Database Reference section displays two database references: the MySQL database with the alias demo_id and the AnalyticDB for MySQL data warehouse with the alias company. Both are in the production environment.

  5. In the SQL editor, write the Spark SQL statements and click Save.

    /*Synchronize data.*/
    /*Perform a full write of the product table.*/
    insert overwrite company.t_product
    select id,
           name,
           type,
           gmt_create,
           gmt_modified
      from demo_id.t_product ;
    
    /*Perform an incremental write to the order table.*/
    insert into company.t_order
    select id,
           product_id,
           gmt_create,
           gmt_modified,
           customer_id,
           price,
           status,
           province
      from demo_id.t_order
     where gmt_create>= '${bizdate}'
       and gmt_create< '${today}' ;
    
    /*Process data and calculate statistics grouped by product category.*/
    insert into company.t_order_report_daily
    select '${bizdate}',
           p.type as product_type,
           count(*)  order_cnt,
           sum(price)  order_amt
      from company.t_product p join company.t_order o on o.product_id= p.id
     where o.gmt_create>= '${bizdate}'
       and o.gmt_create< '${today}'
     group by product_type;
    
     /*Write the data from AnalyticDB for MySQL back to MySQL.*/
    insert into demo_id.t_order_report_daily
    select dt,
           product_type,
           order_cnt,
           order_amt
    from company.t_order_report_daily
    where dt= '${bizdate}';
    Note

    In the sample SQL, bizdate is a system-defined variable that represents the business date. By default, its value is the task's runtime minus one day.

Publish the task flow

  1. On the task flow details page, click Try Run in the upper-left corner of the canvas.

    Click the Execution Log tab to view the results.

    • If the last line of the execution log shows status SUCCEEDED, the dry run was successful.

    • If the last line of the execution log shows status FAILED, the dry run failed.

      Note

      If the dry run fails, check the execution log to identify the failed node and the cause of the failure. Modify the node configuration and run the task again.

    After a successful run, you can also query the MySQL database to view the statistical data synchronized to the t_order_report_daily table.

  2. Configure scheduling.

    1. Click a blank area of the canvas.

    2. Click the Task Flow Information tab at the bottom of the page.

    3. In the Scheduling Settings section, turn on the Enable Scheduling switch and configure the schedule. For more information about the configuration, see Scheduling cycle configuration table.

  3. Publish the task flow. It will then run automatically based on the configured schedule.

    1. In the upper-left corner of the canvas, click Publish.

    2. In the Publish dialog box, enter a description in the Remarks field, and then click Publish.