Data Transmission Service (DTS) provides the extract, transform, and load (ETL) feature to help you process streaming data in real time. For more information, see What is ETL?In combination with the efficient data replication capabilities of DTS, ETL can be used to extract, transform, process, and load streaming data. You can use these features to filter data, mask data, record the modification time of data, and audit data modifications. This topic describes how to configure ETL in a data synchronization task.

Background information

DTS is used for data migration and real-time data transmission between data sources. In some cases, you may need to transform or filter real-time data before the data is written to a database. To meet such requirements, DTS provides the ETL feature and allows you to use domain-specific language (DSL) statements to process data in a flexible manner. For more information about DSL, see the Overview of DSL section of this topic.

You can configure ETL by using one of the following methods:
Note You can configure ETL in both data migration and data synchronization tasks. This topic describes how to configure ETL in a data synchronization task. You can also follow the procedure to configure ETL in a data migration task.

Supported databases

The following table describes the source and destination databases that are supported by the ETL feature.

Source databaseDestination database
SQL Server
  • AnalyticDB for MySQL V3.0
  • SQL Server
  • MySQL
  • PolarDB for MySQL
MySQL
  • AnalyticDB for MySQL V3.0
  • AnalyticDB for PostgreSQL
  • Kafka
  • MySQL
  • PolarDB for MySQL
Self-managed Oracle database
  • AnalyticDB for MySQL V3.0
  • AnalyticDB for PostgreSQL
  • Kafka
  • MaxCompute
  • PolarDB-X 2.0
  • PolarDB for PostgreSQL(Compatible with Oracle)
PolarDB for MySQL
  • AnalyticDB for MySQL V3.0
  • MySQL
  • PolarDB for MySQL
PolarDB for Oracle
  • AnalyticDB for MySQL V3.0
  • PolarDB for PostgreSQL(Compatible with Oracle)
PolarDB-X 1.0
  • Kafka
  • Tablestore
Self-managed Db2 for LUW databaseMySQL
Self-managed Db2 for i databaseMySQL
PolarDB for PostgreSQL
  • PolarDB for PostgreSQL
  • PostgreSQL
PostgreSQL
  • PolarDB for PostgreSQL
  • PostgreSQL
TiDB
  • PolarDB for MySQL
  • MySQL
  • AnalyticDB for MySQL V3.0

Configure ETL when you create a data synchronization task

Usage notes

If the ETL script that you configure contains the operation to add a column, you must manually add a column to the destination table. Otherwise, the ETL script does not take effect. For example, if you configure the ETL script script:e_set(`new_column`, dt_now()), you must manually add the new_column column to the destination table.

  1. Create a data synchronization task. For more information, see Overview of data synchronization scenarios.
  2. In the Advanced Settings section of the Configure Objects and Advanced Settings step, set the Configure ETL parameter to Yes. In the code editor, enter data processing statements based on the DSL syntax.
    Note If you want to drop entries whose values of the id column are greater than 3 by using DSL, you can use the script:e_if(op_gt(`id`, 3), e_drop()) statement. In this statement, op_gt is an expression function used to determine whether an entry is greater than a specific value. id is a variable. This way, entries whose values of the id column are greater than 3 are filtered out.
    Configure ETL
  3. Click Next: Save Task Settings and Precheck and complete the subsequent steps.

Modify the ETL configurations of an existing data synchronization task

You can modify the ETL configurations in the following scenarios:
  • If the Configure ETL parameter is set to No for an existing data synchronization task, you can set this parameter to Yes and enter DSL statements.
  • If the Configure ETL parameter is set to Yes for an existing data synchronization task, you can modify the existing DSL statements or set this parameter to No.
    Important Before you modify the existing DSL statements, you must move the objects to be synchronized from the Selected Objects section to the Source Objects section and then add these objects to the Selected Objects section again.
Usage notes
  • For an existing data synchronization task, you cannot modify the destination table schema by modifying the ETL configurations of the task. You can modify the destination table schema only before the data synchronization task is started.
  • If you modify the ETL configurations, the data synchronization task may be interrupted. Proceed with caution.
  • The modification of the ETL configurations takes effect only on the incremental data generated after the modification.
  • The fields that are configured in the DSL script cannot be the fields that are filtered out by filter conditions. Otherwise, the task becomes abnormal.
  1. Go to the Data Synchronization page of the new DTS console.
  2. Find the data synchronization task whose ETL configurations you want to modify and click Modify ETL Configurations next to the More icon icon. Modify ETL configurations
  3. In the Advanced Settings section of the Configure Objects and Advanced Settings step, set the Configure ETL parameter to Yes. In the code editor, enter data processing statements based on the DSL syntax.
    Note If you want to drop entries whose values of the id column are greater than 3 by using DSL, you can use the script:e_if(op_gt(`id`, 3), e_drop()) statement. In this statement, op_gt is an expression function used to determine whether an entry is greater than a specific value. id is a variable. This way, entries whose values of the id column are greater than 3 are filtered out.
    Configure ETL
  4. Click Next: Precheck and Start Task and complete the subsequent steps.

