Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Real-Time Streaming Data Pipeline Project

TechStack

Apache Kafka Apache Spark Terraform Airflow Docker Amazon Redshift

Table of Contents

  1. πŸ“ Introduction
  2. πŸ“œ Project Overview
  3. πŸ› οΈ Technologies Used
  4. πŸ—‚οΈ Project Structure
  5. πŸ”„ Workflow
  6. πŸ› οΈ Setup Instructions
  7. 🌟 Key Learnings

πŸ“ Introduction

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 πŸš€.


πŸ“œ Project Overview

Architecture diagram

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.


πŸ› οΈ Technologies Used

1. Apache Kafka

  • 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.

2. Apache Spark

  • 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.

3. Amazon Redshift

  • 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.

4. Terraform

  • 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.

5. Docker

  • Purpose: Provides a containerized environment for running services like Kafka, Spark, and Airflow.
  • Key Features: Portability πŸ“¦, scalability πŸ“Š, simplified deployment πŸš€, and support for reproducible environments.

6. 🌬️ Airflow

  • Purpose: Orchestrates the data pipeline workflow.
  • Key Features: Task scheduling πŸ—“οΈ, monitoring πŸ“ˆ, error handling πŸ› οΈ, and DAG-based workflows.

πŸ—‚οΈ Project Structure

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 πŸ“

πŸ”„ Workflow

Step-by-Step Process

  1. Airflow DAG: Fetching and Streaming Data

    Airflow dag

    • 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, and picture.
  2. Kafka Producer Configuration:

    Kafka Topic

    • 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.
  3. Spark Structured Streaming:

    • Spark is configured using spark-sql-kafka JARs for seamless Kafka integration.
    • A custom schema parses incoming Kafka messages.
    • Transformations include:
      • Adding a date_added column (current timestamp).
      • Validating records and logging invalid entries.
  4. Data Pipeline Configuration:

    Spark_worker_node

    • Data flows into Spark for real-time processing.
    • The transformed data is written to Amazon Redshift via JDBC.
  5. Redshift Setup via Terraform:

    Redshift

    • Terraform provisions the following:
      • A single-node Redshift cluster (user_data database).
      • Necessary IAM roles for secure S3 integration (if staging is needed).
      • A table schema with fields including the date_added column.
  6. Dockerized Network:

    Docker

    • Services such as Kafka, Spark, Zookeeper, and Airflow run on a single Docker network.
    • Docker Compose simplifies service orchestration for local development.
  7. 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.

πŸ› οΈ Setup Instructions

Prerequisites

  • 🐳 Docker installed on your machine.
  • 🌍 Terraform installed.
  • 🐍 Python 3.8 or above.
  • ☁️ AWS credentials with necessary permissions (Redshift, S3).

Steps

1. Clone the Repository

git clone <repository-url>
cd project-root

2. Set Up Environment Variables

Create 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:9092

3. Install Python Dependencies

Activate your virtual environment and install dependencies:

source data/bin/activate
pip install -r requirements.txt

4. Run Terraform

Navigate to the Terraform directory and apply configurations:

cd terraform
terraform init
terraform apply

5. Run Docker Compose

Start Kafka, Zookeeper, and other services:

docker-compose up -d

6. Run the Airflow DAG

Start the Airflow scheduler and webserver, then trigger the user_automation DAG.

7. Run the Python Script

Execute the main script to start streaming:

python main/main.py

🌟 Key Learnings

  1. 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.
  2. Infrastructure as Code (IaC):

    • Automating infrastructure setup with Terraform 🌍 for consistency and scalability.
    • Managing changes to cloud resources through version-controlled configurations.
  3. Data Warehouse Integration:

    • Writing streaming data into Amazon Redshift πŸ—„οΈ for analytics with optimized query performance.
    • Structuring data for effective business intelligence insights.
  4. Containerization:

    • Simplifying deployment using Docker 🐳 and Docker Compose 🧩 for reproducible environments.
    • Isolating dependencies and ensuring consistent setups across systems.
  5. 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.
  6. 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.

About

A scalable pipeline leveraging Apache Kafka, Spark, and Amazon Redshift for real-time data processing and analytics. Features infrastructure automation with Terraform and containerized deployment using Docker. Perfect for learning and implementing real-time event-driven architectures.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages