All Products
Search
Document Center

DataWorks:PolarDB-X 2.0 data source

Last Updated:Jul 15, 2024

DataWorks provides PolarDB-X 2.0 Reader and PolarDB-X 2.0 Writer for you to read data from and write data to PolarDB-X 2.0 data sources. This topic describes the capabilities of synchronizing data from or to PolarDB-X 2.0 data sources.

Limits

You can use only exclusive resource groups for Data Integration to read data from or write data to PolarDB-X 2.0 data sources.

Supported versions

Batch data read and write: PolarDB-X 2.0 is supported. Data of views can be read during batch synchronization.

Supported data types

For information about all data types in PolarDB-X 2.0, see Data types. The following table provides the support status of main data types.

Data type

PolarDB-X 2.0 Reader for batch data read

PolarDB-X 2.0 Writer for batch data write

TINYINT

Supported

Supported

SMALLINT

Supported

Supported

INTEGER

Supported

Supported

BIGINT

Supported

Supported

FLOAT

Supported

Supported

DOUBLE

Supported

Supported

DECIMAL/NUMBERIC

Supported

Supported

REAL

Not supported

Not supported

VARCHAR

Supported

Supported

JSON

Supported

Supported

TEXT

Supported

Supported

MEDIUMTEXT

Supported

Supported

LONGTEXT

Supported

Supported

VARBINARY

Supported

Supported

BINARY

Supported

Supported

TINYBLOB

Supported

Supported

MEDIUMBLOB

Supported

Supported

LONGBLOB

Supported

Supported

ENUM

Supported

Supported

SET

Supported

Supported

BOOLEAN

Supported

Supported

BIT

Supported

Supported

DATE

Supported

Supported

DATETIME

Supported

Supported

TIMESTAMP

Supported

Supported

TIME

Supported

Supported

YEAR

Supported

Supported

LINESTRING

Not supported

Not supported

POLYGON

Not supported

Not supported

MULTIPOINT

Not supported

Not supported

MULTILINESTRING

Not supported

Not supported

MULTIPOLYGON

Not supported

Not supported

GEOMETRYCOLLECTION

Not supported

Not supported

Prepare a PolarDB-X 2.0 environment before data synchronization

Before you use DataWorks to synchronize data from or to a PolarDB-X 2.0 data source, you must prepare a PolarDB-X 2.0 environment. This ensures that a data synchronization task can be configured and can synchronize data from or to the PolarDB-X 2.0 data source as expected. The following information describes how to prepare a PolarDB-X 2.0 environment for data synchronization from or to a PolarDB-X 2.0 data source.

Preparation 1: Check the version of your PolarDB-X database

If the version of your PolarDB-X database is PolarDB-X 1.0, refer to DRDS (PolarDB-X 1.0) data source to prepare an environment. If the version of your PolarDB-X database is PolarDB-X 2.0, refer to this topic to prepare an environment.

Preparation 2: Prepare an account that has the required permissions

We recommend that you plan and create an account for DataWorks to access your PolarDB-X 2.0 database. To prepare such an account, perform the following steps:

  1. Optional. Log on to the PolarDB-X 2.0 console to create an account. If you already have such an account, you can skip this step.

  2. Grant the required permissions to the account.

    • Batch synchronization:

      • Batch data read: The account must have the SELECT permission.

      • Batch data write: The account must have the INSERT, DELETE, and UPDATE permissions.

    • Real-time synchronization of data in a database:

      • Privileged account: By default, binary log data can be read for real-time synchronization.

      • Standard account: Use a privileged account to grant the SELECT, REPLICATION SLAVE, and REPLICATION CLIENT permissions on a specific database to the standard account.

-- Create an account that can be used for data synchronization and specify a password. This way, you can use the account and password to access the database from any host. % indicates a host. 
-- CREATE USER 'Account for data synchronization'@'%' IDENTIFIED BY 'Password'. 
-- Grant the SELECT, REPLICATION SLAVE, and REPLICATION CLIENT permissions to the account. 
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'Account for data synchronization'@'%'; 

Develop a data synchronization task

For information about the entry point for and the procedure of configuring a data synchronization task, see the following sections. For information about the parameter settings, view the infotip of each parameter on the configuration tab of the task.

Add a data source

