Skip to content

Unit 7 · Distributed Data Processing: Apache Hadoop

3 h contact · 6 h self study · lab Prerequisites: Units 5 and 6 CLO 6

1. Setting the scene

Scenario: The yearly report

The environment office asks for a yearly report: for every sensor, the number of hours above the WHO limit for fine dust, per month. That means scanning 12.6 TB.

Reading 12.6 TB from one disk at 200 MB/s takes

\[ \frac{12.6 \times 10^{12}\ \text{B}}{2 \times 10^{8}\ \text{B/s}} = 63{,}000\ \text{s} \approx 17.5\ \text{hours} \]

before a single computation happens. With 100 disks reading in parallel it takes about 10 minutes. The question is how to organise 100 machines so that each one reads its own data.

Guiding questions

  1. How can a file be larger than any disk and still be read in parallel?
  2. Why should we move the computation to the data, and not the data to the computation?
  3. How can a simple programming model hide scheduling, data movement and failures?

2. Intended learning outcomes

# Outcome Bloom CLO
ILO 7.1 Describe the Hadoop stack: HDFS, YARN and MapReduce. UNDERSTAND 6
ILO 7.2 Explain how HDFS blocks and replication enable parallelism and fault tolerance. UNDERSTAND 6
ILO 7.3 Trace a MapReduce job through splitting, mapping, shuffling and reducing. APPLY 6
ILO 7.4 Implement a word count with Hadoop Streaming in Python. APPLY 6
ILO 7.5 Estimate blocks, map tasks and storage for a dataset. APPLY 6

3. Why Hadoop in this module?

Hadoop is a foundational framework for distributed computing. It stores massive datasets across clusters and processes them in parallel close to where they are stored. It connects three ideas from this course: distributed memory, data parallelism and fault tolerance. Hadoop makes parallel computing possible at cluster scale.

Key principle

Move computation to the data, not data to the computation. A program of a few kilobytes travels to the machines holding the terabytes.

4. Architecture: storage + scheduling + processing

flowchart TB
    A["<b>Applications</b><br/>analytics · word count · ML · Spark · Hive"]
    M["<b>MapReduce</b> · parallel processing"]
    Y["<b>YARN</b> · resource management"]
    H["<b>HDFS</b> · distributed storage"]
    A --> M --> Y --> H
Layer Responsibility
HDFS distributed data storage
YARN cluster resource allocation
MapReduce parallel job execution

Node architecture

flowchart TB
    subgraph MASTER["Master node"]
      direction LR
      NN["NameNode (HDFS)<br/>file metadata"]
      RM["ResourceManager (YARN)<br/>cluster resources"]
    end
    subgraph WORKERS["Worker nodes · each runs both daemons"]
      direction LR
      W1["Worker 1<br/>DataNode + NodeManager"]
      W2["Worker 2<br/>DataNode + NodeManager"]
      W3["Worker 3<br/>DataNode + NodeManager"]
    end
    MASTER -.->|"metadata and scheduling"| WORKERS

5. HDFS: the distributed file system

Files are split into large blocks (128 MB by default) that are distributed across DataNodes and replicated (3 copies by default). The NameNode stores only metadata: which blocks form a file and where each block lives.

\[ \text{blocks} = \left\lceil \frac{\text{file size}}{\text{block size}} \right\rceil, \qquad \text{raw storage} = \text{RF} \times \text{file size} \]

Worked example: one day of SensorGrid data

\(35\ \text{GB} = 35{,}840\ \text{MB}\): \(\lceil 35{,}840 / 128 \rceil = 280\) blocks, stored as \(3 \times 280 = 840\) block replicas, using \(105\) GB of raw disk.

sequenceDiagram
    participant C as Client
    participant NN as NameNode
    participant D1 as DataNode 1
    participant D2 as DataNode 2
    C->>NN: open(file)
    NN-->>C: block locations
    C->>D1: read block 1
    D1-->>C: 128 MB
    C->>D2: read block 2
    D2-->>C: 128 MB
    Note over C,D2: data never passes the NameNode

