Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ examples/**/Config.toml

# Environment files
*.env

examples/*/tmp
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ bal run

## Examples

The `cdc` module provides practical examples illustrating its usage in various real-world scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples) to understand how to capture and process database change events effectively.

1. [Fraud Detection](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples/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](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples/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.

## Issues and projects

Expand Down
6 changes: 6 additions & 0 deletions ballerina/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,9 @@ bal run
```

## Examples

The `cdc` module provides practical examples illustrating its usage in various real-world scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples) to understand how to capture and process database change events effectively.

1. [Fraud Detection](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples/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](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples/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.
41 changes: 41 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Examples

The `cdc` module provides practical examples illustrating its usage in various real-world scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples) to understand how to capture and process database change events effectively.

1. [Fraud Detection](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples/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](https://github.com/ballerina-platform/module-ballerinax-cdc/tree/main/examples/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.

## Running an Example

Execute the following commands to build an example from the source:

* To build an example:

```bash
bal build
```

* To run an example:

```bash
bal run
```

## Building the Examples with the Local Module

**Warning**: Due to the absence of support for reading local repositories for single Ballerina files, the Bala of the module is manually written to the central repository as a workaround. Consequently, the bash script may modify your local Ballerina repositories.

Execute the following commands to build all the examples against the changes you have made to the module locally:

* To build all the examples:

```bash
./build.sh build
```

* To run all the examples:

```bash
./build.sh run
```
1 change: 1 addition & 0 deletions examples/cache-management/.github/README.md
17 changes: 17 additions & 0 deletions examples/cache-management/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
org = "wso2"
name = "cache_management"
version = "0.1.0"
distribution = "2201.12.2"

[[dependency]]
org="ballerinax"
name="cdc"
version="0.1.0"
repository="local"

[[dependency]]
org="ballerinax"
name="cdc.mysql.driver"
version="0.1.0"
repository="local"
Comment on lines +7 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove them as examples will be used by developers after the module is released.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will do once the driver is released and the PR is updated

78 changes: 78 additions & 0 deletions examples/cache-management/Cache Management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Cache Management

This example demonstrates how to use the Ballerina Change Data Capture (CDC) module to 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.

## Setup Guide

### 1. MySQL Database

1. Refer to the [Setup Guide](https://central.ballerina.io/ballerinax/cdc/latest#setup-guide) for the necessary steps to enable CDC in the MySQL server.

2. Add the necessary schema and data using the `setup.sql` script:
```bash
mysql -u <username> -p < db_scripts/setup.sql
```

### 2. Redis Server

Ensure a Redis server is running on `localhost:6379`.

### 3. Configuration

Configure MySQL database credentials in the `Config.toml` file located in the example directory:

```toml
username = "<DB Username>"
password = "<DB Password>"
```

Replace `<DB Username>` and `<DB Password>` with your MySQL database credentials.

## Setup Guide: Using Docker Compose

You can use Docker Compose to set up both MySQL and Redis services for this example. Follow these steps:

### 1. Start the services

Run the following command to start both MySQL and Redis services:

```bash
docker-compose up -d
```

### 2. Verify the services

Ensure both `mysql` and `redis` services are in a healthy state:

```bash
docker-compose ps
```

### 3. Configuration

Ensure the `Config.toml` file is updated with the following credentials:

```toml
username = "cdc_user"
password = "cdc_password"
```

## Run the Example

1. Execute the following command to run the example:

```bash
bal run
```

2. Use the provided `test.sql` script to insert sample transactions into the `products`, `vendors`, and `product_reviews` tables to test the synchronization. Run the following command:

```bash
mysql -u <username> -p < db_scripts/test.sql
```

If using docker services,

```bash
docker exec -i mysql-cdc mysql -u cdc_user -pcdc_password < db-scripts/test.sql
```
38 changes: 38 additions & 0 deletions examples/cache-management/db-scripts/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
CREATE DATABASE IF NOT EXISTS store_db;
USE store_db;

CREATE TABLE vendors (
id INT PRIMARY KEY,
name VARCHAR(255),
contact_info TEXT
);

INSERT INTO vendors VALUES
(1, 'Samsung', 'contact@samsung.com'),
(2, 'Apple', 'contact@apple.com');

CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(255),
price DECIMAL(10,2),
description TEXT,
vendor_id INT,
FOREIGN KEY (vendor_id) REFERENCES vendors(id)
);

INSERT INTO products VALUES
(1001, 'Samsung Galaxy S24', 999.99, 'Flagship phone with AI camera', 1),
(1002, 'Apple iPhone 15 Pro', 1099.00, 'New titanium design', 2);

CREATE TABLE product_reviews (
review_id INT PRIMARY KEY,
product_id INT,
rating INT CHECK (rating BETWEEN 1 AND 5),
comment TEXT,
FOREIGN KEY (product_id) REFERENCES products(id)
);

INSERT INTO product_reviews VALUES
(1, 1001, 5, 'Amazing camera'),
(2, 1001, 4, 'Great battery life'),
(3, 1002, 5, 'Best iPhone yet');
14 changes: 14 additions & 0 deletions examples/cache-management/db-scripts/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
USE store_db;

UPDATE products
SET price = price * 0.9
WHERE id = 1002;

UPDATE product_reviews
SET rating = rating - 1
WHERE product_id = 1002;

INSERT products VALUES (1003, "Samsung Galaxy S20", 499.99, "Old Smartphone", 2);

DELETE FROM products
WHERE id = 1003;
31 changes: 31 additions & 0 deletions examples/cache-management/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: cache-management-example

services:
mysql:
image: mysql:8.0
container_name: mysql-cdc
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: store_db
MYSQL_USER: cdc_user
MYSQL_PASSWORD: cdc_password
volumes:
- ./db-scripts/setup.sql:/docker-entrypoint-initdb.d/setup.sql
healthcheck:
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
interval: 10s
timeout: 5s
retries: 5

redis:
image: redis:latest
container_name: redis-cache
ports:
- "6379:6379"
healthcheck:
test: [ "CMD", "redis-cli", "ping" ]
interval: 10s
timeout: 5s
retries: 5
Loading
Loading