Before you configure a data synchronization task to synchronize data from or to a specific data source, you must add the data source to DataWorks. For more information, see Add and manage data sources.

Configure a batch synchronization task to synchronize data of a single table

Appendix: Code and parameters

Configure a batch synchronization task by using the code editor

If you use the code editor to configure a batch synchronization task, you must configure parameters for the reader and writer of the related data source based on the format requirements in the code editor. For more information about the format requirements, see Configure a batch synchronization task by using the code editor. The following information describes the configuration details of parameters for the reader and writer in the code editor.

Code for PolarDB-X 2.0 Reader

{
    "type":"job",
    "version":"2.0",// The version number. 
    "steps":[
        {
            "stepType":"polardbx20",// The plug-in name. 
            "parameter":{
               "connection": [
                  {
                      "datasource":"",
                      "table": [
                          "t1"
                      ]
                  }
              ],
              "column": [
                  "c1",
                  "c2",
                  "'const'"
              ],
              "where": "",
              "splitPk": "",
              "checkSlave": "true",
              "slaveDelayLimit": "300"
            },
            "name":"Reader",
            "category":"reader"
        },
        {
            "stepType":"stream",
            "parameter":{},
            "name":"Writer",
            "category":"writer"
        }
    ],
    "setting":{
        "errorLimit":{
            "record":"0"// The maximum number of dirty data records allowed. 
        },
        "speed":{
            "throttle":true,// Specifies whether to enable throttling. The value false indicates that throttling is disabled, and the value true indicates that throttling is enabled. The mbps parameter takes effect only when the throttle parameter is set to true. 
            "concurrent":1,// The maximum number of parallel threads. 
            "mbps":"12"// The maximum transmission rate.
        }
    },
    "order":{
        "hops":[
            {
                "from":"Reader",
                "to":"Writer"
            }
        ]
    }
}

Parameters in code for PolarDB-X 2.0 Reader

Parameter

Description

Required

Default value

datasource

The name of the data source. It must be the same as the name of the data source that you added on the data source management page.

Yes

No default value

table

The name of the table from which you want to read data. Each synchronization task can be used to synchronize data from only one table.

Yes

No default value

column

