Debezium connector for Db2
Overview
The Debezium Db2 connector is based on the ASN Capture/Apply agents that enable SQL Replication in Db2. A capture agent:
- Generates change-data tables for tables that are in capture mode.
- Monitors tables in capture mode and stores change events for updates to those tables in their corresponding change-data tables.
The Debezium connector uses a SQL interface to query change-data tables for change events.
The database administrator must put the tables for which you want to capture changes into capture mode. For convenience and for automating testing, there are Debezium user-defined functions (UDFs) in C that you can compile and then use to do the following management tasks:
- Start, stop, and reinitialize the ASN agent
- Put tables into capture mode
- Create the replication (ASN) schemas and change-data tables
- Remove tables from capture mode
Alternatively, you can use Db2 control commands to accomplish these tasks.
After the tables of interest are in capture mode, the connector reads their corresponding change-data tables to obtain change events for table updates. The connector emits a change event for each row-level insert, update, and delete operation to a Kafka topic that has the same name as the changed table. This is default behavior that you can modify. Client applications read the Kafka topics that correspond to the database tables of interest and can react to each row-level change event.
Typically, the database administrator puts a table into capture mode in the middle of the life of a table. This means that the connector does not have the complete history of all changes that have been made to the table. Therefore, when the Db2 connector first connects to a particular Db2 database, it starts by performing a consistent snapshot of each table that is in capture mode. After the connector completes the snapshot, the connector streams change events from the point at which the snapshot was made. In this way, the connector starts with a consistent view of the tables that are in capture mode, and does not drop any changes that were made while it was performing the snapshot.
Debezium connectors are tolerant of failures. As the connector reads and produces change events, it records the log sequence number (LSN) of the change-data table entry. The LSN is the position of the change event in the database log. If the connector stops for any reason, including communication failures, network problems, or crashes, upon restarting it continues reading the change-data tables where it left off. This includes snapshots. That is, if the snapshot was not complete when the connector stopped, upon restart the connector begins a new snapshot.
How the connector works
To optimally configure and run a Debezium Db2 connector, it is helpful to understand how the connector performs snapshots, streams change events, determines Kafka topic names, and handles schema changes.
Snapshots
Db2`s replication feature is not designed to store the complete history of database changes. Consequently, when a Debezium Db2 connector connects to a database for the first time, it takes a consistent snapshot of tables that are in capture mode and streams this state to Kafka. This establishes the baseline for table content.
By default, when a Db2 connector performs a snapshot, it does the following:
- Determines which tables are in capture mode, and thus must be included in the snapshot. By default, all non-system tables are in capture mode. Connector configuration properties, such as
table.exclude.listandtable.include.listlet you specify which tables should be in capture mode. - Obtains a lock on each of the tables in capture mode. This ensures that no schema changes can occur in those tables during the snapshot. The level of the lock is determined by the
snapshot.isolation.modeconnector configuration property. - Reads the highest (most recent) LSN position in the server’s transaction log.
- Captures the schema of all tables that are in capture mode. The connector persists this information in its internal database history topic.
- Optional, releases the locks obtained in step 2. Typically, these locks are held for only a short time.
-
At the LSN position read in step 3, the connector scans the capture mode tables as well as their schemas. During the scan, the connector:
-
Confirms that the table was created before the start of the snapshot. If it was not, the snapshot skips that table. After the snapshot is complete, and the connector starts emitting change events, the connector produces change events for any tables that were created during the snapshot.
- Produces a read event for each row in each table that is in capture mode. All read events contain the same LSN position, which is the LSN position that was obtained in step 3.
- Emits each read event to the Kafka topic that has the same name as the table.
- Records the successful completion of the snapshot in the connector offsets.
Ad hoc snapshots
By default, a connector runs an initial snapshot operation only after it starts for the first time. Following this initial snapshot, under normal circumstances, the connector does not repeat the snapshot process. Any future change event data that the connector captures comes in through the streaming process only.
However, in some situations the data that the connector obtained during the initial snapshot might become stale, lost, or incomplete. To provide a mechanism for recapturing table data, Debezium includes an option to perform ad hoc snapshots. The following changes in a database might be cause for performing an ad hoc snapshot:
- The connector configuration is modified to capture a different set of tables.
- Kafka topics are deleted and must be rebuilt.
- Data corruption occurs due to a configuration error or some other problem.
You can re-run a snapshot for a table for which you previously captured a snapshot by initiating a so-called ad-hoc snapshot. Ad hoc snapshots require the use of signaling tables. You initiate an ad hoc snapshot by sending a signal request to the Debezium signaling table.
When you initiate an ad hoc snapshot of an existing table, the connector appends content to the topic that already exists for the table. If a previously existing topic was removed, Debezium can create a topic automatically if automatic topic creation is enabled.
Ad hoc snapshot signals specify the tables to include in the snapshot. The snapshot can capture the entire contents of the database, or capture only a subset of the tables in the database.
You specify the tables to capture by sending an execute-snapshot message to the signaling table. Set the type of the execute-snapshot signal to incremental, and provide the names of the tables to include in the snapshot, as described in the following table:
Table 1. Example of an ad hoc execute-snapshot signal record
| Field | Default | Value |
| --- | --- | --- |
| type | incremental | Specifies the type of snapshot that you want to run. Setting the type is optional. Currently, you can request only incremental snapshots. |
| data-collections | N/A | An array that contains the fully-qualified names of the table to be snapshotted. The format of the names is the same as for the signal.data.collection configuration option. |
Triggering an ad hoc snapshot
You initiate an ad hoc snapshot by adding an entry with the execute-snapshot signal type to the signaling table. After the connector processes the message, it begins the snapshot operation. The snapshot process reads the first and last primary key values and uses those values as the start and end point for each table. Based on the number of entries in the table, and the configured chunk size, Debezium divides the table into chunks, and proceeds to snapshot each chunk, in succession, one at a time.
Currently, the execute-snapshot action type triggers incremental snapshots only. For more information, see Incremental snapshots.
Incremental snapshots
To provide flexibility in managing snapshots, Debezium includes a supplementary snapshot mechanism, known as incremental snapshotting. Incremental snapshots rely on the Debezium mechanism for sending signals to a Debezium connector. Incremental snapshots are based on the DDD-3 design document.
In an incremental snapshot, instead of capturing the full state of a database all at once, as in an initial snapshot, Debezium captures each table in phases, in a series of configurable chunks. You can specify the tables that you want the snapshot to capture and the size of each chunk. The chunk size determines the number of rows that the snapshot collects during each fetch operation on the database. The default chunk size for incremental snapshots is 1 KB.
As an incremental snapshot proceeds, Debezium uses watermarks to track its progress, maintaining a record of each table row that it captures. This phased approach to capturing data provides the following advantages over the standard initial snapshot process:
- You can run incremental snapshots in parallel with streamed data capture, instead of postponing streaming until the snapshot completes. The connector continues to capture near real-time events from the change log throughout the snapshot process, and neither operation blocks the other.
- If the progress of an incremental snapshot is interrupted, you can resume it without losing any data. After the process resumes, the snapshot begins at the point where it stopped, rather than recapturing the table from the beginning.
- You can run an incremental snapshot on demand at any time, and repeat the process as needed to adapt to database updates. For example, you might re-run a snapshot after you modify the connector configuration to add a table to its table.include.list property.
Incremental snapshot process
When you run an incremental snapshot, Debezium sorts each table by primary key and then splits the table into chunks based on the configured chunk size. Working chunk by chunk, it then captures each table row in a chunk. For each row that it captures, the snapshot emits a READ event. That event represents the value of the row when the snapshot for the chunk began.
As a snapshot proceeds, it’s likely that other processes continue to access the database, potentially modifying table records. To reflect such changes, INSERT, UPDATE, or DELETE operations are committed to the transaction log as per usual. Similarly, the ongoing Debezium streaming process continues to detect these change events and emits corresponding change event records to Kafka.
How Debezium resolves collisions among records with the same primary key
In some cases, the UPDATE or DELETE events that the streaming process emits are received out of sequence. That is, the streaming process might emit an event that modifies a table row before the snapshot captures the chunk that contains the READ event for that row. When the snapshot eventually emits the corresponding READ event for the row, its value is already superseded. To ensure that incremental snapshot events that arrive out of sequence are processed in the correct logical order, Debezium employs a buffering scheme for resolving collisions. Only after collisions between the snapshot events and the streamed events are resolved does Debezium emit an event record to Kafka.
Snapshot window
To assist in resolving collisions between late-arriving READ events and streamed events that modify the same table row, Debezium employs a so-called snapshot window. The snapshot windows demarcates the interval during which an incremental snapshot captures data for a specified table chunk. Before the snapshot window for a chunk opens, Debezium follows its usual behavior and emits events from the transaction log directly downstream to the target Kafka topic. But from the moment that the snapshot for a particular chunk opens, until it closes, Debezium performs a de-duplication step to resolve collisions between events that have the same primary key..
For each data collection, the Debezium emits two types of events, and stores the records for them both in a single destination Kafka topic. The snapshot records that it captures directly from a table are emitted as READ operations. Meanwhile, as users continue to update records in the data collection, and the transaction log is updated to reflect each commit, Debezium emits UPDATE or DELETE operations for each change.
As the snapshot window opens, and Debezium begins processing a snapshot chunk, it delivers snapshot records to a memory buffer. During the snapshot windows, the primary keys of the READ events in the buffer are compared to the primary keys of the incoming streamed events. If no match is found, the streamed event record is sent directly to Kafka. If Debezium detects a match, it discards the buffered READ event, and writes the streamed record to the destination topic, because the streamed event logically supersede the static snapshot event. After the snapshot window for the chunk closes, the buffer contains only READ events for which no related transaction log events exist. Debezium emits these remaining READ events to the table’s Kafka topic.
The connector repeats the process for each snapshot chunk.
Triggering an incremental snapshot
Currently, the only way to initiate an incremental snapshot is to send an ad hoc snapshot signal to the signaling table on the source database. You submit signals to the table as SQL INSERT queries. After Debezium detects the change in the signaling table, it reads the signal, and runs the requested snapshot operation.
The query that you submit specifies the tables to include in the snapshot, and, optionally, specifies the kind of snapshot operation. Currently, the only valid option for snapshots operations is the default value, incremental.
To specify the tables to include in the snapshot, provide a data-collections array that lists the tables, for example,
{"data-collections": ["public.MyFirstTable", "public.MySecondTable"]}
The data-collections array for an incremental snapshot signal has no default value. If the data-collections array is empty, Debezium detects that no action is required and does not perform a snapshot.
If the name of a table that you want to include in a snapshot contains a dot (.) in the name of the database, schema, or table, to add the table to the data-collections array, you must escape each part of the name in double quotes. For example, to include a table that exists in the public schema and that has the name My.Table, use the following format: “public”.”My.Table”. |
Prerequisites
-
A signaling data collection exists on the source database and the connector is configured to capture it.
- The signaling data collection is specified in the signal.data.collection property.
Procedure
- Send a SQL query to add the ad hoc incremental snapshot request to the signaling table:
1. INSERT INTO _<signalTable>_ (id, type, data) VALUES (_'<id>'_, _'<snapshotType>'_, '{"data-collections": ["_<tableName>_","_<tableName>_"],"type":"_<snapshotType>_"}');
For example,
1. INSERT INTO myschema.debezium_signal (id, type, data) VALUES('ad-hoc-1', 'execute-snapshot', '{"data-collections": ["schema1.table1", "schema2.table2"],"type":"incremental"}');
The values of the id,type, and data parameters in the command correspond to the fields of the signaling table.
The following table describes the these parameters:
Table 2. Descriptions of fields in a SQL command for sending an incremental snapshot signal to the signaling table
| Value | Description |
| --- | --- |
| myschema.debezium_signal | Specifies the fully-qualified name of the signaling table on the source database |
| ad-hoc-1 | The id parameter specifies an arbitrary string that is assigned as the id identifier for the signal request. Use this string to identify logging messages to entries in the signaling table. Debezium does not use this string. Rather, during the snapshot, Debezium generates its own id string as a watermarking signal. |
| execute-snapshot | Specifies type parameter specifies the operation that the signal is intended to trigger. |
| data-collections | A required component of the data field of a signal that specifies an array of table names to include in the snapshot. The array lists tables by their fully-qualified names, using the same format as you use to specify the name of the connector’s signaling table in the signal.data.collection configuration property. |
| incremental | An optional type component of the data field of a signal that specifies the kind of snapshot operation to run. Currently, the only valid option is the default value, incremental. Specifying a type value in the SQL query that you submit to the signaling table is optional. If you do not specify a value, the connector runs an incremental snapshot. |
The following example, shows the JSON for an incremental snapshot event that is captured by a connector.
Example: Incremental snapshot event message
1. {
2. "before":null,
3. "after": {
4. "pk":"1",
5. "value":"New data"
6. },
7. "source": {
8. ...
9. "snapshot":"incremental" (1)
10. },
11. "op":"r", (2)
12. "ts_ms":"1620393591654",
13. "transaction":null
14. }
| Item | Field name | Description |
|---|---|---|
| 1 | snapshot |
Specifies the type of snapshot operation to run. Currently, the only valid option is the default value, incremental. Specifying a type value in the SQL query that you submit to the signaling table is optional. If you do not specify a value, the connector runs an incremental snapshot. |
| 2 | op |
Specifies the event type. The value for snapshot events is r, signifying a READ operation. |
| The Debezium connector for Db2 does not support schema changes while an incremental snapshot is running. |
Change-data tables
After a complete snapshot, when a Debezium Db2 connector starts for the first time, the connector identifies the change-data table for each source table that is in capture mode. The connector does the following for each change-data table:
- Reads change events that were created between the last stored, highest LSN and the current, highest LSN.
- Orders the change events according to the commit LSN and the change LSN for each event. This ensures that the connector emits the change events in the order in which the table changes occurred.
- Passes commit and change LSNs as offsets to Kafka Connect.
- Stores the highest LSN that the connector passed to Kafka Connect.
After a restart, the connector resumes emitting change events from the offset (commit and change LSNs) where it left off. While the connector is running and emitting change events, if you remove a table from capture mode or add a table to capture mode, the connector detects the change, and modifies its behavior accordingly.
Topic names
By default, the Db2 connector writes change events for all of the INSERT, UPDATE, and DELETE operations that occur in a table to a single Apache Kafka topic that is specific to that table. The connector uses the following convention to name change event topics:
databaseName.schemaName.tableName
The following list provides definitions for the components of the default name:
databaseName
The logical name of the connector as specified by the database.server.name connector configuration property.
schemaName
The name of the schema in which the operation occurred.
tableName
The name of the table in which the operation occurred.
For example, consider a Db2 installation with the mydatabase database, which contains four tables: PRODUCTS, PRODUCTS_ON_HAND, CUSTOMERS, and ORDERS that are in the MYSCHEMA schema. The connector would emit events to these four Kafka topics:
mydatabase.MYSCHEMA.PRODUCTSmydatabase.MYSCHEMA.PRODUCTS_ON_HANDmydatabase.MYSCHEMA.CUSTOMERSmydatabase.MYSCHEMA.ORDERS
The connector applies similar naming conventions to label its internal database history topics, schema change topics, and transaction metadata topics.
If the default topic name do not meet your requirements, you can configure custom topic names. To configure custom topic names, you specify regular expressions in the logical topic routing SMT. For more information about using the logical topic routing SMT to customize topic naming, see Topic routing.
Schema change topic
You can configure a Debezium Db2 connector to produce schema change events that describe schema changes that are applied to captured tables in the database.
Debezium emits a message to the schema change topic when:
- A new table goes into capture mode.
- A table is removed from capture mode.
- During a database schema update, there is a change in the schema for a table that is in capture mode.
The connector writes schema change events to a Kafka schema change topic that has the name *<serverName>* where *<serverName>* is the logical server name that is specified in the database.server.name connector configuration property. Messages that the connector sends to the schema change topic contain a payload that includes the following elements:
databaseName
The name of the database to which the statements are applied. The value of databaseName serves as the message key.
pos
The position in the binlog where the statements appear.
tableChanges
A structured representation of the entire table schema after the schema change. The tableChanges field contains an array that includes entries for each column of the table. Because the structured representation presents data in JSON or Avro format, consumers can easily read messages without first processing them through a DDL parser.
| For a table that is in capture mode, the connector not only stores the history of schema changes in the schema change topic, but also in an internal database history topic. The internal database history topic is for connector use only and it is not intended for direct use by consuming applications. Ensure that applications that require notifications about schema changes consume that information only from the schema change topic. |
Never partition the database history topic. For the database history topic to function correctly, it must maintain a consistent, global order of the event records that the connector emits to it. To ensure that the topic is not split among partitions, set the partition count for the topic by using one of the following methods: - If you create the database history topic manually, specify a partition count of 1. - If you use the Apache Kafka broker to create the database history topic automatically, the topic is created, set the value of the Kafka num.partitions configuration option to 1. |
| The format of messages that a connector emits to its schema change topic is in an incubating state and can change without notice. |
Example: Message emitted to the Db2 connector schema change topic
The following example shows a message in the schema change topic. The message contains a logical representation of the table schema.
1. {
2. "schema": {
3. ...
4. },
5. "payload": {
6. "source": {
7. "version": "1.9.6.Final",
8. "connector": "db2",
9. "name": "db2",
10. "ts_ms": 1588252618953,
11. "snapshot": "true",
12. "db": "testdb",
13. "schema": "DB2INST1",
14. "table": "CUSTOMERS",
15. "change_lsn": null,
16. "commit_lsn": "00000025:00000d98:00a2",
17. "event_serial_no": null
18. },
19. "databaseName": "TESTDB", (1)
20. "schemaName": "DB2INST1",
21. "ddl": null, (2)
22. "tableChanges": [ (3)
23. {
24. "type": "CREATE", (4)
25. "id": "\"DB2INST1\".\"CUSTOMERS\"", (5)
26. "table": { (6)
27. "defaultCharsetName": null,
28. "primaryKeyColumnNames": [ (7)
29. "ID"
30. ],
31. "columns": [ (8)
32. {
33. "name": "ID",
34. "jdbcType": 4,
35. "nativeType": null,
36. "typeName": "int identity",
37. "typeExpression": "int identity",
38. "charsetName": null,
39. "length": 10,
40. "scale": 0,
41. "position": 1,
42. "optional": false,
43. "autoIncremented": false,
44. "generated": false
45. },
46. {
47. "name": "FIRST_NAME",
48. "jdbcType": 12,
49. "nativeType": null,
50. "typeName": "varchar",
51. "typeExpression": "varchar",
52. "charsetName": null,
53. "length": 255,
54. "scale": null,
55. "position": 2,
56. "optional": false,
57. "autoIncremented": false,
58. "generated": false
59. },
60. {
61. "name": "LAST_NAME",
62. "jdbcType": 12,
63. "nativeType": null,
64. "typeName": "varchar",
65. "typeExpression": "varchar",
66. "charsetName": null,
67. "length": 255,
68. "scale": null,
69. "position": 3,
70. "optional": false,
71. "autoIncremented": false,
72. "generated": false
73. },
74. {
75. "name": "EMAIL",
76. "jdbcType": 12,
77. "nativeType": null,
78. "typeName": "varchar",
79. "typeExpression": "varchar",
80. "charsetName": null,
81. "length": 255,
82. "scale": null,
83. "position": 4,
84. "optional": false,
85. "autoIncremented": false,
86. "generated": false
87. }
88. ]
89. }
90. }
91. ]
92. }
93. }
Table 3. Descriptions of fields in messages emitted to the schema change topic
| Item | Field name | Description |
| --- | --- | --- |
| 1 | databaseName schemaName | Identifies the database and the schema that contain the change. |
| 2 | ddl | Always null for the Db2 connector. For other connectors, this field contains the DDL responsible for the schema change. This DDL is not available to Db2 connectors. |
| 3 | tableChanges | An array of one or more items that contain the schema changes generated by a DDL command. |
| 4 | type | Describes the kind of change. The value is one of the following: - CREATE - table created - ALTER - table modified - DROP - table deleted |
| 5 | id | Full identifier of the table that was created, altered, or dropped. |
| 6 | table | Represents table metadata after the applied change. |
| 7 | primaryKeyColumnNames | List of columns that compose the table’s primary key. |
| 8 | columns | Metadata for each column in the changed table. |
In messages that the connector sends to the schema change topic, the message key is the name of the database that contains the schema change. In the following example, the payload field contains the key:
1. {
2. "schema": {
3. "type": "struct",
4. "fields": [
5. {
6. "type": "string",
7. "optional": false,
8. "field": "databaseName"
9. }
10. ],
11. "optional": false,
12. "name": "io.debezium.connector.db2.SchemaChangeKey"
13. },
14. "payload": {
15. "databaseName": "TESTDB"
16. }
17. }
Transaction metadata
Debezium can generate events that represent transaction boundaries and that enrich change data event messages.
| Limits on when Debezium receives transaction metadata Debezium registers and receives metadata only for transactions that occur after you deploy the connector. Metadata for transactions that occur before you deploy the connector is not available. |
Debezium generates transaction boundary events for the BEGIN and END delimiters in every transaction. Transaction boundary events contain the following fields:
status
BEGIN or END.
id
String representation of the unique transaction identifier.
event_count (for END events)
Total number of events emitted by the transaction.
data_collections (for END events)
An array of pairs of data_collection and event_count elements. that indicates the number of events that the connector emits for changes that originate from a data collection.
Example
1. {
2. "status": "BEGIN",
3. "id": "00000025:00000d08:0025",
4. "event_count": null,
5. "data_collections": null
6. }
8. {
9. "status": "END",
10. "id": "00000025:00000d08:0025",
11. "event_count": 2,
12. "data_collections": [
13. {
14. "data_collection": "testDB.dbo.tablea",
15. "event_count": 1
16. },
17. {
18. "data_collection": "testDB.dbo.tableb",
19. "event_count": 1
20. }
21. ]
22. }
Unless overridden via the transaction.topic option, the connector emits transaction events to the .transaction topic.
Data change event enrichment
When transaction metadata is enabled the connector enriches the change event Envelope with a new transaction field. This field provides information about every event in the form of a composite of fields:
id
String representation of unique transaction identifier.
total_order
The absolute position of the event among all events generated by the transaction.
data_collection_order
The per-data collection position of the event among all events that were emitted by the transaction.
Following is an example of a message:
1. {
2. "before": null,
3. "after": {
4. "pk": "2",
5. "aa": "1"
6. },
7. "source": {
8. ...
9. },
10. "op": "c",
11. "ts_ms": "1580390884335",
12. "transaction": {
13. "id": "00000025:00000d08:0025",
14. "total_order": "1",
15. "data_collection_order": "1"
16. }
17. }
Data change events
The Debezium Db2 connector generates a data change event for each row-level INSERT, UPDATE, and DELETE operation. Each event contains a key and a value. The structure of the key and the value depends on the table that was changed.
Debezium and Kafka Connect are designed around continuous streams of event messages. However, the structure of these events may change over time, which can be difficult for consumers to handle. To address this, each event contains the schema for its content or, if you are using a schema registry, a schema ID that a consumer can use to obtain the schema from the registry. This makes each event self-contained.
The following skeleton JSON shows the basic four parts of a change event. However, how you configure the Kafka Connect converter that you choose to use in your application determines the representation of these four parts in change events. A schema field is in a change event only when you configure the converter to produce it. Likewise, the event key and event payload are in a change event only if you configure a converter to produce it. If you use the JSON converter and you configure it to produce all four basic change event parts, change events have this structure:
1. {
2. "schema": { (1)
3. ...
4. },
5. "payload": { (2)
6. ...
7. },
8. "schema": { (3)
9. ...
10. },
11. "payload": { (4)
12. ...
13. },
14. }
Table 4. Overview of change event basic content
| Item | Field name | Description |
| --- | --- | --- |
| 1 | schema | The first schema field is part of the event key. It specifies a Kafka Connect schema that describes what is in the event key’s payload portion. In other words, the first schema field describes the structure of the primary key, or the unique key if the table does not have a primary key, for the table that was changed. It is possible to override the table’s primary key by setting the message.key.columns connector configuration property. In this case, the first schema field describes the structure of the the key identified by that property. |
| 2 | payload | The first payload field is part of the event key. It has the structure described by the previous schema field and it contains the key for the row that was changed. |
| 3 | schema | The second schema field is part of the event value. It specifies the Kafka Connect schema that describes what is in the event value’s payload portion. In other words, the second schema describes the structure of the row that was changed. Typically, this schema contains nested schemas. |
| 4 | payload | The second payload field is part of the event value. It has the structure described by the previous schema field and it contains the actual data for the row that was changed. |
By default, the connector streams change event records to topics with names that are the same as the event’s originating table. See topic names.
| The Debezium Db2 connector ensures that all Kafka Connect schema names adhere to the Avro schema name format. This means that the logical server name must start with a Latin letter or an underscore, that is, a-z, A-Z, or *. Each remaining character in the logical server name and each character in the database and table names must be a Latin letter, a digit, or an underscore, that is, a-z, A-Z, 0-9, or *. If there is an invalid character it is replaced with an underscore character. This can lead to unexpected conflicts if the logical server name, a database name, or a table name contains invalid characters, and the only characters that distinguish names from one another are invalid and thus replaced with underscores. Also, Db2 names for databases, schemas, and tables can be case sensitive. This means that the connector could emit event records for more than one table to the same Kafka topic. |
Change event keys
A change event’s key contains the schema for the changed table’s key and the changed row’s actual key. Both the schema and its corresponding payload contain a field for each column in the changed table’s PRIMARY KEY (or unique constraint) at the time the connector created the event.
Consider the following customers table, which is followed by an example of a change event key for this table.
Example table
1. CREATE TABLE customers (
2. ID INTEGER IDENTITY(1001,1) NOT NULL PRIMARY KEY,
3. FIRST_NAME VARCHAR(255) NOT NULL,
4. LAST_NAME VARCHAR(255) NOT NULL,
5. EMAIL VARCHAR(255) NOT NULL UNIQUE
6. );
Example change event key
Every change event that captures a change to the customers table has the same event key schema. For as long as the customers table has the previous definition, every change event that captures a change to the customers table has the following key structure. In JSON, it looks like this:
1. {
2. "schema": { (1)
3. "type": "struct",
4. "fields": [ (2)
5. {
6. "type": "int32",
7. "optional": false,
8. "field": "ID"
9. }
10. ],
11. "optional": false, (3)
12. "name": "mydatabase.MYSCHEMA.CUSTOMERS.Key" (4)
13. },
14. "payload": { (5)
15. "ID": 1004
16. }
17. }
Table 5. Description of change event key
| Item | Field name | Description |
| --- | --- | --- |
| 1 | schema | The schema portion of the key specifies a Kafka Connect schema that describes what is in the key’s payload portion. |
| 2 | fields | Specifies each field that is expected in the payload, including each field’s name, type, and whether it is required. |
| 3 | optional | Indicates whether the event key must contain a value in its payload field. In this example, a value in the key’s payload is required. A value in the key’s payload field is optional when a table does not have a primary key. |
| 4 | mydatabase.MYSCHEMA.CUSTOMERS.Key | Name of the schema that defines the structure of the key’s payload. This schema describes the structure of the primary key for the table that was changed. Key schema names have the format connector-name.database-name.table-name.Key. In this example: - mydatabase is the name of the connector that generated this event. - MYSCHEMA is the database schema that contains the table that was changed. - CUSTOMERS is the table that was updated. |
| 5 | payload | Contains the key for the row for which this change event was generated. In this example, the key, contains a single ID field whose value is 1004. |
Change event values
The value in a change event is a bit more complicated than the key. Like the key, the value has a schema section and a payload section. The schema section contains the schema that describes the Envelope structure of the payload section, including its nested fields. Change events for operations that create, update or delete data all have a value payload with an envelope structure.
Consider the same sample table that was used to show an example of a change event key:
Example table
1. CREATE TABLE customers (
2. ID INTEGER IDENTITY(1001,1) NOT NULL PRIMARY KEY,
3. FIRST_NAME VARCHAR(255) NOT NULL,
4. LAST_NAME VARCHAR(255) NOT NULL,
5. EMAIL VARCHAR(255) NOT NULL UNIQUE
6. );
The event value portion of every change event for the customers table specifies the same schema. The event value’s payload varies according to the event type:
create events
The following example shows the value portion of a change event that the connector generates for an operation that creates data in the customers table:
1. {
2. "schema": { (1)
3. "type": "struct",
4. "fields": [
5. {
6. "type": "struct",
7. "fields": [
8. {
9. "type": "int32",
10. "optional": false,
11. "field": "ID"
12. },
13. {
14. "type": "string",
15. "optional": false,
16. "field": "FIRST_NAME"
17. },
18. {
19. "type": "string",
20. "optional": false,
21. "field": "LAST_NAME"
22. },
23. {
24. "type": "string",
25. "optional": false,
26. "field": "EMAIL"
27. }
28. ],
29. "optional": true,
30. "name": "mydatabase.MYSCHEMA.CUSTOMERS.Value", (2)
31. "field": "before"
32. },
33. {
34. "type": "struct",
35. "fields": [
36. {
37. "type": "int32",
38. "optional": false,
39. "field": "ID"
40. },
41. {
42. "type": "string",
43. "optional": false,
44. "field": "FIRST_NAME"
45. },
46. {
47. "type": "string",
48. "optional": false,
49. "field": "LAST_NAME"
50. },
51. {
52. "type": "string",
53. "optional": false,
54. "field": "EMAIL"
55. }
56. ],
57. "optional": true,
58. "name": "mydatabase.MYSCHEMA.CUSTOMERS.Value",
59. "field": "after"
60. },
61. {
62. "type": "struct",
63. "fields": [
64. {
65. "type": "string",
66. "optional": false,
67. "field": "version"
68. },
69. {
70. "type": "string",
71. "optional": false,
72. "field": "connector"
73. },
74. {
75. "type": "string",
76. "optional": false,
77. "field": "name"
78. },
79. {
80. "type": "int64",
81. "optional": false,
82. "field": "ts_ms"
83. },
84. {
85. "type": "boolean",
86. "optional": true,
87. "default": false,
88. "field": "snapshot"
89. },
90. {
91. "type": "string",
92. "optional": false,
93. "field": "db"
94. },
95. {
96. "type": "string",
97. "optional": false,
98. "field": "schema"
99. },
100. {
101. "type": "string",
102. "optional": false,
103. "field": "table"
104. },
105. {
106. "type": "string",
107. "optional": true,
108. "field": "change_lsn"
109. },
110. {
111. "type": "string",
112. "optional": true,
113. "field": "commit_lsn"
114. },
115. ],
116. "optional": false,
117. "name": "io.debezium.connector.db2.Source", (3)
118. "field": "source"
119. },
120. {
121. "type": "string",
122. "optional": false,
123. "field": "op"
124. },
125. {
126. "type": "int64",
127. "optional": true,
128. "field": "ts_ms"
129. }
130. ],
131. "optional": false,
132. "name": "mydatabase.MYSCHEMA.CUSTOMERS.Envelope" (4)
133. },
134. "payload": { (5)
135. "before": null, (6)
136. "after": { (7)
137. "ID": 1005,
138. "FIRST_NAME": "john",
139. "LAST_NAME": "doe",
140. "EMAIL": "john.doe@example.org"
141. },
142. "source": { (8)
143. "version": "1.9.6.Final",
144. "connector": "db2",
145. "name": "myconnector",
146. "ts_ms": 1559729468470,
147. "snapshot": false,
148. "db": "mydatabase",
149. "schema": "MYSCHEMA",
150. "table": "CUSTOMERS",
151. "change_lsn": "00000027:00000758:0003",
152. "commit_lsn": "00000027:00000758:0005",
153. },
154. "op": "c", (9)
155. "ts_ms": 1559729471739 (10)
156. }
157. }
Table 6. Descriptions of create event value fields
| Item | Field name | Description |
| --- | --- | --- |
| 1 | schema | The value’s schema, which describes the structure of the value’s payload. A change event’s value schema is the same in every change event that the connector generates for a particular table. |
| 2 | name | In the schema section, each name field specifies the schema for a field in the value’s payload. mydatabase.MYSCHEMA.CUSTOMERS.Value is the schema for the payload’s before and after fields. This schema is specific to the customers table. The connector uses this schema for all rows in the MYSCHEMA.CUSTOMERS table. Names of schemas for before and after fields are of the form logicalName.schemaName.tableName.Value, which ensures that the schema name is unique in the database. This means that when using the Avro converter, the resulting Avro schema for each table in each logical source has its own evolution and history. |
| 3 | name | io.debezium.connector.db2.Source is the schema for the payload’s source field. This schema is specific to the Db2 connector. The connector uses it for all events that it generates. |
| 4 | name | mydatabase.MYSCHEMA.CUSTOMERS.Envelope is the schema for the overall structure of the payload, where mydatabase is the database, MYSCHEMA is the schema, and CUSTOMERS is the table. |
| 5 | payload | The value’s actual data. This is the information that the change event is providing. It may appear that JSON representations of events are much larger than the rows they describe. This is because a JSON representation must include the schema portion and the payload portion of the message. However, by using the Avro converter, you can significantly decrease the size of the messages that the connector streams to Kafka topics. |
| 6 | before | An optional field that specifies the state of the row before the event occurred. When the op field is c for create, as it is in this example, the before field is null since this change event is for new content. |
| 7 | after | An optional field that specifies the state of the row after the event occurred. In this example, the after field contains the values of the new row’s ID, FIRST_NAME, LAST_NAME, and EMAIL columns. |
| 8 | source | Mandatory field that describes the source metadata for the event. The source structure shows Db2 information about this change, which provides traceability. It also has information you can use to compare to other events in the same topic or in other topics to know whether this event occurred before, after, or as part of the same commit as other events. The source metadata includes: - Debezium version - Connector type and name - Timestamp for when the change was made in the database - Whether the event is part of an ongoing snapshot - Name of the database, schema, and table that contain the new row - Change LSN - Commit LSN (omitted if this event is part of a snapshot) |
| 9 | op | Mandatory string that describes the type of operation that caused the connector to generate the event. In this example, c indicates that the operation created a row. Valid values are: - c = create - u = update - d = delete - r = read (applies to only snapshots) |
| 10 | ts_ms | Optional field that displays the time at which the connector processed the event. The time is based on the system clock in the JVM running the Kafka Connect task. In the source object, ts_ms in
(本文超出站点单文档体积上限,已截断)