Overview of DSL

DSL is a scripting language that is designed to process data in data synchronization scenarios. You can use conditional functions to process data of the string, date, and numeric types. DSL boasts the following characteristics that help you process data in a flexible manner:
  • Various functions: DSL provides a variety of functions and supports combined functions.
  • Simple syntax: DSL is easy to use. For more information about how to use DSL to filter, transform, and mask data, see Typical scenarios.
  • High efficiency: DSL has a minimal effect on the data synchronization performance because DSL uses the code generation mechanism.
Note The syntax of DSL for DTS has something in common with the syntax of DSL for Log Service. DSL supports JSON functions, and does not support functions for event splitting. For more information about the syntax of DSL for Log Service, see Syntax overview.

Typical scenarios

  • Filter data
    • Filter out data by a numeric column: If an entry whose value of the id column is greater than 10000, drop this entry so that it is not synchronized to the destination database. Example: e_if(op_gt(`id`, 10000), e_drop).
    • Filter out data by a specific string: If an entry whose value of the name column contains "hangzhou", drop this entry so that it is not synchronized to the destination database. Example: e_if(str_contains(`name`, "hangzhou"), e_drop).
    • Filter out data by date: If an entry whose timestamp of the order column is earlier than a specific point in time, drop this entry so that it is not synchronized to the destination database. Example: e_if(op_lt(`order_timestamp`, "2015-02-23 23:54:55"), e_drop).
    • Filter out data by multiple conditions:
      • If an entry whose value of the id column is greater than 1000 and value of the name column contains "hangzhou", drop this entry so that it is not synchronized to the destination database. Example: e_if(op_and(str_contains(`name`, "hangzhou"), op_gt(`id`, 1000)), e_drop()).
      • If an entry whose value of the id column is greater than 1000 or value of the name column contains "hangzhou", drop this entry so that it is not synchronized to the destination database. Example: e_if(op_or(str_contains(`name`, "hangzhou"), op_gt(`id`, 1000)), e_drop()).
  • Mask data
    • Mask the last four digits of a mobile phone number with four asterisks (*). Example: e_set(`phone`, str_mask(`phone`, 7, 10, '*')).
  • Record the time when data is modified
    • Add a column to all tables: If the value of the __OPERATION__ variable is INSERT, UPDATE, or DELETE, a column named dts_sync_time whose value is the same as the __COMMIT_TIMESTAMP__ variable of logs is added to all tables in the source database.
      e_if(op_or(op_or(
              op_eq(__OPERATION__, __OP_INSERT__),
              op_eq(__OPERATION__, __OP_UPDATE__)),
              op_eq(__OPERATION__, __OP_DELETE__)),
          e_set(dts_sync_time, __COMMIT_TIMESTAMP__))
    • Add a column to a specific table: If the value of the __OPERATION__ variable is INSERT, UPDATE, or DELETE, a column named dts_sync_time whose value is the same as the __COMMIT_TIMESTAMP__ variable of logs is added to the dts_test_table table in the source database.
      e_if(op_and(
            op_eq(__TB__,'dts_test_table'),
            op_or(op_or(
              op_eq(__OPERATION__,__OP_INSERT__),
              op_eq(__OPERATION__,__OP_UPDATE__)),
              op_eq(__OPERATION__,__OP_DELETE__))),
            e_set(dts_sync_time,__COMMIT_TIMESTAMP__))
      Note To perform the preceding operations, you must add the dts_sync_time column to the corresponding tables in the destination database before the data synchronization task is started.
  • Audit data modifications
    • Record the type and time of the data modifications in tables: 1. Record the data modification type in the operation_type column of the destination database. 2. Record the time when data is modified in the updated column of the destination database.
      e_compose(
          e_switch(
              op_eq(__OPERATION__,__OP_DELETE__), e_set(operation_type, 'DELETE'),
              op_eq(__OPERATION__,__OP_UPDATE__), e_set(operation_type, 'UPDATE'),
              op_eq(__OPERATION__,__OP_INSERT__), e_set(operation_type, 'INSERT')),
          e_set(updated, __COMMIT_TIMESTAMP__),
          e_set(__OPERATION__,__OP_INSERT__)
      )
      Note You must add the operation_type and updated columns to the tables in the destination database before the data synchronization task is started.

DSL syntax

