MapReduce and YARN
This lecture explains MapReduce (split, map, shuffle and sort, reduce) with full word-count traces. It also explains the Hadoop v1 daemons and YARN resource management. After this page you can trace any word count and assign partitions by hand.
MapReduce Architecture: Distributed Big Data ProcessingThe Coding Gopher · MapReduce with word count
Map Reduce and Hadoop Distributed File SystemSigma Solver · MapReduce and HDFS
Understanding Word Count Program with Map Reduce (With Demonstration)Unboxing Big Data · Word count step by step
Apache Hadoop YARN Explained: ResourceManager, NodeManager & ContainerRoyal Research - Official · ResourceManager, NodeManager, container
Apache Hadoop YARN: How YARN changed Hadoop from v1 to v2Hortonworks · From Hadoop v1 to YARNWhat MapReduce isSearch on YouTube
MapReduce is a framework for distributed and parallel processing of large data volumes. The idea comes from functional programming. It applies one function to many items independently. The map step transforms items. The reduce step collates items into new values. This pattern suits clusters well.
Dean and Ghemawat published the method in 2004. Their paper "MapReduce: Simplified Data Processing on Large Clusters" describes split, process and aggregate steps for the Google search engine. The open-source version shipped inside Apache Hadoop.
The slide pipeline has six stages:
- Data block.
- Read and process (map jobs).
- Generate key-value pairs.
- Receive key-value pairs (reduce side).
- Shuffle and sort.
- Aggregate key-value pairs into smaller sets.
MapReduce programming splits the input into independent chunks. Map tasks process the chunks fully in parallel. A reduce task then combines the map outputs into the final result.
Phases of MapReduceSearch on YouTube
The four phases run in this order: split, map, shuffle and sort, reduce. Input chunks enter the map tasks. The map outputs are grouped by key. The reduce tasks emit the final pairs.
S-M-S-R: Split, Map, Shuffle, Reduce
Split the input · Map each chunk · Shuffle and sort by key · Reduce each group. The order never changes.
The slides draw the data change at each step. Input splits hold records. Map turns each record into intermediate key-value pairs. Grouping collects all values of one key into (k, [v, v, ...]). Reduce turns each group into output pairs.
Map phase and partitioningSearch on YouTube
The map task reads its assigned input split from HDFS. It parses the split into records as key-value pairs. It applies the user map function to each record. Then it informs the JobTracker of completion.
An input split is not an HDFS block. A split is the MapReduce work unit. Splits often match blocks (Hadoop and HDFS explains blocks). The count rule is strict:
One map task handles each input split. Each map task sends each output key to one partition. is the number of reduce tasks. The number of partitions equals the number of reduce tasks. All pairs of one key land in the same partition.
The partition phase rules from the slides:
- Each map task decides which reduce task receives each output.
- The destination partition is the same for any one key.
- The number of partitions equals the number of reduce tasks.
Worked example (extra). Assign 4 keys to 3 reduce tasks. Use a toy hash: sum the letter positions (a is 1, b is 2, and so on). The sums are Bear 26, Car 22, Deer 32, River 72.
Check. . . . .
Bear and Deer share partition 2, so one reduce task handles both keys. The shuffle step sorts them (Bear before Deer) inside that partition. Car goes to partition 1. River goes to partition 0.
Shuffle, sort and reduceSearch on YouTube
The shuffle phase fetches the map outputs for one partition from all map tasks. The sort phase merge-sorts them into a single run per key. The reduce phase applies the user reduce function to each group and emits the output.
The job execution diagram adds three more pieces. A RecordReader parses the split into records before the map task runs. A Combiner pre-aggregates map output on the map task node before the network shuffle. The output lands on HDFS through an OutputFormat.
Worked example. One split holds "Car Car River". Without a combiner the map task sends 3 pairs across the network: (Car, 1), (Car, 1), (River, 1).
With a combiner. The map task pre-sums Car locally and sends only 2 pairs: (Car, 2), (River, 1).
Effect on the Deer example below. Its 3 splits emit pairs. Only the middle split has a repeated word. The combiner shrinks it from 3 pairs to 2. The network carries pairs instead of 9. The final sums stay the same.
Word count: sentence splitsSearch on YouTube
The slide input is one paragraph: "Big data comes in various formats. This data can be stored in multiple data servers". It is split into two chunks. Each chunk gets one map task.
- Split 1: "Big data comes in various formats".
- Split 2: "This data can be stored in multiple data servers".
Map emits (word, 1) for each token. Split 1 emits 6 pairs. Split 2 emits 9 pairs (data appears twice).
Worked example. Trace the full pipeline. Split 1 emits (Big, 1) (data, 1) (comes, 1) (in, 1) (various, 1) (formats, 1). Split 2 emits (This, 1) (data, 1) (can, 1) (be, 1) (stored, 1) (in, 1) (multiple, 1) (data, 1) (servers, 1).
Shuffle groups and reduce sums. Count the map emissions: data appears 3 times (once in split 1, twice in split 2). in appears twice. be appears once. Every other word appears once.
| Key | Shuffle group | Reduce output |
|---|---|---|
| be | (1) | 1 |
| Big | (1) | 1 |
| can | (1) | 1 |
| comes | (1) | 1 |
| data | (1, 1, 1) | 3 |
| formats | (1) | 1 |
| in | (1, 1) | 2 |
| multiple | (1) | 1 |
| servers | (1) | 1 |
| stored | (1) | 1 |
| This | (1) | 1 |
| various | (1) | 1 |
Check. tokens. The two splits hold tokens. The sums match.
The slide shuffle column lists be twice and data, (1, 1). The two map outputs emit be only once and data three times. Use one be, (1) group and data, (1, 1, 1) with reduce output 3.
Word count: Deer Bear RiverSearch on YouTube
The slide input has three lines. Each line is one split, so the job runs 3 map tasks.
- Split 1: "Deer Bear River".
- Split 2: "Car Car River".
- Split 3: "Deer Car Bear".
Worked example. Map each split with (word, 1) per token.
| Split | Map output |
|---|---|
| Deer Bear River | (Deer, 1) (Bear, 1) (River, 1) |
| Car Car River | (Car, 1) (Car, 1) (River, 1) |
| Deer Car Bear | (Deer, 1) (Car, 1) (Bear, 1) |
Shuffle groups.
| Key | Values | Reduce output |
|---|---|---|
| Bear | (1, 1) | 2 |
| Car | (1, 1, 1) | 3 |
| Deer | (1, 1) | 2 |
| River | (1, 1) | 2 |
Final result. Bear 2, Car 3, Deer 2, River 2. This matches the slide exactly. The total equals the 9 map emissions.
The Endeavour paragraphSearch on YouTube
The slides trace a longer document about the space shuttle Endeavour crew. The slide shows only the start of the trace. The map function emits (word, 1) per token: (The, 1) (crew, 1) (of, 1) (the, 1) (space, 1) (shuttle, 1) (Endeavor, 1) (recently, 1). The reduce function collects all values of each key. The slide shows five outputs: (crew, 2) (space, 1) (the, 3) (shuttle, 1) (recently, 1).
Hadoop v1: JobTracker and TaskTrackerSearch on YouTube
Hadoop v1 runs two daemons for MapReduce. The JobTracker is the master daemon. It runs each MapReduce job. It links Hadoop and the application. It creates the execution plan: which task runs on which node. It monitors all running tasks. When a task fails, it reschedules the task on another node after fixed retries. One cluster runs exactly one JobTracker.
The TaskTracker runs the assigned tasks on its node. It sends heartbeat messages to the JobTracker. When the JobTracker misses heartbeats from a TaskTracker, it marks that node as failed. It resubmits the task to another available node.
The master runs the JobTracker and the NameNode; each slave runs a TaskTracker and a DataNode. A map task (also called mapper) runs a map function. A reduce task (also called reducer) runs a reduce function.
Do not mix the two trackers. The JobTracker plans and monitors whole jobs. The TaskTracker runs single tasks and heartbeats the JobTracker.
YARNSearch on YouTube
YARN is Yet Another Resource Negotiator. It arrived in Hadoop 2.0. It is the middle layer between HDFS and MapReduce. It manages cluster resources: memory, network bandwidth, disk IO and CPU. The two basic resource types are memory and virtual cores (vcores).
YARN runs many engines, not only MapReduce:
| Model | Engine |
|---|---|
| Batch | MapReduce |
| Interactive | Tez |
| Online | HBase |
| Streaming | Storm |
| Others | Giraph |
YARN daemonsSearch on YouTube
The ResourceManager is the global job manager. It has two components. The Scheduler allocates resources across all applications. It never monitors job status. The Applications Manager accepts job submissions. It restarts ApplicationMasters on failure.
A NodeManager is a per-node agent. It launches each application container. It monitors the resource use of every container. It tracks the jobs running on its node.
An ApplicationMaster is the per-application manager. It negotiates resource containers with the Scheduler. It tracks container status and job progress. It restarts failed tasks. A container is the resource unit: memory, CPU, disk and network for one task.
The slide legend names four message types: MapReduce status (solid), job submission (dashed), node status (dash-dot) and resource request (dotted).
Execution sequenceSearch on YouTube
- Submit the application from the client.
- Ask the ResourceManager for one container to start the ApplicationMaster.
- Register the ApplicationMaster with the ResourceManager at boot.
- Negotiate further containers with the ResourceManager.
- Contact the NodeManager to launch each granted container.
- Run the application code inside the container and report the status.
- Poll the ApplicationMaster or the ResourceManager for progress.
- Unregister the ApplicationMaster and shut it down. Free its container.
| Hadoop v1 | YARN |
|---|---|
| JobTracker plans tasks and monitors all jobs | Scheduler allocates resources; Applications Manager accepts jobs |
| TaskTracker runs tasks and heartbeats | NodeManager launches containers and monitors use |
| No per-job manager (the JobTracker does it) | ApplicationMaster manages one application |
| MapReduce only | MapReduce, Tez, HBase, Storm, Giraph |
Splitting the JobTracker fixes its bottleneck. No single daemon plans every task and tracks every job. New engines reuse the same resource layer.
Quick self-testSearch on YouTube
1. A job has 4 input splits. How many map tasks run, and why?
4 map tasks. One map task handles each input split, so the task count equals the split count.
2. Letter sums are Bear 26, Car 22, Deer 32, River 72. With 3 reduce tasks, which partition holds each key?
Bear: . Car: . Deer: . River: . Bear and Deer share partition 2.
3. A split holds "Car Car River". How many pairs cross the network with and without a combiner?
Without: 3 pairs, (Car, 1) twice plus (River, 1). With: 2 pairs, (Car, 2) plus (River, 1). The final sums are equal.
4. Map outputs are (a, 1) (b, 1) (a, 1). State the shuffle groups and the reduce outputs.
Groups: a with (1, 1), b with (1). Reduce: (a, 2), (b, 1).
5. Differentiate the JobTracker and the TaskTracker.
The JobTracker plans jobs, monitors tasks and reschedules failures. The TaskTracker runs single tasks and heartbeats the JobTracker.
6. Why must the number of partitions equal the number of reduce tasks?
Each partition feeds exactly one reduce task. One key must land whole in one partition, so the counts match by construction.
7. Differentiate the Scheduler and the Applications Manager.
The Scheduler allocates resources across applications and never monitors status. The Applications Manager accepts submissions and restarts failed ApplicationMasters.
8. Why did YARN split the JobTracker into two daemons?
One JobTracker that plans every task and tracks every job becomes a bottleneck. YARN separates global scheduling (Scheduler) from per-application management (ApplicationMaster), and so lets Tez, Storm and other engines share the cluster.
9. A NodeManager stops heartbeating and an ApplicationMaster crashes. Who restarts what?
The ResourceManager stops sending work to the silent node. The Applications Manager restarts the crashed ApplicationMaster. The ApplicationMaster restarts its own failed tasks in fresh containers.
10. In the sentence-split example, what are the shuffle group and reduce output for data?
Group data, (1, 1, 1) and output 3. The word occurs once in split 1 and twice in split 2.
- MapReduce applies one function to many items in parallel: map transforms, reduce collates. Dean and Ghemawat published it in 2004.
- Phases in order: split, map, shuffle and sort, reduce (S-M-S-R).
- One map task runs per input split. An input split is the work unit, not an HDFS block.
- Partition rule:
partition = hash(key) mod R, one key in one partition, number of partitions equals number of reduce tasks. - Shuffle fetches and merge-sorts map outputs by key. Reduce sums each
(k, [v, v, ...])group. - A combiner pre-aggregates on the map task node and cuts network pairs (9 to 8 in the Deer example).
- Slide traces: sentence splits give
data3 andin2; Deer Bear River gives Bear 2, Car 3, Deer 2, River 2. - Hadoop v1: one JobTracker plans and monitors; TaskTrackers run tasks and heartbeat.
- YARN (Hadoop 2.0) sits between HDFS and MapReduce. ResourceManager holds the Scheduler and the Applications Manager. NodeManagers launch containers. Each ApplicationMaster manages one job through 8 steps: submit, container, register, negotiate, launch, run, report, release.