The names of the columns from which you want to read data. Specify the names in a JSON array. The default value is [*], which indicates all columns in the source table.

  • You can select specific columns to read.

  • The column order can be changed. This indicates that you can specify columns in an order different from the order specified by the schema of the source table.

  • Constants are supported. The column names must be arranged in compliance with the SQL syntax supported by PolarDB-X 2.0, such as ["id","table","1","'mingya.wmy'","'null'","to_char(a+1)","2.3","true"].

    • id: a column name.

    • table: the name of a column that contains reserved keywords.

    • 1: an integer constant.

    • 'mingya.wmy': a string constant, which is enclosed in single quotation marks (').

    • null:

      • " " indicates an empty string.

      • null indicates a null value.

      • 'null' indicates the string null.

    • to_char(a+1): a function expression that is used to calculate the length of a string.

    • 2.3: a floating-point constant.

    • true: a Boolean value.

  • The column parameter must explicitly specify all the columns from which you want to read data. This parameter cannot be left empty.

Yes

No default value

splitPk

The field that is used for data sharding when PolarDB-X 2.0 Reader reads data. If you configure this parameter, the source table is sharded based on the value of this parameter. Then, parallel threads are run to read data. This way, data can be synchronized more efficiently.

  • We recommend that you set the splitPk parameter to the name of the primary key column of the table. Data can be evenly distributed to different shards based on the primary key column, instead of being intensively distributed only to specific shards.

  • The splitPk parameter must be set to a field of an integer data type. If you set the splitPk parameter to a field of an unsupported data type, such as a string, floating point, or date data type, the setting of this parameter is ignored, and a single thread is used to read data.

  • If the splitPk parameter is not contained in code for PolarDB-X 2.0 Reader or is left empty, a single thread is used to read data.

No

No default value

where

The WHERE clause. For example, you can set this parameter to gmt_create>$bizdate to read the data that is generated on the current day.

  • You can use the WHERE clause to read incremental data. If the where parameter is not provided or is left empty, PolarDB-X 2.0 Reader reads all data.

  • Do not set the where parameter to LIMIT 10. This value does not conform to the constraints of PolarDB-X 2.0 on the SQL WHERE clause.

No

No default value

checkSlave

Specifies whether to check the latency of data synchronization between a primary instance and a read-only secondary instance. If you add a PolarDB-X 2.0 read-only instance as a data source, you must check the latency of data synchronization between the read-only instance and a primary instance before you run a data synchronization task. This helps prevent data loss caused by the latency.

No

true

slaveDelayLimit

The maximum latency of data synchronization between a primary instance and a read-only instance, in seconds. If the latency exceeds the value of this parameter, the data synchronization task fails. This helps prevent data loss caused by the latency.

No

30

Code for PolarDB-X 2.0 Writer

{
    "type":"job",
    "version":"2.0",// The version number. 
    "steps":[ 
        {
            "stepType":"stream",
            "parameter":{},
            "name":"Reader",
            "category":"reader"
        },
        {
            "stepType":"PolarDB-X 2.0",// The plug-in name. 
            "parameter":{
                "postSql":[],// The SQL statement that you want to execute after the synchronization task is run. 
                "datasource":"",// The name of the data source. 
                "column":[// The names of the columns. 
                    "id",
                    "value"
                ],
                "writeMode":"insert",// The write mode. Valid values: insert and replace. 
                "batchSize":1024,// The number of data records to write at a time. 
                "table":"",// The name of the table. 
                "preSql":[ 
                     "delete from XXX;" // The SQL statement that you want to execute before the synchronization task is run. 
                   ]
            },
            "name":"Writer",
            "category":"writer"
        }
    ],
    "setting":{
        "errorLimit":{// The maximum number of dirty data records allowed. 
            "record":"0"
        },
        "speed":{
            "throttle":true,// Specifies whether to enable throttling. The value false indicates that throttling is disabled, and the value true indicates that throttling is enabled. The mbps parameter takes effect only when the throttle parameter is set to true. 
            "concurrent":1, // The maximum number of parallel threads. 
            "mbps":"12"// The maximum transmission rate. You can specify a maximum transmission rate to prevent heavy read workloads on the source or heavy write workloads on the destination. 
        }
    },
    "order":{
        "hops":[
            {
                "from":"Reader",
                "to":"Writer"
            }
        ]
    }
}

Parameters in code for PolarDB-X 2.0 Writer

Parameter

Description

Required

Default value

datasource

The name of the data source. It must be the same as the name of the data source that you added on the data source management page.

Yes

No default value

table

The name of the table to which you want to write data.

Yes

No default value

writeMode

The write mode. Valid values:

  • insert into: If a primary key conflict or unique index conflict occurs, data cannot be written to the conflicting rows, and the data that is not written to these rows is regarded as dirty data. If you configure a synchronization task by using the code editor, set writeMode to insert.

  • replace into: If no primary key conflict or unique index conflict occurs, the data is processed in the same way as that when you set this parameter to insert into. If a conflict occurs, the original rows are deleted, and new rows are inserted. This indicates that all fields of the original rows are replaced. If you create a synchronization task by using the code editor, set writeMode to replace.

No

insert

column

The names of the columns to which you want to write data. Separate the names with commas (,), such as "column": ["id", "name", "age"]. If you want to write data to all the columns in the destination table, set this parameter to an asterisk (*), such as "column": ["*"].

Yes

No default value

preSql

The SQL statement that you want to execute before the synchronization task is run. You can execute only one SQL statement on the codeless UI and multiple SQL statements in the code editor. For example, you can set this parameter to the following SQL statement to delete outdated data before the synchronization task is run:

-- tablename indicates the table name.
truncate table tablename;
Note

If you configure multiple SQL statements, the statements are not executed in the same transaction.

No

No default value

postSql

The SQL statement that you want to execute after the synchronization task is run. You can execute only one SQL statement on the codeless UI and multiple SQL statements in the code editor. For example, you can set this parameter to the following statements to add a timestamp after the synchronization task is run:

-- tablename indicates the table name, and colname indicates the column name.
alter table tablename 
add colname timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Note

If you configure multiple SQL statements, the statements are not executed in the same transaction.

No

No default value

batchSize

The number of data records to write at a time. Set this parameter to an appropriate value based on your business requirements. This greatly reduces the interactions between Data Integration and PolarDB-X 2.0 and increases throughput. If you set this parameter to an excessively large value, an out of memory (OOM) error may occur during data synchronization.

No

256