Develop a Flink CDC data ingestion job

更新时间:
复制 MD 格式

Realtime Compute for Apache Flink allows you to create Flink CDC data ingestion jobs using a YAML file to synchronize data from a source to a sink. This topic describes the steps to develop a Flink CDC data ingestion job.

Background

Using YAML configurations, you can easily define complex ETL pipelines that are automatically converted into Flink execution logic. Flink CDC data ingestion provides a powerful data integration solution based on Flink CDC. This solution efficiently supports full-database synchronization, single-table synchronization, synchronization of sharded databases and tables, automatic discovery of new tables, schema change handling, and custom computed columns. It also supports ETL processing, WHERE clause filtering, and column pruning. This declarative approach significantly simplifies the data integration process and improves efficiency and reliability.

Advantages of Flink CDC

In Realtime Compute for Apache Flink, you can develop a Flink CDC data ingestion job, a SQL job, or a DataStream job to perform data synchronization. The following sections describe the advantages of using a Flink CDC data ingestion job over the other two options.

Flink CDC vs. Flink SQL

Flink CDC data ingestion jobs and SQL jobs use different data types for data transmission:

  • SQL jobs transmit data as RowData. Each RowData object has its own change type, which includes four main types: insert (+I), update before (-U), update after (+U), and delete (-D).

  • Flink CDC uses SchemaChangeEvent to convey schema change information, such as creating a table, adding a column, or truncating a table. It uses DataChangeEvent to convey data changes, such as inserts, updates, and deletes. An update message contains both the before and after content, allowing you to write the original change data to the sink.

The following table lists the advantages of Flink CDC data ingestion jobs over SQL jobs.

Flink CDC ingestion

Flink SQL

Automatic schema detection and full database synchronization

Requires manual CREATE TABLE and INSERT statements

Supports multiple policies for schema changes

Does not support schema changes

Preserves the original changelog

Disrupts the original changelog structure

Supports reading from and writing to multiple tables

Reads from and writes to a single table

Compared to CTAS/CDAS statements, Flink CDC jobs offer more powerful features, including:

  • Immediate synchronization of upstream schema changes, without waiting for new data writes to trigger the synchronization.

  • Preservation of the original changelog, ensuring that update messages are not split.

  • Synchronization of more types of schema changes, such as TRUNCATE TABLE and DROP TABLE.

  • Flexible table mapping and definition of sink table names.

  • Flexible and configurable schema evolution behaviors.

  • Data filtering using WHERE clauses.

  • Support for column pruning.

Flink CDC vs. Flink DataStream

The following table lists the advantages of Flink CDC data ingestion jobs over DataStream jobs.

Flink CDC ingestion

Flink DataStream

Accessible to users of all skill levels.

Requires expertise in Java and distributed systems.

Hides underlying complexity and simplifies development.

Requires knowledge of the Flink framework.

Easy-to-learn YAML format.

Requires knowledge of tools like Maven for dependency management.

High reusability for existing jobs.

Difficult to reuse existing code.

Limitations

  • Use Ververica Runtime (VVR) 11.1 or later to develop Flink CDC data ingestion jobs. If you need to use VVR 8.x, use VVR 8.0.11.

  • Each job supports only one source and one sink. To read from multiple sources or write to multiple sinks, you must create multiple Flink CDC jobs.

  • Flink CDC jobs cannot be deployed to a session cluster.

  • Flink CDC data ingestion jobs do not support automatic tuning.

Flink CDC data ingestion connectors

For details about supported source and sink connectors for Flink CDC data ingestion, see Supported connectors.

Create a Flink CDC data ingestion job

From a template

  1. Log on to the Realtime Compute for Apache Flink console.

  2. In the Actions column for the target workspace, click Console.

  3. In the left-side navigation pane, choose Development > Data Ingestion.

  4. Click image and then click New Draft with Template.

  5. Select a data synchronization template.

    Currently, only the MySQL to StarRocks, MySQL to Paimon, and MySQL to Hologres templates are available.

  6. Enter the job information, such as draft name, location, and engine version, and then click OK.

  7. Configure the source and sink information for the Flink CDC job.

    For parameter configuration details, see the documentation for the relevant connectors.

From a CTAS/CDAS job

Important
  • If a job contains multiple CXAS statements, Flink detects and converts only the first one.

  • Due to differences in built-in function support between Flink SQL and Flink CDC, the generated transform rules may not work as-is. You must review and adjust them as needed.

  • If the source is MySQL and an original CTAS/CDAS job is still running, you must adjust the server-id for the source in the Flink CDC data ingestion job to prevent conflicts.

  1. Log on to the Realtime Compute for Apache Flink console.

  2. In the Actions column for the target workspace, click Console.

  3. In the left-side navigation pane, choose Development > Data Ingestion.

  4. Click image, and then click New Draft from CTAS/CDAS. Select the target CTAS or CDAS job, and then click OK.

    On the selection page, the system displays only valid CTAS and CDAS jobs. Regular ETL jobs and drafts with syntax errors are not displayed.

  5. Enter the job information, such as draft name, location, and engine version, and then click OK.