Blocks enable parallelism

flowchart TB
    F["Large file"] --> B1["Block 1"] & B2["Block 2"] & B3["Block 3"]
    B1 --> N1["Node 1 · map task"]
    B2 --> N2["Node 2 · map task"]
    B3 --> N3["Node 3 · map task"]

Each block can be processed independently: data parallelism at cluster scale. If a DataNode dies, the NameNode notices the missing heartbeats and re replicates its blocks from the surviving copies.

6. MapReduce: the parallel computation model

flowchart LR
    I["Input data"] --> M["Map tasks<br/>(fully parallel)"] --> S["Shuffle<br/>group by key"] --> R["Reduce tasks<br/>(aggregate)"] --> O["Output"]

The programmer writes two functions:

\[ \text{map}: (k_1, v_1) \rightarrow \text{list}(k_2, v_2) \qquad\qquad \text{reduce}: (k_2, \text{list}(v_2)) \rightarrow \text{list}(v_3) \]

The framework does everything else: splitting, scheduling near the data, sorting, moving intermediate data, and re running failed tasks.

Execution overview

Following Dean and Ghemawat's original design:

flowchart TB
    UP["User program"] -->|"(1) fork"| MA["Master"]
    UP -->|"(1) fork"| WK["Workers"]
    MA -->|"(2) assign map"| MW1["Map worker 1"]
    MA -->|"(2) assign map"| MW2["Map worker 2"]
    MA -->|"(2) assign reduce"| RW1["Reduce worker 1"]
    MA -->|"(2) assign reduce"| RW2["Reduce worker 2"]
    S0[("Split 0")] -->|"(3) read"| MW1
    S1[("Split 1")] -->|"(3) read"| MW2
    MW1 -->|"(4) local write,<br/>partitioned by key"| LD1[("Local disk")]
    MW2 -->|"(4) local write"| LD2[("Local disk")]
    LD1 & LD2 -->|"(5) remote read"| RW1 & RW2
    RW1 -->|"(6) write"| O1[("Output file 0")]
    RW2 -->|"(6) write"| O2[("Output file 1")]

Which reducer receives a key? A partition function, typically

\[ \text{reducer}(k) = h(k) \bmod R \]

so that all values of the same key meet at the same reducer.

The word count process step by step

flowchart LR
    subgraph IN["Input"]
      i["Deer Bear River<br/>Car Car River<br/>Deer Car Bear"]
    end
    subgraph SP["Splitting"]
      s1["Deer Bear River"]
      s2["Car Car River"]
      s3["Deer Car Bear"]
    end
    subgraph MP["Mapping"]
      m1["Deer,1 · Bear,1 · River,1"]
      m2["Car,1 · Car,1 · River,1"]
      m3["Deer,1 · Car,1 · Bear,1"]
    end
    i --> s1 & s2 & s3
    s1 --> m1
    s2 --> m2
    s3 --> m3
flowchart LR
    subgraph SH["Shuffling"]
      h1["Bear: 1, 1"]
      h2["Car: 1, 1, 1"]
      h3["Deer: 1, 1"]
      h4["River: 1, 1"]
    end
    subgraph RD["Reducing"]
      r1["Bear, 2"]
      r2["Car, 3"]
      r3["Deer, 2"]
      r4["River, 2"]
    end
    h1 --> r1
    h2 --> r2
    h3 --> r3
    h4 --> r4
    r1 & r2 & r3 & r4 --> F["Final result"]
#!/usr/bin/env python3
import sys
for line in sys.stdin:
    for word in line.strip().lower().split():
        print(f"{word}\t1")
#!/usr/bin/env python3
import sys
current, count = None, 0
for line in sys.stdin:                      # input arrives sorted by key
    word, value = line.rstrip("\n").split("\t")
    if word != current:
        if current is not None:
            print(f"{current}\t{count}")
        current, count = word, 0
    count += int(value)
if current is not None:
    print(f"{current}\t{count}")
