ballerinax/cdc Ballerina library

1.0.2

Overview

The Ballerina Change Data Capture (CDC) module provides APIs to capture and process database change events in real-time. This module enables developers to define services that handle change capture events such as inserts, updates, and deletes. It is built on top of the Debezium framework and supports popular databases like MySQL and Microsoft SQL Server.

With the CDC module, you can:

  • Capture real-time changes from databases.
  • Process and react to database events programmatically.
  • Build event-driven applications with ease.

Quickstart

Step 1: Import Required Modules

Add the following imports to your Ballerina program:

  • ballerinax/cdc: Core module that provides APIs to capture and process database change events.
  • ballerinax/mysql: Provides MySQL-specific listener and types for CDC. Replace with the corresponding module for your database if needed.
  • ballerinax/mysql.cdc.driver as _: Debezium-based driver for MySQL CDC. Use the appropriate driver for your database (e.g., mssql.cdc.driver, postgresql.cdc.driver, or oracledb.cdc.driver).
Copy
import ballerinax/cdc;
import ballerinax/mysql;
import ballerinax/mysql.cdc.driver as _;

Step 2: Configure the CDC Listener

Create a CDC listener for your MySQL database by specifying the connection details:

Copy
listener mysql:CdcListener mysqlListener = new ({
    hostname: "localhost",
    port: 3306,
    username: "username",
    password: "password",
    databaseInclude: ["inventory"]
});

Step 3: Define the CDC Service

Implement a cdc:Service to handle database change events:

Copy
service on mysqlListener {

    remote function onRead(record {} after) returns error? {
        // Handle the read event
        log:printInfo(`Record read: ${after}`);
    }

    remote function onCreate(record {} after) returns error? {
        // Handle the create event
        log:printInfo(`Record created: ${after}`);
    }

    remote function onUpdate(record {} before, record {} after) returns error? {
        // Handle the update event
        log:printInfo(`Record updated from: ${before}, to ${after}`);
    }

    remote function onDelete(record {} before) returns error? {
        // Handle the delete event
        log:printInfo(`Record deleted: ${before}`);
    }
}

Step 4: Run the Application

Run your Ballerina application:

Copy
bal run

Examples

The cdc module provides practical examples illustrating its usage in various real-world scenarios. Explore these examples to understand how to capture and process database change events effectively.

  1. Fraud Detection - Detect suspicious transactions in a financial database and send fraud alerts via email. This example showcases how to integrate the CDC module with the Gmail connector to notify stakeholders of potential fraud.

  2. Cache Management - Synchronize a Redis cache with changes in a MySQL database. It listens to changes in the products, vendors, and product_reviews tables and updates the Redis cache accordingly.

Import

import ballerinax/cdc;Copy

Metadata

Released date: 3 days ago

Version: 1.0.2

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 99

Current verison: 41


Weekly downloads


Source repository


Contributors

Other versions