Skip to main content

Batch processing, in parallel,
fault tolerant.

Apache MapReduce processes large volumes of data in parallel, as a YARN application: the application writes the map and reduce functions, and the framework schedules the tasks, monitors execution, and re-executes the ones that fail. Follow a word count from the input file to the result.

A MapReduce job splits the input into independent chunks, which the map tasks process in parallel; the framework sorts their outputs and hands them to the reduce tasks, which produce the result; a job can also have only map tasks.

Key-value pairs

Everything is a pair

The job input is a set of key-value pairs, and so is the output. Between the two, the types can change.

Two functions

The application writes map and reduce

The application indicates where the input and output are and supplies the map function and, in general, the reduce function. This and the other parameters make up the job configuration.

The rest is up to the framework

Schedule, sort, redo

The framework schedules the tasks, sorts the map outputs and delivers them to the reduces, monitors execution, and re-executes the tasks that fail.

On this page

See a job count words​

Two files, five different words: the pairs each phase produces. Press Play or step through. The underlined names lead to the Glossary.

HDFS · inputTwo files
file01Hello World Bye World
file02Hello Hadoop Goodbye Hadoop
Map tasksOne per InputSplit
Map 1<Hello, 1> <World, 1> <Bye, 1> <World, 1>
Combiner 1<Bye, 1> <Hello, 1> <World, 2>
Map 2<Hello, 1> <Hadoop, 1> <Goodbye, 1> <Hadoop, 1>
Combiner 2<Goodbye, 1> <Hadoop, 2> <Hello, 1>
Reduce taskShuffle, sort, and reduce
Shuffle and sortBye (1) · Goodbye (1) · Hadoop (2) · Hello (1, 1) · World (2)
ReduceSums the values of each key
HDFS · outputThe result
part-r-00000Bye 1 · Goodbye 1 · Hadoop 2 · Hello 2 · World 2
Step 1 of 7

1 The input becomes InputSplits

The job counts how many times each word appears in two HDFS files. The InputFormat splits the input into InputSplits, and the framework starts a map task for each one. In each task, the RecordReader reads the InputSplit and hands the map function one record at a time: with TextInputFormat, one line.

  1. The input becomes InputSplits. The job counts how many times each word appears in two HDFS files. The InputFormat splits the input into InputSplits, and the framework starts a map task for each one. In each task, the RecordReader reads the InputSplit and hands the map function one record at a time: with TextInputFormat, one line.
  2. Each map emits pairs. The map function splits the line into words and emits a <word, 1> pair for each one. The two map tasks run in parallel, each over its own InputSplit, and World, which appears twice in the first file, comes out twice from the first map.
  3. The combiner aggregates inside the map. The output of each map is sorted by key and goes through the combiner, which does a local aggregation. In this count, the combiner is the reducer itself. The two <World, 1> become <World, 2> still in the map task, and less data goes on to the reduce task.
  4. The partitioner chooses the reduce. The sorted outputs are divided into partitions, one per reduce task: the partitioner derives the partition from the key, usually with a hash function. In this job there is a single reduce task, and all keys go to the same partition.
  5. The shuffle fetches the partitions. The reduce task fetches its partition from the output of all the map tasks over HTTP: this is the shuffle, served by the ShuffleHandler of each NodeManager. At the same time, the sort groups the inputs by key, since different maps may have emitted the same one: Hello arrives from both map tasks.
  6. The reduce sums. The reduce function is called for each key, with its list of values, and sums the counts: Hello, with the list (1, 1), becomes <Hello, 2>.
  7. The result goes to HDFS. The OutputFormat's RecordWriter writes the output pairs to a file in the file system, part-r-00000. With TextOutputFormat, each line holds the word and the count.
Figure 1 - Word count over two HDFS files. Each map task handles one InputSplit; the combiner aggregates inside the map task itself; the shuffle carries the partitions to the reduce task, which sums them and writes the result.

The same job, seen from the infrastructure, from the file written in blocks on HDFS to the YARN containers, is in Apache Hadoop.

Architecture​

One MRAppMaster per job, map tasks close to the data, and the shuffle between the map and reduce tasks.

MapReduce architecture: job, MRAppMaster, map tasks, shuffle and reduce taskThe job client submits the job to the YARN ResourceManager, which starts the MRAppMaster. The MRAppMaster runs and tracks the map and reduce tasks and records the job history, which the JobHistory Server stores. The InputFormat splits the HDFS input into InputSplits, one per map task. In each map task, the RecordReader hands the records to the map function; the output is sorted and written to disk in the spill, aggregated by the combiner and divided into partitions by the partitioner. The ShuffleHandler on each NodeManager delivers the partitions to the reduce task, which performs the shuffle, the sort and the reduce, and the OutputFormat writes the result to HDFS.Job clientSubmits the jobResourceManagerIn YARN: starts the MRAppMasterMRAppMasterThe job's ApplicationMasterRuns and tracks the tasksJobHistory ServerHistory of completed jobsHDFS · inputInputSplit 1InputFormatSplits the inputInputSplit 2Map task 1RecordReaderMap functionSorted spillCombinerPartitionerOutput per partitionMap task 2RecordReaderMap functionSorted spillCombinerPartitionerOutput per partitionShuffleHandlerOn each NodeManagerReduce taskShuffleSortReduceFetches its partition from each map,groups by key and reducesHDFS · outputOutputFormat · RecordWriterFinal pairs in part-r-00000Job submissionTask executionInput and outputShuffle over HTTPJob history
Figure 2 - MapReduce architecture: the job client, the YARN ResourceManager, the MRAppMaster, and the JobHistory Server. The InputFormat and the InputSplits on HDFS, the map tasks, the ShuffleHandler, the reduce task, and the output on HDFS. The underlined terms lead to the Glossary.

