- π Introduction
- π Project Overview
- π οΈ Technologies Used
- ποΈ Project Structure
- π Workflow
- π οΈ Setup Instructions
- π Key Learnings
This project π demonstrates a real-time data streaming pipeline π using modern technologies like Apache Kafka π, Apache Spark π, and Amazon Redshift π, orchestrated with Terraform π for infrastructure management and Docker π³ for containerization. The focus is on processing real-time events with Spark Structured Streaming β‘, integrating Kafka as the message broker π’, and storing processed data in Redshift for analytics π.
This repository showcases the integration of these technologies for scalable, reliable, and efficient real-time data processing ποΈ. By leveraging these cutting-edge tools, the project demonstrates how to transform raw event streams into meaningful insights in real time, showcasing practical applications across industries.
With this approach, users can learn how to implement data-driven solutions that are robust π, dynamic π, and tailored to solve real-world challenges. This project introduces a comprehensive way to use state-of-the-art technologies for handling high-volume, real-time data while ensuring accuracy, consistency, and flexibility for future expansion π.
The pipeline is designed to process user creation events π€ from a Kafka topic π‘, transform the data using Spark Structured Streaming β¨, and store the results in a Redshift database ποΈ. The data pipeline supports scalability π, resilience π‘οΈ, and real-time processing β±οΈ, making it suitable for industrial applications like monitoring systems π οΈ, IoT π, e-commerce analytics ποΈ, and customer behavior analysis. This approach enhances decision-making, optimizes operational efficiency, and provides a framework for building advanced real-time solutions.
The project stands as an example of how modern technologies can synergize to deliver a seamless flow of information, ensuring timely responses to critical events and uncovering actionable insights π. Whether itβs handling IoT device logs or analyzing customer purchasing patterns, the architecture is built to cater to demanding data-driven scenarios.
- Purpose: Kafka serves as the message broker π€, enabling the streaming of real-time user events.
- Key Features: Scalability π, fault tolerance π‘οΈ, high throughput β‘, and distributed architecture.
- Purpose: Used for real-time data transformation and processing with Spark Structured Streaming.
- Key Features: Distributed data processing βοΈ, fault tolerance π, seamless integration with Kafka, and support for large-scale datasets.
- Purpose: Serves as the data warehouse π’ for storing processed user data.
- Key Features: Scalability π, analytics optimization π, columnar storage for performance, and integration with other AWS services.
- Purpose: Manages infrastructure as code π₯οΈ to provision resources like Redshift and S3 buckets.
- Key Features: Declarative syntax ποΈ, automation π€, cloud-agnostic capabilities βοΈ, and version control for infrastructure.
- Purpose: Provides a containerized environment for running services like Kafka, Spark, and Airflow.
- Key Features: Portability π¦, scalability π, simplified deployment π, and support for reproducible environments.
- Purpose: Orchestrates the data pipeline workflow.
- Key Features: Task scheduling ποΈ, monitoring π, error handling π οΈ, and DAG-based workflows.
project-root/
βββ dags/ # For Airflow DAGs (if used)
βββ data/ # Python virtual environment (added to .gitignore)
βββ logs/ # Logs for debugging π οΈ
βββ terraform/ # Terraform configurations π
β βββ main.tf # Redshift connection and configurations ποΈ
β βββ providers.tf # Providers configuration π
β βββ variables.tf # Variables definition π
β βββ outputs.tf # Outputs definition π€
β βββ table_schema.sql # SQL script to create table schema π
βββ script/ # Utility Airflow scripts π οΈ
β βββ entrypoint.sh # Entry point script for π
βββ main/ # Main Spark job scripts π
β βββ spark_stream.py # Spark streaming job
β βββ test_kafka.py # Test run for Kafka streaming π
β βββ __init__.py # Package initialization π¦
βββ .gitignore # Git ignore rules π«
βββ docker-compose.yml # Docker Compose configuration π³
βββ requirements.txt # Python dependencies π
βββ README.md # Documentation π
-
Airflow DAG: Fetching and Streaming Data
- A custom Airflow DAG (
user_automation) fetches user data from a publicly available API (e.g.,https://randomuser.me). - The DAG orchestrates the task of fetching data and sending it to the Kafka producer topic
user_created. - Fields Extracted:
id,first_name,last_name,gender,address,post_code,email,username,registered_date,phone, andpicture.
- A custom Airflow DAG (
-
Kafka Producer Configuration:
- Kafka is configured to accept incoming user data streamed in real time by the Airflow DAG.
- Messages are serialized in JSON format before publishing.
- Dockerized Kafka brokers ensure easy scalability and maintenance.
-
Spark Structured Streaming:
- Spark is configured using
spark-sql-kafkaJARs for seamless Kafka integration. - A custom schema parses incoming Kafka messages.
- Transformations include:
- Adding a
date_addedcolumn (current timestamp). - Validating records and logging invalid entries.
- Adding a
- Spark is configured using
-
Data Pipeline Configuration:
- Data flows into Spark for real-time processing.
- The transformed data is written to Amazon Redshift via JDBC.
-
Redshift Setup via Terraform:
- Terraform provisions the following:
- A single-node Redshift cluster (
user_datadatabase). - Necessary IAM roles for secure S3 integration (if staging is needed).
- A table schema with fields including the
date_addedcolumn.
- A single-node Redshift cluster (
- Terraform provisions the following:
-
Dockerized Network:
- Services such as Kafka, Spark, Zookeeper, and Airflow run on a single Docker network.
- Docker Compose simplifies service orchestration for local development.
-
End-to-End Data Flow:
- The pipeline starts with the Airflow DAG fetching API data, sending it to Kafka.
- Spark consumes the Kafka messages, processes them, and writes to Redshift.
- Logs are stored locally for debugging and monitoring.
- π³ Docker installed on your machine.
- π Terraform installed.
- π Python 3.8 or above.
- βοΈ AWS credentials with necessary permissions (Redshift, S3).
git clone <repository-url>
cd project-rootCreate a .env file:
REDSHIFT_JDBC_URL=jdbc:redshift://<redshift-cluster-endpoint>:5439/user_data
REDSHIFT_USERNAME=admin_user_01
REDSHIFT_PASSWORD=<your-password>
KAFKA_BROKER=localhost:9092Activate your virtual environment and install dependencies:
source data/bin/activate
pip install -r requirements.txtNavigate to the Terraform directory and apply configurations:
cd terraform
terraform init
terraform applyStart Kafka, Zookeeper, and other services:
docker-compose up -dStart the Airflow scheduler and webserver, then trigger the user_automation DAG.
Execute the main script to start streaming:
python main/main.py-
Real-Time Data Streaming:
- Understanding Kafka's producer-consumer architecture π, including partitioning and offset management.
- Leveraging Spark Structured Streaming β‘ for scalable processing and fault-tolerant computations.
-
Infrastructure as Code (IaC):
- Automating infrastructure setup with Terraform π for consistency and scalability.
- Managing changes to cloud resources through version-controlled configurations.
-
Data Warehouse Integration:
- Writing streaming data into Amazon Redshift ποΈ for analytics with optimized query performance.
- Structuring data for effective business intelligence insights.
-
Containerization:
- Simplifying deployment using Docker π³ and Docker Compose π§© for reproducible environments.
- Isolating dependencies and ensuring consistent setups across systems.
-
Integration of Airflow, Spark, and Kafka:
- Configuring Airflow to orchestrate API-based data collection and Kafka message production.
- Setting up a Spark Kafka consumer π for real-time event ingestion.
- Handling schema evolution and data enrichment dynamically.
-
Best Practices:
- Organizing repositories ποΈ for maintainability and scalability π.
- Securing sensitive information using environment variables π.
- Implementing logging and monitoring for observability.
This repository π exemplifies the integration of modern data engineering tools π οΈ and techniques π§ for building robust πͺ and scalable π real-time data pipelines β‘. It lays the groundwork for developing production-ready systems with enhanced analytics capabilities.