Constants and variables
  • Constants
    Data typeExample
    INT123
    FLOAT123.4
    STRING"hello1_world"
    BOOLEANtrue or false
    DATETIME'2021-01-01 10:10:01'
  • Variables
    VariableDescriptionData typeExample
    __TB__The name of the table.STRINGtable
    __DB__The name of the database.STRINGmydb
    __OPERATION__The type of the operation.STRING__OP_INSERT__,__OP_UPDATE__,__OP_DELETE__
    __COMMIT_TIMESTAMP__The time when the transaction was committed.DATETIME'2021-01-01 10:10:01'
    `column`The name of the column.STRING`id` or `name`
Expression functions
  • Arithmetic operations
    OperationSyntaxValid valueReturn valueExample
    Additionop_sum(value1, value2)
    • value1: an integer or a floating-point number
    • value2: an integer or a floating-point number
    If value1 and value2 are integers, an integer is returned. Otherwise, a floating-point number is returned. op_sum(`col1`, 1.0)
    Subtractionop_sub(value1, value2)
    • value1: an integer or a floating-point number
    • value2: an integer or a floating-point number
    If value1 and value2 are integers, an integer is returned. Otherwise, a floating-point number is returned. op_sub(`col1`, 1.0)
    Multiplicationop_mul(value1, value2)
    • value1: an integer or a floating-point number
    • value2: an integer or a floating-point number
    If value1 and value2 are integers, an integer is returned. Otherwise, a floating-point number is returned. op_mul(`col1`, 1.0)
    Divisionop_div_true(value1, value2)
    • value1: an integer or a floating-point number
    • value2: an integer or a floating-point number
    If value1 and value2 are integers, an integer is returned. Otherwise, a floating-point number is returned. op_div_true(`col1`, 2.0). In this example, if the col1 value is 15, 7.5 is returned.
    Moduloop_mod(value1, value2)
    • value1: an integer or a floating-point number
    • value2: an integer or a floating-point number
    If value1 and value2 are integers, an integer is returned. Otherwise, a floating-point number is returned. op_mod(`col1`, 10). In this example, if the col1 value is 23, 3 is returned.
  • Logical operations
    OperationSyntaxValid valueReturn valueExample
    Equal toop_eq(value1, value2)
    • value1: an integer, a floating-point number, or a string
    • value2: an integer, a floating-point number, or a string
    true or falseop_eq(`col1`, 23)
    Greater thanop_gt(value1, value2)
    • value1: an integer, a floating-point number, or a string
    • value2: an integer, a floating-point number, or a string
    true or falseop_gt(`col1`, 1.0)
    Less thanop_lt(value1, value2)
    • value1: an integer, a floating-point number, or a string
    • value2: an integer, a floating-point number, or a string
    true or falseop_lt(`col1`, 1.0)
    Greater than or equal toop_ge(value1, value2)
    • value1: an integer, a floating-point number, or a string
    • value2: an integer, a floating-point number, or a string
    true or falseop_ge(`col1`, 1.0)
    Less than or equal toop_le(value1, value2)
    • value1: an integer, a floating-point number, or a string
    • value2: an integer, a floating-point number, or a string
    true or falseop_le(`col1`, 1.0)
    ANDop_and(value1, value2)
    • value1: a Boolean value
    • value2: a Boolean value
    true or falseop_and(`is_male`, `is_student`)
    ORop_or(value1, value2)
    • value1: a Boolean value
    • value2: a Boolean value
    true or falseop_or(`is_male`, `is_student`)
  • String functions
    OperationSyntaxValid valueReturn valueExample
    Format strings and append stringsstr_format(format, value1, value2, value3, ...)
    • format: a string. Braces ({}) are used as placeholders. Example: "part1: {}, part2: {}".
    • value1: an arbitrary value.
    • value2: an arbitrary value.
    The string after the format operation.str_format("part1: {}, part2: {}", `col1`, `col2`). In this example, if the col1 value is ab and the col2 value is 12, "part1: ab, part2: 12" is returned.
    Replace stringsstr_replace(original, oldStr, newStr, count)
    • original: the original string.
    • oldStr: the string to be replaced.
    • newStr: the string after the replacement.
    • count: an integer that indicates the maximum number of times that a string can be replaced. A value of -1 indicates that all oldStr is replaced with newStr.
    The string after the replace operation.Example 1: str_replace(`name`, "a", 'b', 1). In this example, if the name is aba, bba is returned. Example 2: str_replace(`name`, "a", 'b', -1). In this example, if the name is aba, bbb is returned.
    Remove specific characters at the start and end of a stringstr_strip(string_val, charSet)
    • string_val: the original string.
    • char_set: the set of the first characters and the last characters of the string.
    The string after the remove operation.str_strip(`name`, 'ab'). In this example, if the name is axbzb, xbz is returned.
    Convert strings to lowercase lettersstr_lower(value1)value1: a column of the string type or a string constant.The string after the convert operation.str_lower(`str_col`)
    Convert strings to uppercase lettersstr_upper(value1)value1: a column of the string type or a string constant.The string after the convert operation.str_upper(`str_col`)
    Count stringsstr_count(str, pattern)
    • str: a column of the string type or a string constant.
    • pattern: the substring to query.
    The number of times for which the substring appears. str_count(`str_col`, 'abc'). In this example, if the str_col value is zabcyabcz, 2 is returned.
    Query stringsstr_find(str, pattern)
    • str: a column of the string type or a string constant.
    • pattern: the substring to query.
    The position in which the substring matches for the first time. If no match is found, -1 is returned. str_find(`str_col`, 'abc'). In this example, if the str_col value is xabcy, 1 is returned.
    Determine whether a string contains only letters str_isalpha(str)str: a column of the string type or a string constant.true or falsestr_isalpha(`str_col`)
    Determine whether a string contains only digitsstr_isdigit(str)str: a column of the string type or a string constant.true or false str_isdigit(`str_col`)
    Mask part of a string with specific characters. This operation can be used for data masking. For example, mask the last four digits of a mobile phone number with four asterisks (*). str_mask(str, start, end, maskStr)
    • str: a column of the string type or a string constant.
    • start: an integer that indicates the start position of the masking. The minimum value is 0.
    • end: an integer that indicates the end position of the masking. The maximum value is the length of the string minus 1.
    • maskStr: a string. The length is 1. Example: #.
    The string whose part from start to end is masked with the specified characters. str_mask(`phone`, 7, 10, '#')
    Query the DateTime value of the current time zonedt_now()NoneThe DateTime value of the current time zone.e_set(`dt_col`, dt_now())
    Query the DateTime value of the current time zone in UTCdt_utcnow()NoneThe DateTime value of the current time zone in UTC.e_set(`dt_col`, dt_utcnow())
