-
Notifications
You must be signed in to change notification settings - Fork 4
Add examples #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add examples #7
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,3 +62,5 @@ examples/**/Config.toml | |
|
|
||
| # Environment files | ||
| *.env | ||
|
|
||
| examples/*/tmp | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../Cache Management.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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