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
Prepare the data and tables
-
Create the order, product, and reporting tables in your MySQL database.
Log in to DMS 5.0.
Move the pointer over the
icon in the upper-left corner of the DMS console and choose . NoteIf you use the DMS console in normal mode, choose in the top navigation bar.
-
In the Please select the database first dialog box, search for and select your MySQL database, and then click Confirm.
-
In the MySQL database, create the order table
t_order, the product tablet_product, and the reporting tablet_order_report_dailyto store synchronized analysis results.-
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_ordertable: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_ordertable is as follows:The table has eight columns:
id,product_id,gmt_create,gmt_modified,customer_id,price,status, andprovince. Theidcolumn is the auto-incrementing primary key. Theprovincecolumn can be null. Thegmt_createandgmt_modifiedcolumns default toCURRENT_TIMESTAMP. -
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_producttable: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_producttable is as follows:The table has five columns:
id,name,type,gmt_create, andgmt_modified. Theidcolumn is the auto-incrementing primary key. Thegmt_createandgmt_modifiedcolumns default toCURRENT_TIMESTAMP. -
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_dailytable: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_dailytable is as follows:The table has four columns:
dt,product_type,order_cnt, andorder_amt. Thedtcolumn is the primary key.
-
-
In the AnalyticDB for MySQL data warehouse, create the order, product, and reporting tables to receive data and store analysis results.
-
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.
-
In the AnalyticDB for MySQL data warehouse, create the
t_orderandt_producttables to receive synchronized data, and thet_order_report_dailytable to store analysis results.NoteFor the SQL statements to create the
t_order,t_product, andt_order_report_dailytables, see Step 4 in the preceding section. -
Populate the
t_orderandt_producttables 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_ordertable.NoteFor 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_producttable.
-
-
Create a cross-database Spark SQL task
Log in to DMS 5.0.
-
In the top navigation bar, choose .
NoteIf you use the DMS console in simple mode, move the pointer over the
icon in the upper-left corner and choose . -
Create a new task flow.
-
Click Create Task Flow.
-
In the New Task Flow dialog box, enter a Task Flow Name and Description, and then click Confirm.
-
-
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
-
On the task flow details page, select the Cross-Database Spark SQL node.
-
Configure the
${today}variable to represent the current date. For more information about variables, see Variables.-
Click the Variable Setting tab.
-
Click the Node Variable tab.
-
Enter the parameters for the node variable.
Set Variable Name to
today, Time Format toyyyy-MM-dd, and the variable rule to the current date plus 1 day.
-
-
Add the MySQL database that contains the product and order data.
-
In the Database Reference section, click Add Database Reference.
-
Select the database type, search for and select the database, and then enter
demo_idas the reference alias for the database in your Spark SQL statements. For details on the configuration items, see Database configuration table. -
Click Save.
-
-
Add the AnalyticDB for MySQL data warehouse that is used for data processing.
-
Click the
icon to the right of the demo_iddatabase to add a new database reference. -
Select the database type, search for and select the database, and then enter
companyas the reference alias for the database in your Spark SQL statements. -
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.
-
-
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}';NoteIn the sample SQL,
bizdateis 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
-
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.NoteIf 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_dailytable. -
-
Configure scheduling.
-
Click a blank area of the canvas.
-
Click the Task Flow Information tab at the bottom of the page.
-
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.
-
-
Publish the task flow. It will then run automatically based on the configured schedule.
-
In the upper-left corner of the canvas, click Publish.
-
In the Publish dialog box, enter a description in the Remarks field, and then click Publish.
-