Global functions
  • Flow control functions
    OperationSyntaxDescriptionExample
    IF statemente_if(bool_expr, func_invoke)
    • bool_expr: a Boolean constant or a function. A Boolean constant: true or false. A function: op_gt(`id`, 10).
    • func_invoke: a function. Valid values: e_drop, e_keep, e_set, e_if, and e_compose.
    e_if(op_gt(`id`, 10), e_drop()). In this example, if an entry whose value of the id column is greater than 10, this entry is dropped.
    IF ELSE statemente_if_else(bool_expr, func_invoke1, func_invoke2)
    • bool_expr: a Boolean constant or a function. A Boolean constant: true or false. A function: op_gt(`id`, 10).
    • func_invoke1: a function. Invoke this function if the condition is true.
    • func_invoke2: a function. Invoke this function if the condition is false.
    e_if_else(op_gt(`id`, 10), e_set(`tag`, 'large'), e_set(`tag`, 'small')). In this example, if an entry whose value of the id column is greater than 10, the tag column is set to large. Otherwise, the tag column is set to small.
    SWITCH statement that contains multiple conditions and a default operation s_switch(condition1, func1, condition2, func2, ..., default = default_func)
    • condition1: a Boolean constant or a function. A Boolean constant: true or false. A function: op_gt(`id`, 10).
    • func_invoke: a function. If condition1 is true, invoke this function and finish the statement. Otherwise, proceed to the next condition.
    • default_func: a function. If the preceding conditions are false, invoke this function.
    e_switch(op_gt(`id`, 100), e_set(`str_col`, '>100'), op_gt(`id`, 90), e_set(`str_col`, '>90'), default=e_set(`str_col`, '<=90'))
    Combination of multiple operationse_compose(func1, func2, func3, ...)
    • func1: a function. Valid values: e_set, e_drop, and e_if.
    • func2: a function. Valid values: e_set, e_drop, and e_if.
    e_compose(e_set(`str_col`, 'test'), e_set(`dt_col`, dt_now())). In this example, the value of the str_col column is set to test, and the value of the dt_col column is set to the current time.
  • Data manipulation functions
    OperationSyntaxDescriptionExample
    Drop an entry so that it is not synchronized to the destination databasee_drop()Nonee_if(op_gt(`id`, 10), e_drop()). In this example, entries whose values of the id column are greater than 10 are dropped.
    Retain an entry so that it is synchronized to the destination databasee_keep(condition)condition: a Boolean expression.e_keep(op_gt(id, 1)). In this example, only entries whose values of the id column are greater than 1 are synchronized.
    Specify the value of a columne_set(`col`, val)
    • col: the name of the column.
    • val: a constant or a function. The data type of the val value must match the data type of the col value.
    • e_set(`dt_col`, dt_now()). In this example, the value of the dt_col column is set to the current time.
    • e_set(`col1`, `col2` + 1). In this example, the value of the col1 column is set to the value of the col2 column plus 1.