A MySQL catalog lets you directly access tables in a MySQL instance from the Realtime Compute for Apache Flink console and use them in Flink SQL deployments. This topic shows you how to create and use a MySQL catalog.
Background
A MySQL catalog provides these features:
-
Access tables in a MySQL instance directly without manual registration using DDL statements, improving development efficiency and accuracy.
-
Use tables from the MySQL catalog as CDC source tables, sink tables, or dimension tables in Flink SQL deployments.
-
Supports ApsaraDB RDS for MySQL, PolarDB for MySQL, and self-managed MySQL databases.
-
Supports direct access to logical tables for sharded tables.
-
Works with CDAS (CREATE DATABASE AS) and CTAS (CREATE TABLE AS) statements to perform full-database sync, merged sync of sharded tables, and schema change sync for MySQL data sources.
Limitations
-
Realtime Compute for Apache Flink and the MySQL instance must be in the same VPC. To connect across different VPCs or over the internet, you must establish network connectivity. For more information, see network connectivity.
-
Catalog configuration cannot be modified after creation. To change the configuration, delete the catalog and create it again.
-
You cannot create databases or tables with Flink.
-
When used as a source, these tables support only stream read, not batch read.
NoteBefore you use a table from a MySQL catalog as a CDC source table, you must enable binary logging (Binlog) on the ApsaraDB RDS for MySQL, PolarDB for MySQL, or self-managed MySQL database. For more information, see Configure a MySQL database.
-
The catalog cannot identify tables that use PolarDB-specific syntax in their DDL statement.
For example,
PARTITION BY KEY(`idempotent_id`) PARTITIONS 16, UNIQUE KEY `uk_order_id` (`order_id`). -
For Ververica Runtime (VVR) 8.0.7 and later, you cannot use views as Flink tables.
-
Only MySQL versions 5.7 and 8.0.x are supported.
Create a MySQL catalog
You can create a MySQL catalog by using the console or an SQL command.
Console (recommended)
-
Go to the Data Management page.
-
Log on to the Realtime Compute for Apache Flink console. In the Actions column of the workspace that you want to manage, click Console.
-
In the left-side navigation pane, click Data Management.
-
-
Click Create Catalog, select MySQL, and then click Next.
-
Configure the parameters.
ImportantYou cannot modify these configuration parameters after creation. To make changes, you must delete and recreate the catalog.
Parameter
Description
Required
catalogname
A name for the MySQL catalog.
Yes
hostname
The IP address or hostname of the MySQL database.
NoteTo connect across different VPCs or over the internet, you must establish network connectivity. For more information, see network connectivity.
Yes
port
The port number of the MySQL database. Default: 3306.
No
default-database
The name of the default MySQL database.
Yes
username
The username for the MySQL database.
Yes
password
The password for the MySQL database.
To avoid exposing secrets in plaintext, we recommend using a variable. The example uses a variable named mysqlpw. For more information, see Create a variable.
Yes
-
Click OK.
The created catalog appears in the Catalogs area on the left.
SQL command
-
Go to the Scripts page.
-
Log on to the Realtime Compute for Apache Flink console. In the Actions column of the workspace that you want to manage, click Console.
-
In the left-side navigation pane, click .
-
-
Click
, click New Script, enter the File Name and Storage Location, and then click Save. -
Enter the following code.
CREATE CATALOG YourCatalogName WITH( 'type' = 'mysql', 'hostname' = 'rm-bp1gcn0q0j0******.mysql.rds.aliyuncs.com', 'port' = '3306', 'username' = 'usertest', 'password' = '${secret_values.mysqlpw}', 'default-database' = 'flinktest', 'catalog.table.metadata-columns'='table_name' );Parameter
Description
Required
YourCatalogName
A name for the MySQL catalog.
Yes
type
The catalog type. Set to
mysql.Yes
hostname
The IP address or hostname of the MySQL database.
NoteTo connect across different VPCs or over the internet, you must establish network connectivity. For more information, see network connectivity.
Yes
port
The port number of the MySQL database. Default: 3306.
No
default-database
The name of the default MySQL database.
Yes
username
The username for the MySQL database.
Yes
password
The password for the MySQL database.
To avoid exposing secrets in plaintext, we recommend using a variable. The example uses a variable named mysqlpw. For more information, see Create a variable.
Yes
property-version
The catalog property schema version. Set to
0(default) or1(recommended).Different versions may support different properties and default values. See the property descriptions for details.
Note-
Supported only in VVR 8.0.6 and later.
-
In VVR 11.1 and later, the default value is 1.
No
catalog.table.metadata-columns
Specifies the metadata columns of a MySQL CDC source table to add to the table schema when you query the table. By default, no metadata columns are added.
Separate multiple metadata columns with semicolons (;), for example:
op_ts;table_name;database_name.Note-
Supported only in VVR 6.0.5 and later.
-
Setting this property adds the specified metadata columns to the schema. Because these columns are specific to MySQL CDC source tables, you can only use tables from this catalog as source tables, not as sink or dimension tables.
No
catalog.table.treat-tinyint1-as-boolean
When fetching a table schema, specifies whether to map MySQL
TinyInt(1)andBooleanto FlinkBoolean. Valid values:-
true: Map to Boolean. -
false: Map to TINYINT.
Default value:
-
If property-version is
0, the default value istrue. -
If property-version is
1, the default value isfalse.
Note-
Supported only in VVR 8.0.4 and later.
-
We do not recommend using
TinyInt(1)in MySQL to store values other than 0 and 1. Choose an appropriate type mapping. For more information, see Type mapping.
No
-
-
Select the CREATE CATALOG statement, then click Run next to the line number on the left.
A
The following statement has been executed successfully!message indicates that the catalog was created.The SQL statement in the editor,
CREATE CATALOG myCatalog, has configuration parameters that includetype=mysql,hostname=rm-bp1gcn0q0j0**.mysql.rds.aliyuncs.com,port=3306,username=usertest,password=${secret_values.mysqlpw}(a reference to a variable),default-database=flinktest, andcatalog.table.metadata-columns=table_name.
View and delete a MySQL catalog
Console (recommended)
On the Data Management page, you can view the Name and Type of the created catalogs in the Catalog List.
-
View: In the Actions column for the catalog, click View to see the databases and tables in the catalog.
Table schema details do not display field comments.
-
Delete: In the Actions column for the catalog, click Delete.
This operation deletes only the catalog, not the underlying tables in the associated service. Active deployments that use tables from the catalog are unaffected. However, redeploying or restarting these deployments will cause an error because the tables will no longer be found. Proceed with caution.
SQL command
-
In the editor on the Scripts page, enter the following commands.
-- View the table schema in Flink. Field comments are not displayed. DESCRIBE `<catalogname>`.`<dbname>`.`<tablename>`; -- Delete the catalog. DROP CATALOG `<catalogname>`;NoteThis operation deletes only the catalog and does not delete the underlying tables in the associated service. Running deployments that use tables from the catalog are not affected. However, if you redeploy or restart a deployment, an error is reported because the table cannot be found. Proceed with caution.
-
Select the command, right-click, and then choose Run.
Running the
DESCRIBEstatement returns the schema of a table, including fields such asorderkey,custkey,order_status, andtotal_price, along with their data types and properties.
Use a MySQL catalog
Read from a MySQL source table
INSERT INTO `<othersinktable>`
SELECT ...
FROM `<mysqlcatalog>`.`<dbname>`.`<tablename>` /*+ OPTIONS('server-id' = '6000-6008') */;
When you use a table from a MySQL catalog as a CDC source table, we recommend that you use SQL hints to specify a different server-id for each Flink SQL deployment. If the source table requires a higher parallelism, you must configure server-id as a range with a size greater than or equal to the parallelism.
Read from logical tables for sharded tables
A MySQL catalog supports using regular expressions for database and table names to read data from sharded tables as a single logical table.
For example, a sharded database contains multiple tables such as user01, user02, and user99 that are distributed across databases such as db01 to db10. If all tables have compatible schemas, you can use regular expressions for the database and table names to access all the sharded user tables.
SELECT ... FROM `db.*`.`user.*` /*+ OPTIONS('server-id'='6000-6018') */;
A logical table for sharded tables returns two additional system fields: _db_name (STRING) and _table_name (STRING). These fields, together with the original primary key, form a new composite primary key that ensures uniqueness. For example, if the primary key of the user01 to user99 tables is id, the composite primary key of the logical user table is (_db_name, _table_name, id).
A MySQL catalog supports using regular expressions to match multiple tables that you want to synchronize, which enables merged sync of sharded tables. For an example, see Merge and synchronize sharded tables.
Sync data and schema with CTAS and CDAS
Before you start a sync, confirm the supported source and sink data stores for CTAS (CREATE TABLE AS) and CDAS (CREATE DATABASE AS). For example, MongoDB cannot be used as a sink table. Deployment fails with the error: CREATE TABLE ... AS TABLE ... statement requires target catalog ... implements org.apache.flink.table.catalog.CatalogTableProvider interface.
CTAS (CREATE TABLE AS) supports single-table sync, schema change sync, merged sync for sharded tables, sync with custom computed columns, and adding CTAS statements to an existing sync deployment. For examples and details, see CREATE TABLE AS (CTAS) statement. CDAS (CREATE DATABASE AS) supports real-time sync of schema and data at the database level, including schema changes. For more information, see CREATE DATABASE AS (CDAS) statement.
-- Single-table sync: synchronizes table-level schema changes and data changes in real time.
CREATE TABLE IF NOT EXISTS `<targetcatalog>`.`<targetdbname>`.`<targettablename>`
WITH (...)
AS TABLE `<mysqlcatalog>`.`<dbname>`.`<tablename>`
/*+ OPTIONS('server-id'='6000-6018') */;
-- Full-database sync: synchronizes database-level schema changes and data changes in real time.
CREATE DATABASE `<targetcatalog>`.`<targetdbname>` WITH (...)
AS DATABASE `<mysqlcatalog>`.`<dbname>` INCLUDING ALL TABLES
/*+ OPTIONS('server-id'='6000-6018') */;
For example, to synchronize MySQL data to Hologres, see Use a Hologres catalog.
USE CATALOG holocatalog; -- Specify the target catalog.
CREATE TABLE IF NOT EXISTS holotable -- Specify the target table name. If you do not specify a database, the table is created in the catalog's default database.
WITH ('jdbcWriteBatchSize' = '1024') -- Optional. Specify parameters for the sink table.
AS TABLE mysqlcatalog.dbmysql.mysqltable
/*+ OPTIONS('server-id'='8001-8004') */; -- Specify additional parameters for the MySQL CDC source table.
Read from a MySQL dimension table
INSERT INTO `<othersinktable>`
SELECT ...
FROM `<othersourcetable>` AS e
JOIN `<mysqlcatalog>`.`<dbname>`.`<tablename>` FOR SYSTEM_TIME AS OF e.proctime AS w
ON e.id = w.id;
Write to a MySQL table
INSERT INTO `<mysqlcatalog>`.`<dbname>`.`<tablename>`
SELECT ...
FROM `<othersourcetable>`