What the figure shows:

  1. The job client submits the job to the YARN ResourceManager.
  2. The YARN ResourceManager starts the MRAppMaster, the job's ApplicationMaster.
  3. The MRAppMaster is the job's ApplicationMaster: it runs and tracks the map and reduce tasks, each as a child process, in a separate JVM, and records the job history.
  4. The JobHistory Server stores the history files of completed jobs.
  5. The input on HDFS: the InputFormat splits the input into InputSplits, one per map task.
  6. The map tasks: in each one, the RecordReader delivers the records to the map function. The emitted records go to an in-memory buffer. When it exceeds a threshold, the contents are sorted and written to disk, in the spill, while the task continues, and at the end the segments are merged into a single file. The output is aggregated by the combiner and split into partitions by the partitioner.
  7. The ShuffleHandler, an auxiliary service of each NodeManager, delivers the map task outputs to the reduce task, over HTTP.
  8. The reduce task fetches its partition from each map, groups by key and reduces: shuffle, sort and reduce.
  9. The output on HDFS: the OutputFormat, with the RecordWriter, writes the result, the final pairs.

Job, task, and attempt​

A job has tasks, and each task has attempts: the identifier of an attempt carries all three. Click each part.

Click each part of the identifier.

Prefix

Marks the identifier of a task attempt: one specific execution of a map or reduce task. A task can have several attempts, when one fails or when there are speculative copies.

See in the Glossary
attempt — Prefix
Marks the identifier of a task attempt: one specific execution of a map or reduce task. A task can have several attempts, when one fails or when there are speculative copies.
200707121733 — Start
The first part of the job identifier, which ties it to what coordinates it. On a cluster, it is a start time. In a local run, it is the word local and a random number.
0003 — Job
The job number: here, the third. Together with the previous part, it forms the job identifier, job_200707121733_0003.
m — Type
m for a map task, r for a reduce task.
000005 — Task
The task number in the job: here, the fifth map task. Together with the previous parts, it forms the task identifier, task_200707121733_0003_m_000005.
0 — Attempt
Which execution of the task this is: here, attempt zero.

Two attempts of the same task can run at the same time, as with speculative copies.

For this reason, each attempt writes its output to its own temporary subdirectory, inside the job's output directory: when the attempt finishes successfully, its files are promoted to the output. Those of unsuccessful attempts are discarded, without the application having to handle it.

When a task fails​

The framework re-executes what fails: what happens with each piece.

A task fails

The framework runs the task again, in a new attempt, up to the maximum number of attempts per map or reduce task. After that, it gives up on the task.

A task stops signaling

A task that, for a configured time, neither reads input, writes output, nor updates its status is terminated.

Speculative copies

With speculative execution, several instances of some tasks can run in parallel. Only the output of the successful attempt goes to the result.

A record brings the task down

When a map task always fails on the same stretch of the input, the bad-record skipping mode, when enabled, kicks in after a few failures: the following attempts skip the stretch, and the skipped records go to HDFS for later analysis.

The MRAppMaster fails

The YARN ApplicationsManager restarts the MRAppMaster container, in a new attempt of the job as an application, up to the MapReduce maximum number of attempts, which does not exceed the ResourceManager's global limit.

The job history​

Each job, its tasks, and the attempts of each one, after it finishes.

The JobHistory Server stores and presents the history of completed jobs, in three steps.

Who writes

The job writes its history

During execution, the job writes the history files to an intermediate directory.

Who stores

The server manages

The JobHistory Server manages these files in a directory of its own and can save its state to recover it when it restarts.

Who queries

Web interface and REST API

For each completed job: the job attempts, the tasks, the attempts of each task, the counters, and the configuration.

The MRAppMaster can also send job data to the YARN Timeline Service.

Why MapReduce​

Parallelism

The input in independent chunks

The map tasks process independent chunks of the input in a completely parallel manner, on clusters of commodity hardware.

Local data

The computation goes to the data

Tasks are scheduled on the machines where the data already is, which gives the cluster very high aggregate bandwidth. See data locality.

Fault tolerance

What fails runs again

The framework monitors the tasks and re-executes the ones that fail, with new attempts.

Less data on the network

Aggregation before the shuffle

The combiner aggregates the output of each map still in the map task and reduces what goes to the reduce tasks.

Other languages

Map and reduce in any executable

With Hadoop Streaming, any executable, such as a shell utility, can be the mapper or the reducer. With Hadoop Pipes, applications are written in C++.

