You can write records in JSON format by using the KcopJsonFormatIntegrated Kafka custom
operation processor.
About this task
You can use this KCOP to replicate source operations to JSON-encoded key and value fields in an
Avro Kafka producer record. This KCOP does not connect to a schema registry. The topic names are
appended with "-json". By using the JsonEncoder library for serialization, a schema registry is not
required so your replication target can be any installation of Apache Kafka without the need to use
consumers with special deserializers or a schema registry. For full details, see API reference in Javadoc format.
-
In Management Console, click .
-
Select the subscription.
-
Right-click the subscription and select Kafka Properties.
-
Verify that Zookeeper is selected as the method for Kafka apply.
-
Click OK.
-
Right-click the subscription and select User Exit.
-
Enter values for the following fields:
- Class Name
- com.datamirror.ts.target.publication.userexit.sample.kafka.KcopJsonFormatIntegrated
- Parameters
-
[-file:full_path_to_filename]
You might want to use the convention
CDC_Kafka_installation_directory/conf/filename.properties.
-
Click OK.
Sample output
$ db2 "insert into tab1 values (8,8,8, 'Tab1 data')"
DB20000I The SQL command completed successfully.
$ db2 "update tab1 set I1 = 9, I2 = 9 where I1 = 8"
DB20000I The SQL command completed successfully.
$ db2 "delete from tab1 where I1 = 9"
DB20000I The SQL command completed successfully.
Use the Kafka provided consumer to read the records
kafka-console-consumer --zookeeper hostname:port --property print.key=true --topic topic name-json --from-beginning
{"I2":8,"I3":8} {"I1":8,"I2":8,"I3":8,"V1":{"string":"Tab1 data"}}
{"I2":8,"I3":8} null
{"I2":9,"I3":8} {"I1":9,"I2":9,"I3":8,"V1":{"string":"Tab1 data"}}
{"I2":9,"I3":8} null