From open-source Flink CDC

  1. Log on to the Realtime Compute for Apache Flink console.

  2. In the Actions column for the target workspace, click Console.

  3. In the left-side navigation pane, choose Development > Data Ingestion.

  4. Click image, and then select New Draft. Enter the name and engine version, and then click Create.

  5. Paste the YAML code from your open-source Flink CDC job into the editor.

  6. (Optional) Click Validate.

    This option checks for syntax errors, network connectivity issues, and permission problems.

From scratch

  1. Log on to the Realtime Compute for Apache Flink console.

  2. In the Actions column for the target workspace, click Console.

  3. In the left-side navigation pane, choose Development > Data Ingestion.

  4. Click image, and then select New Draft. Enter the name and engine version, and then click Create.

  5. Configure the Flink CDC job using YAML. Example:

    # Required
    source:
      # The type of the data source.
      type: <Replace with your source connector type>
      # The configuration for the data source. For details about configuration items, see the documentation for the corresponding connector.
      ...
    # Required
    sink:
      # The type of the sink.
      type: <Replace with your sink connector type>
      # The configuration for the sink. For details about configuration items, see the documentation for the corresponding connector.
      ...
    # Optional
    transform:
      # A transform rule for the flink_test.customers table.
      - source-table: flink_test.customers
        # Projection configuration. Specifies the columns to synchronize and performs data transformations.
        projection: id, username, UPPER(username) as username1, age, (age + 1) as age1, test_col1, __schema_name__ || '.' || __table_name__ identifier_name
        # Filter condition. Synchronizes only data where id is greater than 10.
        filter: id > 10
        # Description of the transform rule.
        description: append calculated columns based on source table
    # Optional
    route:
      # A route rule that specifies the mapping between the source table and the sink table.
      - source-table: flink_test.customers
        sink-table: db.customers_o
        # Description of the route rule.
        description: sync customers table
      - source-table: flink_test.customers_suffix
        sink-table: db.customers_s
        # Description of the route rule.
        description: sync customers_suffix table
    # Optional
    pipeline:
      # The name of the job.
      name: MySQL to Hologres Pipeline
    Note

    In a Flink CDC job, separate a key and its value with a colon and a space. The format is Key: Value.

    The following table describes the code blocks.

    Required

    Module

    Description

    Yes

    source

    The starting point of the data pipeline. Flink CDC captures change data from the source.

    Note
    • Currently, MySQL is the only supported source. For specific configuration details, see MySQL connector.

    • You can use variables to manage sensitive information. For more information, see Variable Management.

    sink

    The endpoint of the data pipeline. Flink CDC transmits the captured data changes to the sink system.

    Note

    No

    pipeline

    (data pipeline)

    Defines basic configurations for the entire data pipeline job, such as the pipeline name.

    transform

    Specifies data transformation rules for operating on data as it flows through the Flink pipeline. It supports ETL processing, WHERE clause filtering, column pruning, and computed columns.

    If you need to transform raw change data captured by Flink CDC to suit specific downstream systems, use the transform block.

    route

    If this module is not configured, the job performs full database or target table synchronization by default.

    In some cases, you may need to send captured change data to different destinations based on specific rules. The route module allows you to flexibly specify the mapping relationship between source and sink, sending data to different targets.

    For more information about the syntax and configuration of each module, see Reference for Flink CDC data ingestion jobs.

    The following code provides an example of synchronizing all tables from the app_db database in MySQL to a database in Hologres.

    source:
      type: mysql
      hostname: <hostname>
      port: 3306
      username: ${secret_values.mysqlusername}
      password: ${secret_values.mysqlpassword}
      tables: app_db.\.*
      server-id: 5400-5404
      # (Optional) Synchronize data from tables newly created in the incremental phase.
      scan.binlog.newly-added-table.enabled: true
      # (Optional) Synchronize table and field comments.
      include-comments.enabled: true
      # (Optional) Prioritize dispatching unbounded chunks to prevent potential TaskManager OutOfMemory issues.
      scan.incremental.snapshot.unbounded-chunk-first.enabled: true
      # (Optional) Enable parsing filters to accelerate reading.
      scan.only.deserialize.captured.tables.changelog.enabled: true
    sink:
      type: hologres
      name: Hologres Sink
      endpoint: <endpoint>
      dbname: <database-name>
      username: ${secret_values.holousername}
      password: ${secret_values.holopassword}
    pipeline:
      name: Sync MySQL Database to Hologres
  6. (Optional) Click Validate.

    This option checks for syntax errors, network connectivity issues, and permission problems.

Related topics