# tainguyenbk/cdc-debezium-kafka-spark architecture

> Architecture diagram of the GitHub repository tainguyenbk/cdc-debezium-kafka-spark, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tainguyenbk/cdc-debezium-kafka-spark
- Repository: https://github.com/tainguyenbk/cdc-debezium-kafka-spark

## Overview

##### Project type and purpose
- This is an end‑to‑end real‑time data streaming pipeline that captures Change Data Capture (CDC) events from PostgreSQL, pushes them through Kafka, processes them with Spark Streaming, and stores results in MinIO.
- Its goal is low‑latency ingestion, transformation, and storage of relational data changes for analytics or downstream systems.

##### File structure
- Top level:
- docker‑compose.yaml → orchestrates Postgres, ZooKeeper, Kafka, Kafka Connect (Debezium) and MinIO containers
- csv_to_postgresql.py & customers.csv → helper script and initial data for populating Postgres
- debezium‑postgresql-connector.json → Debezium connector config for CDC
- command.sh → likely a convenience script for setup steps
- spark_client/
- Dockerfile, requirements.txt → Spark job container image
- src/
– schema_config.json → mapping/schema settings for streaming job
– stream_kafka_to_minio.py → Spark‑Streaming + Kafka consumer + MinIO writer

##### Extract architecture insights from README
- Components: PostgreSQL, Debezium (Kafka Connect), Kafka, Spark Streaming, MinIO
- Dataflow:
1. CSV loaded into Postgres
2. Debezium listens on WAL, emits CDC events into Kafka topic(s)
3. Spark Streaming job consumes from Kafka, transforms/serializes
4. Writes CSV & Parquet into MinIO bucket(s)
- Deployment: all services in Docker Compose, Spark job submitted via spark‑submit or run in container

- Debezium Connect (Kafka Connect worker)
- (Optionally) Docker Compose / Orchestration layer surrounding all services
c. Architectural patterns & principles:
- CDC pattern (Debezium + WAL)
- Event‑driven streaming pipeline
- Containerized microservices (Docker Compose)
- Lambda‑style real‑time processing (streaming job)
d. Key technologies & frameworks:
- PostgreSQL (REPLICA IDENTITY FULL)
- Debezium (Kafka Connect plugin)
- Apache Kafka (broker, topics)
- Spark Streaming (PySpark, kafka‑sql connector, Delta)
- MinIO (S3‑compatible object storage)
- Docker Compose for orchestration

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External scripts and initial load
    customers["customers.csv"]:::script
    CSV_LOAD["csv_to_postgresql.py"]:::script
    customers -->|loads CSV| CSV_LOAD
    CSV_LOAD -->|writes to| PG

    %% Docker Compose group
    subgraph "Docker Compose"
        direction TB
        PG["PostgreSQL (CDC WAL)"]:::db
        ZK["ZooKeeper"]:::mess
        KAFKA["Kafka Broker"]:::mess
        CONNECT["Debezium Connect"]:::mess
        MINIO["MinIO (Object Storage)"]:::db
        SCRIPT_HELPER["command.sh"]:::script
    end

    %% Connector configuration step
    CONNECT_CONFIG["Connector Config"]:::mess_conf
    SCRIPT_HELPER -->|POST REST API| CONNECT_CONFIG
    CONNECT_CONFIG -->|configures| CONNECT

    %% CDC flow
    PG -->|"WAL JSON events"| CONNECT
    CONNECT -->|"CDC events (JSON)"| KAFKA

    %% Processing layer
    subgraph "Processing"
        direction TB
        SPARK_JOB["Spark Streaming Job"]:::proc
        SCHEMA["schema_config.json"]:::proc_conf
        DOCKERFILE["Dockerfile"]:::proc_conf
        REQS["requirements.txt"]:::proc_conf
    end

    %% Streaming data flow
    KAFKA -->|"subscribe"| SPARK_JOB
    SPARK_JOB -->|"writes CSV/Parquet"| MINIO
    SCHEMA --> SPARK_JOB
    DOCKERFILE --> SPARK_JOB
    REQS --> SPARK_JOB

    %% Styling classes
    classDef db fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef mess fill:#FFA500,stroke:#333,stroke-width:1px
    classDef proc fill:#90EE90,stroke:#333,stroke-width:1px
    classDef script fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef proc_conf fill:#98FB98,stroke:#333,stroke-width:1px
    classDef mess_conf fill:#FFD580,stroke:#333,stroke-width:1px

    %% Click events
    click customers "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/customers.csv"
    click CSV_LOAD "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/csv_to_postgresql.py"
    click PG "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/docker-compose.yaml"
    click ZK "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/docker-compose.yaml"
    click KAFKA "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/docker-compose.yaml"
    click CONNECT "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/debezium-postgres-connector.json"
    click MINIO "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/docker-compose.yaml"
    click SCRIPT_HELPER "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/command.sh"
    click SPARK_JOB "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/spark_client/src/stream_kafka_to_minio.py"
    click SCHEMA "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/spark_client/src/schema_config.json"
    click DOCKERFILE "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/tree/main/spark_client/Dockerfile"
    click REQS "https://github.com/tainguyenbk/cdc-debezium-kafka-spark/blob/main/spark_client/requirements.txt"
```