Chained jobs

One's output, the other's input

The output of one job stays in the distributed file system and can be the input of the next job. A job can also have only map tasks, which write directly to the file system.

In the TDP editions​

Available in the TDP editions
EditionVersionDocumentation
TDP Datacenter3.3.6TDP Datacenter components

In TDP Datacenter​

In TDP Datacenter, MapReduce is the MapReduce2 service of the stack, installed and managed by Ambari.

Service components

HISTORYSERVER (master, one instance) and MAPREDUCE2_CLIENT (client, optional). The HISTORYSERVER requires HDFS_CLIENT (HDFS) and TEZ_CLIENT (Tez). Component category.

Where jobs run

The job execution framework is configurable in mapreduce.framework.name, among local, classic, and yarn. Jobs are submitted to the queue in mapreduce.job.queuename, one of the YARN queues, and the staging directory for submission is in yarn.app.mapreduce.am.staging-dir.

Memory of the tasks and the MRAppMaster

The memory requested from the Scheduler for each task is in mapreduce.map.memory.mb and mapreduce.reduce.memory.mb, and the JVM options of each task, in mapreduce.map.java.opts and mapreduce.reduce.java.opts. For the MRAppMaster, the equivalents are yarn.app.mapreduce.am.resource.mb and yarn.app.mapreduce.am.command-opts.

Sorting and spill

The sort buffer of the map tasks and the threshold that triggers the spill are configurable in mapreduce.task.io.sort.mb and mapreduce.map.sort.spill.percent, and the number of segments merged at once, in mapreduce.task.io.sort.factor.

Shuffle

The ShuffleHandler uses the port in mapreduce.shuffle.port and is registered on the NodeManagers as the auxiliary service mapreduce_shuffle, in yarn.nodemanager.aux-services and yarn.nodemanager.aux-services.mapreduce_shuffle.class, in the YARN configuration. The parallel transfers of each reduce task, the memory used for the map outputs, and the moment the reduce tasks start are in mapreduce.reduce.shuffle.parallelcopies, mapreduce.reduce.shuffle.input.buffer.percent, mapreduce.reduce.shuffle.merge.percent, mapreduce.reduce.input.buffer.percent, and mapreduce.job.reduce.slowstart.completedmaps.

Compression

Compression of the map outputs, before they go over the network, can be enabled in mapreduce.map.output.compress. That of the job output, in mapreduce.output.fileoutputformat.compress, with the type in mapreduce.output.fileoutputformat.compress.type.

Attempts and speculative execution

The maximum number of MRAppMaster attempts is in mapreduce.am.max-attempts, and the time without a signal that terminates a task, in mapreduce.task.timeout. Speculative execution can be enabled in mapreduce.map.speculative and mapreduce.reduce.speculative.

History

The JobHistory Server addresses are in mapreduce.jobhistory.address and mapreduce.jobhistory.webapp.address, and the history directories, in mapreduce.jobhistory.intermediate-done-dir and mapreduce.jobhistory.done-dir. Recovery of the server state can be enabled in mapreduce.jobhistory.recovery.enable, the HTTP endpoint of the web interface is configurable in mapreduce.jobhistory.http.policy, and sending data to the YARN Timeline Service, in mapreduce.job.emit-timeline-data.

Authorization

MapReduce ACLs can be enabled in mapreduce.cluster.acls.enabled, with the administrators in mapreduce.cluster.administrators, the ACLs of each job in mapreduce.job.acl-view-job and mapreduce.job.acl-modify-job, and the JobHistory Server administrators in mapreduce.jobhistory.admin.acl. See Security in TDP Datacenter.

Dependencies

Ambari requires in the cluster YARN.

Required by

These services require this one in the cluster: YARN. The FLINK_CLIENT (Flink) requires MAPREDUCE2_CLIENT. The FLINK_JOBHISTORYSERVER (Flink) requires MAPREDUCE2_CLIENT. The FLINK_RESTSERVER (Flink) requires MAPREDUCE2_CLIENT. The HIVE_SERVER (Hive) requires MAPREDUCE2_CLIENT. The HIVE_SERVER_INTERACTIVE (Hive) requires MAPREDUCE2_CLIENT. The SPARK3_CLIENT (Spark) requires MAPREDUCE2_CLIENT. The SPARK3_JOBHISTORYSERVER (Spark) requires MAPREDUCE2_CLIENT. The SPARK3_THRIFTSERVER (Spark) requires MAPREDUCE2_CLIENT. The SQOOP (Sqoop) requires MAPREDUCE2_CLIENT. The TEZ_CLIENT (Tez) requires MAPREDUCE2_CLIENT.

Next steps​

Sources​

Apache Hadoop

Apache Hadoop MapReduce - MapReduce Tutorial

Apache Hadoop MapReduce - mapred-default.xml

Apache Hadoop MapReduce - History Server REST API

Apache Hadoop MapReduce - Pluggable Shuffle and Pluggable Sort

Apache Hadoop MapReduce - Encrypted Shuffle

Apache Hadoop API - TaskAttemptID