DataWorks allows you to configure a batch synchronization task in wizard mode or script mode to synchronize data from sharded databases and tables to a single destination table. This topic provides an example of synchronizing data from sharded MySQL databases.
Prerequisite
Prepare the data sources for synchronization. For more information, see Supported data sources and read/write plug-ins.
Overview
You can use the following methods to create a batch synchronization task for sharding. The following table compares these methods.
|
Item |
Sharded data source and codeless UI (recommended) |
||
|
Codeless UI support |
Yes |
Yes |
No |
|
Rule-based table name configuration |
Supports configuring source table names by using regular expressions. At runtime, the task searches for and synchronizes matching tables. |
Not supported. |
Supports configuring table names by using a numeric range, for example, |
|
Identical table schemas required |
You can configure a missing field policy to allow some tables to have missing fields. These fields are output as NULL values. |
All tables must have identical schemas. |
|
|
Column mapping reference |
The first matching table in the meta data source of the sharded data source. |
The first table from the first data source. |
Manually written in script mode. |
|
Number of supported data sources |
A sharded data source supports up to 5,000 referenced data sources. |
We recommend that you configure no more than 50 data sources per node. |
|
|
Whether adding a data source requires modifying and deploying the node |
No node modification is required. After you modify the data source, the change takes effect on newly running instances. |
You must modify the task to add the data source and configure the table name for the data source. |
|
|
Supported data source types |
MySQL, PolarDB, PolarDB-O, OceanBase |
MySQL, PolarDB, AnalyticDB, OceanBase |
MySQL, PolarDB, AnalyticDB, SQL Server, Oracle, PostgreSQL, DM, DB2, Oracle, OceanBase |
Sharded data source and codeless UI
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 Integration.
-
In the left-side navigation pane, click Data Source, click Add Data Source, and then select a data source type. This method supports MySQL, PolarDB, and PolarDB-O.
-
Create a standard data source for each sharded database. This topic uses MySQL as an example. For detailed steps, see Configure a MySQL data source.
After the creation is complete, you can view the MySQL data source for each sharded database on the Data Source List page. For example, the data source name is MySQL_20241114, corresponding to the database
user_info_20241114. -
Create a Sharded Data Source to merge multiple standard data sources into a single data source as the source for data synchronization.
-
Click Add Data Source and select Sharded Data Source. In the Add Data Source dialog, click the Sharded Data Source tab. The available data source types include MySQL (sharded), PolarDB-O (sharded), and PolarDB (sharded).
-
Select a sharded data source type as needed and configure the relevant parameters. This topic uses MySQL (sharded) as an example.
In the Data Source Name field, enter a name such as
Database_and_Table_Sharding_of_MySQL. After the configuration is complete, click OK to create the sharded data source.Key parameter description:
-
Customize the Data Source Name.
-
Select Data Sources: Select the standard data sources that you have created for each sharded database.
-
Meta Data Source: For sharding synchronization, all data sources must have identical table schemas. Select one of the data sources as the default metadata template for retrieving the default table schema when you configure a synchronization task.
ImportantIf the table schemas across data sources are inconsistent, subsequent synchronization tasks will fail.
-
-
-
Configure a sharding synchronization task.
Configure sharding in wizard mode: For Data Source, select MySQL (sharded), and then select the sharded data source that you have created. For the complete task configuration steps, see Configure a batch synchronization task in wizard mode.
NoteThis topic uses MySQL (sharded) as an example. Select the appropriate type based on your needs.
On the network connectivity test page, the source data source name is Database_and_Table_Sharding_of_MySQL, the resource group is Serveless_Resource (China (Shanghai), pay-as-you-go, 0.5 CU), and the destination is MaxCompute(ODPS) with the data source name doc_test_project. The network connectivity test results show that all three are connected.
-
Click Next.
-
Select the tables to synchronize, click Save and Publish, and complete the remaining steps.
On the Configure Source and Destination page, in Step 2 Configure Task, the left-side Source section is configured as follows: the data source is MySQL (sharded), the table selection method is Fixed Table, the table is user_info, and the split key is id. (Sharding requires all tables to have identical schemas; otherwise, execution errors occur.) The right-side Destination section is configured as follows: the data source is MaxCompute(ODPS), the table is user_info, the partition information is
pt=${bizdate}, the write mode is Clear existing data before writing (Insert Overwrite), and convert empty strings to NULL is set to No.
Standard data source and codeless UI
-
Create a standard data source for each sharded database. This topic uses MySQL as an example. For detailed steps, see Configure a MySQL data source.
-
Configure a sharding synchronization task.
Configure sharding in wizard mode: In the Data Source section, click + Edit Source. In the data source selection panel that appears, select and add the data sources corresponding to multiple sharded databases. For the complete task configuration steps, see Configure a batch synchronization task in wizard mode.
-
Add multiple standard data sources to Selected Data Sources and click Confirm.
-
Click Next.
-
Select the tables to synchronize from each data source, click Save and Publish, and complete the remaining steps.
ImportantIn this configuration, the Data Sources for Sharded Databases Use Same Account and Password option is selected by default. This means that all sharded data sources will use the account and password configured in the first data source to access the databases. If your sharded databases use different accounts and passwords, clear this option so that each data source uses its own account and password.
On the Configure Source and Destination page, the left-side Source section has three MySQL sharded data sources configured (such as MySQL_20241112, MySQL_20241114, and MySQL_20241113), and each data source selects the user_info table. The page displays a warning: "Sharding requires all tables to have identical schemas; otherwise, execution errors occur." The right-side Destination section selects the MaxCompute(ODPS) data source, the table is user_info, the partition information is set to
pt=${bizdate}, and the write mode is Clear existing data before writing (Insert Overwrite).
Code editor
-
Configure a sharding synchronization task.
Configure sharding in script mode: The following is a script example. For the complete task configuration steps, see Configure a batch synchronization task in script mode.
ImportantBefore running the task, remove the comments from the following code.
{ "type":"job", "version":"2.0", "steps":[ { "stepType":"mysql", "parameter":{ "envType":0, "column":[ "id", "name" ], "socketTimeout":3600000, "tableComment":"", "connection":[ //Configure connections based on the number of sharded databases { "datasource":"datasourceName1", //Data source 1 for sharded databases and tables "table":[ //Table list 1 for sharded databases and tables "tb1" ] }, { "datasource":"datasourceName2", //Data source 2 for sharded databases and tables "table":[ //Table list 2 for sharded databases and tables "tb2", "tb3" ] } ], "useSpecialSecret":true,//Each data source uses its own password "where":"", "splitPk":"id", "encoding":"UTF-8" }, "name":"Reader", "category":"reader" }, { "stepType":"odps", "parameter":{ "partition":"pt=${bizdate}", "truncate":true, "datasource":"odpsname", "envType":0, "isSupportThreeModel":false, "column":[ "id", "name" ], "emptyAsNull":false, "tableComment":"", "table":"t1", "consistencyCommit":false }, "name":"Writer", "category":"writer" } ], "setting":{ "executeMode":null, "errorLimit":{ "record":"" }, "speed":{ "concurrent":2, "throttle":false } }, "order":{ "hops":[ { "from":"Reader", "to":"Writer" } ] } }
FAQ
After a sharded data source is created, it is not visible in the data source drop-down list of the synchronization node, and the API returns error code 610002
Symptom: After a sharded data source is created, it is not visible in the data source drop-down list of the synchronization node. The API /di/getDataSourceList returns code:610002.
Cause: This is a known issue caused by dirty data in the system. It only affects sharded data sources. Standard data sources such as MySQL and PolarDB are not affected.
Solution: Submit a ticket with the workspace ID and data source name. The DataWorks backend team will investigate and clean up the dirty data.
After an employee leaves the company, sharding synchronization tasks fail, and the configured sharded data source cannot be found in the synchronization node
Symptom: After an employee leaves the company, the sharding synchronization tasks that the employee created change from running normally to failed. The previously configured sharded data source cannot be found in the synchronization node, but the data source is still visible in the data source center with a successful connectivity test. Changing the task owner does not resolve the issue.
Cause: This issue is caused by dirty data in the system, which is related to changes in the account status (such as departure) of the data source creator. The dirty data prevents the sharded data source from being returned properly by the data source list API for synchronization nodes.
Solution: Submit a ticket with the workspace ID and data source name. The DataWorks backend team will investigate and clean up the dirty data. After the cleanup is complete, the sharded data source will become visible again, and the synchronization tasks will run normally.