<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amanlonare on Medium]]></title>
        <description><![CDATA[Stories by Amanlonare on Medium]]></description>
        <link>https://medium.com/@amanlonare95?source=rss-18c84c713783------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*noUxjbe6Jz5cMYRU</url>
            <title>Stories by Amanlonare on Medium</title>
            <link>https://medium.com/@amanlonare95?source=rss-18c84c713783------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 03:44:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amanlonare95/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Near Real Time Data Replication from MySQL to PostgreSQL — II]]></title>
            <link>https://medium.com/@amanlonare95/near-real-time-data-replication-from-mysql-to-postgresql-ii-b34cf6b0bab4?source=rss-18c84c713783------2</link>
            <guid isPermaLink="false">https://medium.com/p/b34cf6b0bab4</guid>
            <category><![CDATA[debezium]]></category>
            <category><![CDATA[change-data-capture]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[architecture]]></category>
            <dc:creator><![CDATA[Amanlonare]]></dc:creator>
            <pubDate>Sat, 16 Jul 2022 10:32:48 GMT</pubDate>
            <atom:updated>2022-07-16T10:32:48.910Z</atom:updated>
            <content:encoded><![CDATA[<h3>Near Real Time Data Replication from MySQL to PostgreSQL — II</h3><h4>Building a data pipeline with Apache Kafka, Zookeeper, Kafka Connect, MySQL, PostgreSQL and Debezium</h4><p>This the second part of the series on replicating the data from one relational database management system (RDBMS) to another. In the <a href="https://medium.com/@amanlonare95/near-real-time-data-replication-from-mysql-to-postgresql-i-2732bedbec8e#52e9-d4531187130">first part</a> of the series, we have examined the structure of the change data event streamed by the Debezium whenever there is any INSERT/UPDATE/DELETE operations on our MySQL source database. We have described what Debezium is, and delved into the complete environment setup for streaming the change events from MySQL to the Kafka.</p><p>In this blog I will show you how these change events can be used to replicate the changes from our source database, which is MySQL, to our sink database, which is PostgreSQL, with almost no code. This task is achieved completely in docker environment by building relevant docker containers. The complete code for the project can be found in the following <a href="https://github.com/AmanLonare/CDC-MySQL-Debezium-PostgreSQL">GitHub repository</a>. We are going to setup all the components as per the following architecture</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BDA21n6w7AQKSRxHfdXmXA.png" /><figcaption>Change data capture of MySQL database to PostgreSQL using Debezium</figcaption></figure><h3>Implementing the Architecture</h3><h4>Docker Compose to start all the services</h4><pre>$ <strong>git clone </strong><a href="https://github.com/AmanLonare/CDC-MySQL-Debezium-PostgreSQL.git"><strong>https://github.com/AmanLonare/CDC-MySQL-Debezium-PostgreSQL.git</strong></a></pre><pre>$ <strong>cd CDC-MySQL-Debezium-PostgreSQL/</strong></pre><pre>$ <strong>docker-compose -f docker-compose-mysql-dbz-postgres.yml up -d</strong></pre><p>This will start all the 5 components and you can see all five containers up and running using the command “docker ps”.</p><p>If we look at the configuration file of our PostgreSQL sink connector (postgres-sink-config.json) or MySQL source connector (mysql-source-config.json), we have few properties with the word “transforms” in it. Now what are these “transforms”? The change data event’s structure which we have seen in the <a href="https://medium.com/@amanlonare95/near-real-time-data-replication-from-mysql-to-postgresql-i-2732bedbec8e#52e9-d4531187130">first part</a> of the series is quite complex though it provides wealth of information. Kafka consumers in our cases expects simple format or flat structure of field names and their values, thus it is important that we change the structure of original events to the consumable ones. To accomplish this task, Debezium provides event flattening single message transformation (SMT) which can be configured in our Kafka Connect source or sink connector by adding the SMTs configuration details to our connector configuration. There are many <a href="https://debezium.io/documentation/reference/stable/transformations/index.html">SMTs provided by the Debezium</a> which can be configured to source or sink connector based on your particular use case. To know more about this topic you can go through the following <a href="https://debezium.io/documentation/reference/stable/transformations/event-flattening.html">documentation by Debezium</a> which has extensively explained the configurations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SUOu1hc0rvhuJT7JlosvIw.png" /><figcaption>Sink connector configuration with SMT configured</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/895/1*628mFnJTEliupgH1OEat1g.png" /><figcaption>Source connector configuration with SMT configured</figcaption></figure><p>If you have closely looked at the project structure, we have build the Kafka Connect by copying the PostgreSQL JDBC driver into the /kafka/libs directory and also Kafka Connect JDBC connector into the /kafka/connect/kafka-connect-jdbc directory. We can log into our enriched docker connect instance and confirm the following additions. We can also see different driver and connector already present in our Kafka Connect instance. The same process can be followed if you want to replicate the data between two DBMS whose driver and connector files are not already included in the Kafka Connect image provided by the Debezium.</p><h4>Configure the Connectors with the Kafka Connect</h4><pre>$ <strong>curl -i -X POST -H &quot;Accept:application/json&quot; -H &quot;Content-Type:application/json&quot; localhost:8083/connectors/ --data &quot;@mysql-source-config.json&quot;</strong></pre><p>This will register our MySQL source connector to our Kafka Connect and we should get a 201 response once our POST request is successful. Now we will register our PostgreSQL sink connector with the similar command</p><pre>$ <strong>curl -i -X POST -H &quot;Accept:application/json&quot; -H  &quot;Content-Type:application/json&quot; http://localhost:8083/connectors/ -d @postgres-sink-config.json</strong></pre><p>The following command will register our PostgreSQL sink connector to the Kafka Connect and we should get a similar 201 response. We can now check all the connectors configured by us by the following command which should show the output as shown below.</p><pre>$ <strong>curl -H &quot;Accept:application/json&quot; localhost:8083/connectors/</strong></pre><pre>[&quot;inventory-connector&quot;, &quot;jdbc-sink&quot;]</pre><h4>MySQL Source Database</h4><p>In the next step, we will check out our “inventory” database which already is present in our MySQL docker container. We can log into our container and get more information about our database. Note that in our MySQL source configuration file (mysql-source-config.json), we have explicitly mentioned the Kafka topic of interest, which in our case is “customers”. The “customers” topic in Kafka records all the change data events which are emitted whenever there are any INSERT/UPDATE/DELETE operations on customer table.</p><pre><strong>$ docker exec -it &lt;container_id_of_MySQL&gt; bash</strong></pre><pre>root@0fe701a833c8:/# <strong>mysql -u mysqluser -p</strong><br>Enter password: </pre><pre>Welcome to the MySQL monitor.  Commands end with ; or \g.<br>Your MySQL connection id is 8<br>Server version: 8.0.29 MySQL Community Server - GPL<br><br>Copyright (c) 2000, 2022, Oracle and/or its affiliates.<br><br>Oracle is a registered trademark of Oracle Corporation and/or its<br>affiliates. Other names may be trademarks of their respective<br>owners.<br><br>Type &#39;help;&#39; or &#39;\h&#39; for help. Type &#39;\c&#39; to clear the current input statement.<br><br>$ mysql&gt;<strong> SHOW databases;</strong><br>+--------------------+<br>| Database           |<br>+--------------------+<br>| information_schema |<br>| inventory          |<br>+--------------------+<br>2 rows in set (0.01 sec)<br><br>mysql&gt; <strong>use inventory;</strong><br>Reading table information for completion of table and column names<br>You can turn off this feature to get a quicker startup with -A<br><br>Database changed<br>mysql&gt; <strong>SHOW TABLES;</strong><br>+---------------------+<br>| Tables_in_inventory |<br>+---------------------+<br>| addresses           |<br>| customers           |<br>| geom                |<br>| orders              |<br>| products            |<br>| products_on_hand    |<br>+---------------------+<br>6 rows in set (0.00 sec)</pre><pre>mysql&gt; <strong>SELECT * FROM customers;</strong><br>+------+------------+-----------+-----------------------+<br>| id   | first_name | last_name | email                 |<br>+------+------------+-----------+-----------------------+<br>| 1001 | Sally      | Thomas    | sally.thomas@acme.com |<br>| 1002 | George     | Bailey    | gbailey@foobar.com    |<br>| 1003 | Edward     | Walker    | ed@walker.com         |<br>| 1004 | Anne       | Kretchmar | annek@noanswer.org    |<br>+------+------------+-----------+-----------------------+<br>4 rows in set (0.00 sec)</pre><p>The “customers” table in our “inventory” database looks something like the one we have shown above. Now remember that we had spin up an empty PostgreSQL docker container with no data inside. Now in our next step we will see if the following table is replicated or not.</p><h4>PostgreSQL Sink Database</h4><pre>$ <strong>docker exec -it 6c37ba47fdc3 bash</strong><br>root@6c37ba47fdc3:/# <strong>psql -U postgresuser inventory</strong><br>psql (9.6.24)<br>Type &quot;help&quot; for help.<br><br>inventory=# <strong>SELECT * FROM customers;</strong><br> last_name |  id  | first_name |         email<br>-----------+------+------------+-----------------------<br> Thomas    | 1001 | Sally      | sally.thomas@acme.com<br> Bailey    | 1002 | George     | gbailey@foobar.com<br> Walker    | 1003 | Edward     | ed@walker.com<br> Kretchmar | 1004 | Anne       | annek@noanswer.org<br>(4 rows)</pre><p>And Voila! As we can witness here, we have successfully replicated the data from our source database to our sink database with very little configuration and almost no coding. Isn’t it awesome? Now you can go on and try to change any data in our source MySQL database. <em>Eventually </em>you will see the changes in your sink database too. Now you can experiment this with any combination of DBMS which are supported by Debezium. You can also build your own custom connectors which you can use to stream the data from one DBMS to other.</p><h3>Conclusion</h3><p>In the <a href="https://medium.com/@amanlonare95/near-real-time-data-replication-from-mysql-to-postgresql-i-2732bedbec8e#52e9-d4531187130">first part</a> of the series, we have examined the basic structure of change data events and we can see these events in real time by continuously tailing the respective Kafka topics. In the second part we have seen how we can leverage these change data events to populate a sink database and in no time our sink database will look as similar to the source database with very little configuration changes and no coding. I hope this blog can help you in implementing any use case which require replication of data from one DBMS to other in near-real time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b34cf6b0bab4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Near Real Time Data Replication from MySQL to PostgreSQL — I]]></title>
            <link>https://medium.com/@amanlonare95/near-real-time-data-replication-from-mysql-to-postgresql-i-2732bedbec8e?source=rss-18c84c713783------2</link>
            <guid isPermaLink="false">https://medium.com/p/2732bedbec8e</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[change-data-capture]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[debezium]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Amanlonare]]></dc:creator>
            <pubDate>Mon, 11 Jul 2022 09:42:32 GMT</pubDate>
            <atom:updated>2022-07-16T10:35:26.266Z</atom:updated>
            <content:encoded><![CDATA[<h3>Near Real Time Data Replication from MySQL to PostgreSQL — I</h3><h4>Building a data pipeline with Apache Kafka, Zookeeper, Kafka Connect, MySQL, PostgreSQL and Debezium</h4><p>There are many intimidating ways of migrating data from one database management system (DBMS) to another. In this <strong>two-part blog</strong> series we will walkthrough a step by step guide to migrate the data from MySQL to PostgreSQL with just few configuration files. The environment is built completely on docker and following are the links of the project files for <a href="https://github.com/AmanLonare/Debezium-CDC-MySQL">first part</a> and <a href="https://github.com/AmanLonare/CDC-MySQL-Debezium-PostgreSQL">second part</a> of the blog. In the <a href="https://medium.com/p/2732bedbec8e/edit">first part</a> of the series, we will see how we can emit change data events from our source database and see it in real time. In the<a href="https://medium.com/@amanlonare95/near-real-time-data-replication-from-mysql-to-postgresql-ii-b34cf6b0bab4"> second part</a> of the series, we will see how we can use these changes events to replicate the data and any changes in our source database to a sink database n near-real time. Here are some key components I will be using to achieve our task at hand:</p><ul><li><a href="https://kafka.apache.org/"><strong>Apache Kafka</strong></a><strong>:</strong> An open-source distributed events streaming platform</li><li><a href="https://zookeeper.apache.org/"><strong>Apache Zookeeper</strong></a>: An open-source distributed server for managing coordination of cloud applications</li><li><a href="https://kafka.apache.org/documentation/#connect"><strong>Kafka Connect</strong></a><strong>:</strong> An open-source component for reliable streaming of data between Kafka and other system</li><li><a href="https://docs.confluent.io/platform/current/schema-registry/index.html"><strong>Schema Registry</strong></a><strong>: </strong>It helps the producer and consumer to retrieve schemas that describe the data models for the messages</li><li><a href="https://debezium.io/"><strong>Debezium</strong></a>: Debezium is the main component for change data capture and will be the main focus of this article.</li><li><a href="https://www.mysql.com/"><strong>MySQL</strong></a><strong>:</strong> An open-source relational database management system. In our demo, MySQL will be source database</li><li><a href="https://www.postgresql.org/"><strong>PostgreSQL</strong></a><strong>:</strong> An open-source relational database management system. In our demo, PostgreSQL will be sink database</li></ul><h3>What is Debezium?</h3><p>As per the official page of<em> </em><a href="https://debezium.io/"><em>Debezium</em></a>:</p><blockquote>Debezium is an open-source distributed platform for change data capture. Start it up, point it at your databases, and your apps can start responding to all of the inserts, updates, and deletes that other apps commit to your databases. Debezium is durable and fast, so your apps can respond quickly and never miss an event, even when things go wrong.</blockquote><p>The following architecture of a change data capture pipeline based on Debezium shows Debezium connector for MySQL and PostgreSQL, also called source connectors, establishing a connection to their respective databases. Debezium captures any row-level ‘INSERT’, ‘UPDATE’ or ‘DELETE’ operations on any tables of MySQL database as a change events and streams these events to the Kafka topics. Debezium is a log-based change data capture system which reads logs (binary log or binlog in case of MySQL, and logical replication streams in case of PostgreSQL) that records any changes in the schema of table or any ‘INSERT’, ‘UPDATE’ or ‘DELETE’ operations on the table in the order in which they are committed. The sink connector propagates the records from Kafka topics to other systems</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yF9AfZdE2woMDn2_ZsM0Ng.png" /><figcaption>(source: debezium.io)</figcaption></figure><p>Generally, we deploy Debezium by configuring it on top of Apache Kafka Connect which will act as a framework and runtime for implementing and operating the source and sink connectors. At the time of writing this blog, Debezium provides connector for <em>MongoDB, MySQL, PostgreSQL, SQL Server, Oracle, DB2, Cassandra (incubating), Vitess (incubating). </em>You can even write your own connector for different source data with the little bit of your Java coding skills and deploy them as jars.</p><p>Debezium has lots of use cases across the industries and has been helping the community to achieve different tasks like:</p><ul><li>Low latency data replication across multiple database instances or data centers</li><li>Creating audit logs for sensitive information</li><li>Implementing strangler pattern for transforming the legacy monolithic system to more of event driven architecture (EDA) or service oriented architecture (SOA)</li><li>Moving changes from transactional database to data lakes or OLAP stores (similar to the implementation of CQRS pattern)</li></ul><h3>Architecture</h3><p>We will be implementing the following architecture in the first part of the series. It will allow the user to read changes in the MySQL database that are stored in the Apache Kafka topics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZfAEelo4vUfub0bvNWP6hA.png" /><figcaption>Change Data Capture of MySQL database using Debezium</figcaption></figure><h3>Implementing the Architecture</h3><h4>Docker Compose to start all the services</h4><pre>$ git clone <a href="https://github.com/AmanLonare/Debezium-CDC-MySQL.git">https://github.com/AmanLonare/Debezium-CDC-MySQL.git</a></pre><pre>$ cd Debezium-CDC-MySQL/</pre><pre>$ docker-compose -f docker-compose-mysql-dbz.yml up -d</pre><p>All the services will start in the background. It might take a while to download all the images if they are not already available in your local environment. You can check the running container using the following command “docker ps”. You can see 5 containers of Kafka Connect, Kafka, Zookeeper, MySQL, and Schema Registry. In the first part of the series, we are more concerned on the change data events from the MySQL.</p><h4>Configure the MySQL Source Connector with the Kafka Connect</h4><pre>$ curl -H &quot;Accept:application/json&quot; localhost:8083/</pre><pre>{&quot;version&quot;:&quot;3.1.0&quot;,&quot;commit&quot;:&quot;cb8625948210849f&quot;}</pre><pre>$ curl -H &quot;Accept:application/json&quot; localhost:8083/connectors/</pre><pre>[]</pre><p>The first command will check the status of the Kafka Connect. If there is no error and the output is similar to the one show, then we can rest assured that Kafka Connect is successfully up and running. The second command will check the list of connectors already registered with the Kafka Connect. The list is empty as we haven’t yet registered any connectors yet.</p><p>The next step is to register the MySQL source connector with the following configuration file</p><pre>$ cat debezium-mysql-source-connector.json </pre><pre>{<br>    &quot;name&quot;: &quot;inventory-connector&quot;,<br>    &quot;config&quot;: {<br>        &quot;connector.class&quot;:        &quot;io.debezium.connector.mysql.MySqlConnector&quot;,<br>        &quot;tasks.max&quot;: &quot;1&quot;,<br>        &quot;database.hostname&quot;: &quot;mysql&quot;,<br>        &quot;database.port&quot;: &quot;3306&quot;,<br>        &quot;database.user&quot;: &quot;debezium&quot;,<br>        &quot;database.password&quot;: &quot;dbz&quot;,<br>        &quot;database.server.id&quot;: &quot;184054&quot;,<br>        &quot;database.server.name&quot;: &quot;dbserver1&quot;,<br>        &quot;database.include.list&quot;: &quot;inventory&quot;,<br>        &quot;database.history.kafka.bootstrap.servers&quot;: &quot;kafka:9092&quot;,       <br>        &quot;database.history.kafka.topic&quot;: &quot;schema-changes.inventory&quot;      <br>    }<br>}</pre><p>The JSON file describes the configuration of our source connector. The “name” field tells us the name of the connector. It can be anything of user’s choice. The “config” field contains the “class” of our source connector which is MySQL in our case. You can find various connectors and their classes provided by the Debezium in the following <a href="https://debezium.io/documentation/reference/stable/connectors/index.html">link</a>. It contains the “tasks” which should be 1 in the case of MySQL source connector since only one MySQL connector reads the MySQL server’s binlog. A single connector task ensures proper order and event handling. The Kafka Connect service uses connectors to start one or more tasks that do the work, and it automatically distributes the running tasks across the cluster of Kafka Connect services. If any of the services stop or crash, those tasks will be redistributed to running services. The “config” further contains database port, username, password, unique id and name to identify the MySQL server or cluster of servers. The name will be used as prefix for all Kafka topics. The “database.include.list” signifies to include only the changes in the “inventory” database to be detected. The final two fields of the “config” property helps the user to store the history of database schemas in Kafka using our broker and topic name. Upon restart, the connector will recover the schemas of the database that existed at the point in time in the binlog when the connector should begin reading.</p><p>The following command uses the Kafka Connect service’s API to submit the POST request against the /connectors resource with the JSON configuration file we have described.</p><pre>$ curl -i -X POST -H &quot;Accept:application/json&quot; -H &quot;Content-Type:application/json&quot; localhost:8083/connectors/ --data &quot;<a href="http://twitter.com/debezium">@debezium</a>-mysql-source-connector.json&quot;</pre><pre>HTTP/1.1 201 Created<br>Date: Sun, 03 Jul 2022 02:59:30 GMT<br>Location: <a href="http://localhost:8083/connectors/inventory-connector">http://localhost:8083/connectors/inventory-connector</a><br>Content-Type: application/json<br>Content-Length: 495<br>Server: Jetty(9.4.43.v20210629)</pre><pre>{&quot;name&quot;:&quot;inventory-connector&quot;,&quot;config&quot;:{&quot;connector.class&quot;:&quot;io.debezium.connector.mysql.MySqlConnector&quot;,&quot;tasks.max&quot;:&quot;1&quot;,&quot;database.hostname&quot;:&quot;mysql&quot;,&quot;database.port&quot;:&quot;3306&quot;,&quot;database.user&quot;:&quot;debezium&quot;,&quot;database.password&quot;:&quot;dbz&quot;,&quot;database.server.id&quot;:&quot;184054&quot;,&quot;database.server.name&quot;:&quot;dbserver1&quot;,&quot;database.include.list&quot;:&quot;inventory&quot;,&quot;database.history.kafka.bootstrap.servers&quot;:&quot;kafka:9092&quot;,&quot;database.history.kafka.topic&quot;:&quot;schema-changes.inventory&quot;,&quot;name&quot;:&quot;inventory-connector&quot;},&quot;tasks&quot;:[],&quot;type&quot;:&quot;source&quot;}</pre><p>The 201 response is created when we have successfully registered out source connector with the Kafka Connect. To further check if our source connector is registered or not, we can use the following command in our terminal. The response will give the name of the connectors registered with the Kafka Connector. We can have as many as we like as per our use case.</p><pre>$ curl -H &quot;Accept:application/json&quot; localhost:8083/connectors/</pre><pre>[&quot;inventory-connector&quot;]</pre><h4>Tailing Kafka Topics for Change Events</h4><p>We will be using a command line tool <a href="https://hub.docker.com/r/confluentinc/cp-kafkacat">Kafkacat</a>. Before using Kafkacat, you should know the network in which your containers are running. Docker compose command will create a default network for you where all the containers are running together. In my case the name of the default network is “debezium-cdc-mysql_default”.</p><pre>$ docker network ls</pre><pre>NETWORK ID     NAME                                 DRIVER    SCOPE<br>294ee1ef5251   bridge                               bridge    local<br>9222d05266cf   debezium-cdc-mysql_default           bridge    local<br>96dab83c6348   host                                 host      local<br>437726dd9735   none                                 null      local</pre><p>Following command will show all the topics present in the our Kafka broker. The Kafka automatically creates topics with names of the topics as per the names of the tables.</p><pre>$ docker run --tty --network &lt;your_network_name&gt;  confluentinc/cp-kafkacat kafkacat -b kafka:9092 -L</pre><p>You will see Kafka topics names like “dbserver1”, “dbserver1.inventory.geom”, “dbserver1.inventory.addresses”, “dbserver1.inventory.orders”, “dbserver1.inventory.products_on_hand”, “dbserver1.inventory.products”, “dbserver1.inventory.customers” which are named as per the tables present in the “inventory” database.</p><p>Now we will capture the changes in the “Customer” table in the “Inventory” database by tailing the dbserver1.inventory.customers topic. Run the following command in the new terminal.</p><pre>$ docker run --tty --network debezium-cdc-mysql_default confluentinc/cp-kafkacat kafkacat -b kafka:9092 -C -K: -f &#39;\nKey (%K bytes): %k\t\nValue (%S bytes): %s\n\Partitio<br>n: %p\tOffset: %o\n--\n&#39; -t dbserver1.inventory.customers</pre><p>You can see complex events structure describing the schema, before, after field of each row of the database. These are the events that describes the data already present in the tables. The following is an example of one of the events from the “customers” tables. Since there were already 4 rows of data, the offset at the end tells us that it will now read data at offset 4 (the offset starts from 0 so offset 3 is the fourth event in our topic)</p><pre>Key (168 bytes): {&quot;schema&quot;:{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;int32&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;id&quot;}],&quot;optional&quot;:false,&quot;name&quot;:&quot;dbserver1.inventory.customers.Key&quot;},&quot;payload&quot;:{&quot;id&quot;:1004}}<br>Value (2486 bytes): {&quot;schema&quot;:{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;int32&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;id&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;first_name&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;last_name&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;email&quot;}],&quot;optional&quot;:true,&quot;name&quot;:&quot;dbserver1.inventory.customers.Value&quot;,&quot;field&quot;:&quot;before&quot;},{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;int32&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;id&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;first_name&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;last_name&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;email&quot;}],&quot;optional&quot;:true,&quot;name&quot;:&quot;dbserver1.inventory.customers.Value&quot;,&quot;field&quot;:&quot;after&quot;},{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;version&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;connector&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;name&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;ts_ms&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;name&quot;:&quot;io.debezium.data.Enum&quot;,&quot;version&quot;:1,&quot;parameters&quot;:{&quot;allowed&quot;:&quot;true,last,false,incremental&quot;},&quot;default&quot;:&quot;false&quot;,&quot;field&quot;:&quot;snapshot&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;db&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;sequence&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;table&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;server_id&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;gtid&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;file&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;pos&quot;},{&quot;type&quot;:&quot;int32&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;row&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;thread&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;query&quot;}],&quot;optional&quot;:false,&quot;name&quot;:&quot;io.debezium.connector.mysql.Source&quot;,&quot;field&quot;:&quot;source&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;op&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;ts_ms&quot;},{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;id&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;total_order&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;data_collection_order&quot;}],&quot;optional&quot;:true,&quot;field&quot;:&quot;transaction&quot;}],&quot;optional&quot;:false,&quot;name&quot;:&quot;dbserver1.inventory.customers.Envelope&quot;},&quot;payload&quot;:{<strong>&quot;before&quot;</strong>:null,<strong>&quot;after&quot;</strong>:{&quot;id&quot;:1004,&quot;first_name&quot;:&quot;Anne&quot;,&quot;last_name&quot;:&quot;Kretchmar&quot;,&quot;email&quot;:&quot;annek@noanswer.org&quot;},<strong>&quot;source&quot;</strong>:{&quot;version&quot;:&quot;1.9.4.Final&quot;,&quot;connector&quot;:&quot;mysql&quot;,&quot;name&quot;:&quot;dbserver1&quot;,&quot;ts_ms&quot;:1656817172212,&quot;snapshot&quot;:&quot;true&quot;,&quot;db&quot;:&quot;inventory&quot;,&quot;sequence&quot;:null,&quot;table&quot;:&quot;customers&quot;,&quot;server_id&quot;:0,&quot;gtid&quot;:null,&quot;file&quot;:&quot;mysql-bin.000003&quot;,&quot;pos&quot;:157,&quot;row&quot;:0,&quot;thread&quot;:null,&quot;query&quot;:null},&quot;op&quot;:&quot;r&quot;,&quot;ts_ms&quot;:1656817172212,&quot;transaction&quot;:null}}<br>Partition: 0    Offset: 3<br><br>% Reached end of topic dbserver1.inventory.customers [0] at <strong>offset 4</strong><br>--</pre><h4>Observing the Change Events</h4><p>Now we will update the data in our inventory database by inserting few data into the table “customers”. We will log into the running MySQL container using the below commands. Enter “mysql -u mysqluser -p” once you entered into the container and provide the password as “mysqlpw”. These details were provided in the our docker compose file.</p><pre>$ docker exec -it &lt;container_id_of_MySQL&gt; bash</pre><pre>root@0fe701a833c8:/# mysql -u mysqluser -p<br>Enter password:</pre><p>Once you logged into the container yo can see the inventory database and contents inside the customers table using simple SQL queries.</p><pre>$ mysql&gt; SHOW databases;</pre><pre>+--------------------+<br>| Database           |<br>+--------------------+<br>| information_schema |<br>| inventory          |<br>+--------------------+<br>2 rows in set (0.01 sec)<br><br>mysql&gt; use inventory;<br>Reading table information for completion of table and column names<br>You can turn off this feature to get a quicker startup with -A<br><br>Database changed</pre><pre>mysql&gt; SHOW TABLES;</pre><pre>+---------------------+<br>| Tables_in_inventory |<br>+---------------------+<br>| addresses           |<br>| customers           |<br>| geom                |<br>| orders              |<br>| products            |<br>| products_on_hand    |<br>+---------------------+<br>6 rows in set (0.00 sec)</pre><p>Let us insert one row into our customers table. Once new row is added our next step will be to check if the changed data is captured by our Debezium or not.</p><pre>mysql&gt; use inventory;</pre><pre>Reading table information for completion of table and column names<br>You can turn off this feature to get a quicker startup with -A    <br><br>Database changed</pre><pre>mysql&gt; SELECT * FROM customers;</pre><pre>+------+------------+-----------+-----------------------+<br>| id   | first_name | last_name | email                 |<br>+------+------------+-----------+-----------------------+<br>| 1001 | Sally      | Thomas    | sally.thomas@acme.com |<br>| 1002 | George     | Bailey    | gbailey@foobar.com    |<br>| 1003 | Edward     | Walker    | ed@walker.com         |<br>| 1004 | Anne       | Kretchmar | annek@noanswer.org    |<br>+------+------------+-----------+-----------------------+<br>4 rows in set (0.00 sec)<br><br>mysql&gt; INSERT customers(first_name, last_name, email) VALUES(&quot;abc&quot;, &quot;def&quot;, &quot;abcdef@fake.com&quot;);</pre><pre>Query OK, 1 row affected (0.02 sec)</pre><p>Now we will switch to the terminal where we are running Kafkacat. We will see the following events in which I have highlighted the data we entered in their respective fields.</p><pre>Key (168 bytes): {&quot;schema&quot;:{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;int32&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;id&quot;}],&quot;optional&quot;:false,&quot;name&quot;:&quot;dbserver1.inventory.customers.Key&quot;},&quot;payload&quot;:{&quot;id&quot;:1005}}<br>Value (2480 bytes): {&quot;schema&quot;:{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;int32&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;id&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;first_name&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;last_name&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;email&quot;}],&quot;optional&quot;:true,&quot;name&quot;:&quot;dbserver1.inventory.customers.Value&quot;,&quot;field&quot;:&quot;before&quot;},{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;int32&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;id&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;first_name&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;last_name&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;email&quot;}],&quot;optional&quot;:true,&quot;name&quot;:&quot;dbserver1.inventory.customers.Value&quot;,&quot;field&quot;:&quot;after&quot;},{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;version&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;connector&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;name&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;ts_ms&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;name&quot;:&quot;io.debezium.data.Enum&quot;,&quot;version&quot;:1,&quot;parameters&quot;:{&quot;allowed&quot;:&quot;true,last,false,incremental&quot;},&quot;default&quot;:&quot;false&quot;,&quot;field&quot;:&quot;snapshot&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;db&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;sequence&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;table&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;server_id&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;gtid&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;file&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;pos&quot;},{&quot;type&quot;:&quot;int32&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;row&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;thread&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;query&quot;}],&quot;optional&quot;:false,&quot;name&quot;:&quot;io.debezium.connector.mysql.Source&quot;,&quot;field&quot;:&quot;source&quot;},{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;op&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:true,&quot;field&quot;:&quot;ts_ms&quot;},{&quot;type&quot;:&quot;struct&quot;,&quot;fields&quot;:[{&quot;type&quot;:&quot;string&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;id&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;total_order&quot;},{&quot;type&quot;:&quot;int64&quot;,&quot;optional&quot;:false,&quot;field&quot;:&quot;data_collection_order&quot;}],&quot;optional&quot;:true,&quot;field&quot;:&quot;transaction&quot;}],&quot;optional&quot;:false,&quot;name&quot;:&quot;dbserver1.inventory.customers.Envelope&quot;},&quot;payload&quot;:{<strong>&quot;before&quot;</strong>:null,<strong>&quot;after&quot;</strong>:<strong>{&quot;id&quot;:1005,&quot;first_name&quot;:&quot;abc&quot;,&quot;last_name&quot;:&quot;def&quot;,&quot;email&quot;:&quot;abcdef@fake.com&quot;}</strong>,&quot;source&quot;:{&quot;version&quot;:&quot;1.9.4.Final&quot;,&quot;connector&quot;:&quot;mysql&quot;,&quot;name&quot;:&quot;dbserver1&quot;,&quot;ts_ms&quot;:1656829756000,&quot;snapshot&quot;:&quot;false&quot;,&quot;db&quot;:&quot;inventory&quot;,&quot;sequence&quot;:null,&quot;table&quot;:&quot;customers&quot;,&quot;server_id&quot;:223344,&quot;gtid&quot;:null,&quot;file&quot;:&quot;mysql-bin.000003&quot;,&quot;pos&quot;:392,&quot;row&quot;:0,&quot;thread&quot;:14,&quot;query&quot;:null},&quot;op&quot;:&quot;c&quot;,&quot;ts_ms&quot;:1656829756750,&quot;transaction&quot;:null}}<br>Partition: 0    Offset: 4<br>--<br>% Reached end of topic dbserver1.inventory.customers [0] at offset 5</pre><h3>Conclusion</h3><p>There are many use cases involved replicating the data from one database management system to the other. This task sometimes become overwhelming to execute with so many options available. In this blog, I have introduced how we can capture any changes in a MySQL database using an open-source change data capture system, Debezium. We have seen that the task requires almost no coding but just few configuration setups. In the second and final part of the blog series, I will show how to use these change events as an input and replicate the operations on the source database to the sink database i.e. PostgreSQL in our case</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2732bedbec8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>