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
- Architecture
- Job, task, and attempt
- When a task fails
- The job history
- Why MapReduce
- In the TDP editions
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.
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.
In the Glossary: file01 · file02 · Map 1 · Map 2 · InputFormat · RecordReader
- 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.
- 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.
- 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.
- 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.
- 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.
- 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>.
- 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.
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.
What the figure shows:
- The job client submits the job to the YARN ResourceManager.
- The YARN ResourceManager starts the MRAppMaster, the job's ApplicationMaster.
- 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.
- The JobHistory Server stores the history files of completed jobs.
- The input on HDFS: the InputFormat splits the input into InputSplits, one per map task.
- 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.
- The ShuffleHandler, an auxiliary service of each NodeManager, delivers the map task outputs to the reduce task, over HTTP.
- The reduce task fetches its partition from each map, groups by key and reduces: shuffle, sort and reduce.
- 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 Glossaryattempt— 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.
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 that, for a configured time, neither reads input, writes output, nor updates its status is terminated.
With speculative execution, several instances of some tasks can run in parallel. Only the output of the successful attempt goes to the result.
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 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
| Edition | Version | Documentation |
|---|---|---|
| TDP Datacenter | 3.3.6 | TDP Datacenter components |
In TDP Datacenter
In TDP Datacenter, MapReduce is the MapReduce2 service of the stack, installed and managed by Ambari.
HISTORYSERVER (master, one instance) and MAPREDUCE2_CLIENT (client, optional). The HISTORYSERVER requires HDFS_CLIENT (HDFS) and TEZ_CLIENT (Tez). Component category.
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.
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.
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.
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 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.
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.
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.
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.
Ambari requires in the cluster YARN.
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
TDP Datacenter
Data and processing on the same machines
HDFS, YARN, and the engines that run on it in the cluster managed by Ambari.
Fundamentals
Apache Hadoop
The project of which MapReduce is one of the modules, alongside HDFS and YARN.
Sources
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