# locally: the pipe imitates map → shuffle (sort) → reduce
cat input.txt | python3 mapper.py | sort | python3 reducer.py
# bear 2 · car 3 · deer 2 · river 2

# on the cluster
hdfs dfs -put input.txt /user/student/input/
mapred streaming \
  -files mapper.py,reducer.py \
  -mapper mapper.py -reducer reducer.py \
  -input /user/student/input -output /user/student/wc_out

Reducing the shuffle with a combiner

Without a combiner, every (word, 1) crosses the network. A combiner runs the reducer logic locally on each mapper's output first. For mapper 2 above, Car,1 · Car,1 becomes Car,2. In general, if a mapper emits \(n\) pairs over \(u\) distinct keys, the combiner shrinks its shuffle volume from \(n\) to \(u\) pairs; for the frequent words of natural language, \(u \ll n\).

7. YARN: the operating system of the cluster

flowchart TB
    RM["ResourceManager<br/>global scheduling"] --> NM1["NodeManager 1<br/>containers"]
    RM --> NM2["NodeManager 2<br/>containers"]
    RM --> NM3["NodeManager 3<br/>containers"]
sequenceDiagram
    participant C as Client
    participant RM as ResourceManager
    participant NM as NodeManager
    participant AM as ApplicationMaster
    C->>RM: submit job
    RM->>NM: start ApplicationMaster container
    NM->>AM: launch
    AM->>RM: request containers (near the data)
    RM-->>AM: containers granted
    AM->>NM: run map and reduce tasks
    AM-->>C: job finished

The ResourceManager schedules globally; NodeManagers manage containers on each node. YARN lets several jobs run in parallel on one cluster, and lets engines other than MapReduce (Spark, Hive, Tez) share it.

8. Lab

Hadoop lab

  1. Deploy a Hadoop cluster with Docker.
  2. Store a dataset in HDFS.
  3. Run a MapReduce job on YARN.
  4. Monitor the job in the ResourceManager web UI.
  5. Query the data with Hive.

Instructions: github.com/bugmend/bigdataanalytics/tree/main/hadoop

9. Classwork

Plan the yearly SensorGrid job (20 min)

12.6 TB, block size 128 MB, 100 worker nodes with 8 map slots each.

  1. How many blocks and map tasks?
  2. How many "waves" of map tasks are needed?
  3. Write map and reduce (in pseudocode) for "hours per sensor and month with PM2.5 above 15 µg/m³".
Solutions
  1. \(12.6 \times 10^{6}\ \text{MB} / 128 \approx 98{,}438\) blocks, so about 98,438 map tasks (one per split).
  2. \(800\) slots: \(\lceil 98{,}438 / 800 \rceil = 124\) waves.
  3. map: for each reading with pm25 > 15, emit ((sensor_id, month), 5 s); reduce: sum the seconds and divide by 3,600.

10. Check your understanding

Why does the NameNode not become a bandwidth bottleneck?

It serves only metadata; clients read blocks directly from DataNodes.

Why must reducer input be sorted or grouped by key?

A reducer must see all values of a key together to aggregate them; the shuffle guarantees this.

What does a DataNode failure cost a running job?

Map tasks on that node are re executed elsewhere from another replica of their block; the job continues.

11. Key takeaways

  • Hadoop = HDFS (storage) + YARN (scheduling) + MapReduce (processing).
  • Big files become blocks; blocks are replicated and processed in parallel where they live.
  • MapReduce: map in parallel, shuffle by key, reduce per key; the framework handles failures.
  • Combiners and locality keep network traffic low.

Portfolio option: implement a MapReduce or Spark batch job on the SensorGrid data and report blocks, tasks, run time and the effect of a combiner.

Further reading

  • J. Dean, S. Ghemawat (2008), MapReduce: Simplified Data Processing on Large Clusters, CACM 51(1), 107 to 113.
  • M. van Groningen (2009), Introduction to Hadoop, Trifork Blog.
  • T. White, Hadoop: The Definitive Guide, O'Reilly.