{% extends "base.html" %} {% block title %}Big Data & Engineering - Data Science Lab{% endblock %} {% block content %}

πŸ“– Batch vs Streaming Processing

Data processing falls into two paradigms, both relevant to modern data infrastructure:

🎯 Batch Processing

Processes large volumes of data at scheduled intervals. Example: Monthly housing resale transaction reports published by housing. Data is collected over a month, then processed and published as a complete dataset.

Concept
Batch Processing Pipeline:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ housing Data │───>β”‚  Extract  │───>β”‚Transform │───>β”‚   Load   β”‚
β”‚  (CSV)   β”‚    β”‚  Monthly  β”‚    β”‚  Clean   β”‚    β”‚  SQLite  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  Scheduled       Pandas          Database
                  (Cron/DAG)      Processing      Storage

🎯 Stream Processing

Processes data in real-time as it arrives. Example: transit crowding updates every few minutes from Transport Authority, weather updates from Environment Agency, or road toll readings in real-time.

Concept
Stream Processing Pipeline:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚transit Crowd │───>β”‚  Ingest   │───>β”‚ Window   │───>β”‚  Alert   β”‚
β”‚  Events  β”‚    β”‚  Events   β”‚    β”‚ Aggregateβ”‚    β”‚Dashboard β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  Continuous     5-min windows   Real-time

πŸ“– Data Lake vs Data Warehouse

AspectData LakeData Warehouse
SchemaSchema-on-readSchema-on-write
Data formatRaw (JSON, CSV, Parquet)Structured (tables)
UsersData scientists, engineersAnalysts, business users
ProcessingELT (Extract-Load-Transform)ETL (Extract-Transform-Load)
ExampleRaw open data portal API responsesStar schema with facts/dims
CostLower (cheap storage)Higher (compute + storage)

πŸ“– ETL vs ELT

ETL (Traditional)

1. Extract from open data portal API
2. Transform in Python (clean, validate)
3. Load into dimensional tables

# Used when: Target system has limited
# compute (e.g., small SQLite DB)

ELT (Modern)

1. Extract from open data portal API
2. Load raw data into data lake
3. Transform using SQL in warehouse

# Used when: Target system has powerful
# compute (e.g., BigQuery, Snowflake)

πŸ“– Key Engineering Concepts

Idempotency

Running the same pipeline multiple times produces the same result. Example: Re-ingesting January 2024 housing data should not create duplicate rows.

Python
# Idempotent ingestion pattern
def ingest_hdb_month(month):
    # Delete existing data for this month first
    execute_db("DELETE FROM raw_data WHERE month = ?", (month,))
    # Then insert fresh data
    df = fetch_housing_data(month)
    df.to_sql('raw_data', conn, if_exists='append', index=False)

Partitioning

Splitting large datasets into smaller chunks for parallel processing. Example: Partition housing data by town or by year-month for faster queries.

Event-Driven Architecture

Systems react to events rather than polling. Example: When a new transit crowding update is published, trigger alerts and dashboard refreshes.

Data Lineage

Tracking where data came from and how it was transformed. Example: "This average housing price was derived from raw_data β†’ cleaned β†’ aggregated by town."

Retries and Monitoring

Python
import time

def with_retry(func, max_retries=3, delay=2):
    """Retry function with exponential backoff."""
    for attempt in range(max_retries):
        try:
            return func()
        except Exception as e:
            if attempt == max_retries - 1:
                raise
            time.sleep(delay * (2 ** attempt))
            print(f"Retry {attempt + 1}/{max_retries}: {e}")
πŸ§ͺ Try It Yourself

1. Design an ETL pipeline for various weather data from Environment Agency.

2. How would you make the housing ingestion pipeline idempotent?

3. Draw a data lineage diagram from raw CSV to average price per town.

{% endblock %}