{% extends "base.html" %} {% block title %}Big Data & Engineering - Data Science Lab{% endblock %} {% block content %}
Data processing falls into two paradigms, both relevant to modern data infrastructure:
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.
Batch Processing Pipeline:
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
β housing Data ββββ>β Extract ββββ>βTransform ββββ>β Load β
β (CSV) β β Monthly β β Clean β β SQLite β
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
Scheduled Pandas Database
(Cron/DAG) Processing StorageProcesses 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.
Stream Processing Pipeline:
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
βtransit Crowd ββββ>β Ingest ββββ>β Window ββββ>β Alert β
β Events β β Events β β Aggregateβ βDashboard β
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
Continuous 5-min windows Real-time| Aspect | Data Lake | Data Warehouse |
|---|---|---|
| Schema | Schema-on-read | Schema-on-write |
| Data format | Raw (JSON, CSV, Parquet) | Structured (tables) |
| Users | Data scientists, engineers | Analysts, business users |
| Processing | ELT (Extract-Load-Transform) | ETL (Extract-Transform-Load) |
| Example | Raw open data portal API responses | Star schema with facts/dims |
| Cost | Lower (cheap storage) | Higher (compute + storage) |
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)
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)
Running the same pipeline multiple times produces the same result. Example: Re-ingesting January 2024 housing data should not create duplicate rows.
# 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)Splitting large datasets into smaller chunks for parallel processing. Example: Partition housing data by town or by year-month for faster queries.
Systems react to events rather than polling. Example: When a new transit crowding update is published, trigger alerts and dashboard refreshes.
Tracking where data came from and how it was transformed. Example: "This average housing price was derived from raw_data β cleaned β aggregated by town."
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}")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.