Chapter 5
Chapter 5
Hadoop MapReduce
This MapReduce chapter provides you the complete guide about each and everything in Hadoop
MapReduce.
You will explore what Hadoop MapReduce is, How the MapReduce framework works. The chapter
also covers MapReduce DataFlow, Different phases in MapReduce, Mapper, Reducer, Partitioner,
Cominer, Shuffling, Sorting, Data Locality, and many more.
The above figure depicts the schematic view of the traditional enterprise systems. The traditional
systems normally have a centralized server for storing and processing data. This model is not suitable
for processing huge amounts of scalable data.
Also, this model could not be accommodated by the standard database servers. Additionally, the
centralized system creates too much bottleneck while processing multiple files simultaneously.
By using the MapReduce algorithm, Google solved this bottleneck issue. The MapReduce framework
divides the task into small parts and assigns tasks to many computers.
Later on, the results are collected at a commonplace and are then integrated to form the result
dataset.
MapReduce Framework
MapReduce is the processing layer in Hadoop. It is a software framework designed for processing
huge volumes of data in parallel by dividing the task into the set of independent tasks.
We just need to put the business logic in the way the MapReduce works, and the framework will take
care of the rest things. The MapReduce framework works by dividing the job into small tasks and
assigns these tasks to the slaves.
The MapReduce programs are written in a particular style influenced by the functional programming
constructs, specifical idioms for processing the lists of data.
In MapReduce, the inputs are in the form of a list and the output from the framework is also in the
form of a list. MapReduce is the heart of Hadoop. The efficiency and powerfulness of Hadoop are
due to the MapReduce framework parallel processing.
MapReduce Partitioner
The Hadoop MapReduce Partitioner partitions the keyspace. Partitioning keyspace in MapReduce
specifies that all the values of each key were grouped together, and it ensures that all the values of
the single key must go to the same Reducer.
This partitioning allows even distribution of mapper’s output over Reducer by assuring that the right
key goes to the right Reducer.
The default MapReducer partitioner is the Hash Partitioner, which partitions the keyspaces on the
basis of the hash value.
MapReduce Combiner
The MapReduce Combiner is also known as the “Semi-Reducer.” It plays a major role in reducing
network congestion. The MapReduce framework provides the functionality to define the Combiner,
which combines the intermediate output from Mappers before passing them to Reducer.
The aggregation of Mapper outputs before passing to Reducer helps the framework shuffle small
amounts of data, leading to low network congestion.
The main function of the Combiner is to summarize the output of the Mappers with the same key and
pass it to the Reducer. The Combiner class is used between the Mapper class and the Reducer class.
Data Locality in MapReduce
Data locality refers to “Moving computation closer to the data rather than moving data to the
computation.” It is much more efficient if the computation requested by the application is executed
on the machine where the data requested resides. This is very true in the case where the data size is
huge. It is because it minimizes the network congestion and increases the overall throughput of the
system.
The only assumption behind this is that it is better to move computation closer to the machine where
data is present instead of moving data to the machine where the application is running.
Apache Hadoop works on a huge volume of data, so it is not efficient to move such huge data over the
network. Hence the framework came up with the most innovative principle that is data locality, which
moves computation logic to data instead of moving data to computation algorithms. This is called data
locality.
Usage of MapReduce
1. Log analysis: MapReduce is used basically for analyzing log files. The framework breaks the big log
files into the split and a mapper search for the different web pages that were accessed. Every
time when a web page is found in the log, then a key, value pair is passed to the reducer where
the key is the webpage, and value is “1”. After emitting a key, value pair to Reducer, the Reducers
aggregate the number of for certain webpages. The final result will be the total number of hits for
every webpage.
2. Full-text indexing: MapReduce is also used for performing full-text indexing. The mapper in
MapReduce will map every phrase or word in one document to the document. The Reducer will
write these mappings to an index.
3. Google uses MapReduce for calculating their Pagerank.
4. Reverse Web-Link Graph: MapReduce is also used in Reverse Web-Link GRaph. The Map function
outputs the URL target and source, taking input from the webpage (source). The reduce function
then concatenates the list of all the source URLs which are associated with the given target URL
and it returns the target and list of sources.
5. Word count in a document: MapReduce framework can be used for counting the number of
times the word appears in a document.
Phases of MapReduce
MapReduce is one of the core components of Hadoop that processes large datasets in parallel by
dividing the task into a set of independent tasks. In this MapReduce Tutorial, you will study the
working of Hadoop MapReduce in detail.
It covers all the phases of MapReduce job execution like Input Files, InputFormat, InputSplits,
RecordReader, Mapper, Combiner, Partitioner, Shuffling, and Sorting, Reducer, RecordWriter, and
OutputFormat in detail.
The Hadoop MapReduce framework consists of:
single master ResourceManager
One worker NodeManager per cluster-node
MRAppMaster per application
The applications specify the input and the output locations and supply the map and reduce functions
through the implementations of appropriate interfaces or abstract classes.
These and the other job parameters comprise job configuration.
Then, the Hadoop job client submits the job, that is, jar or executable, and configuration to the
ResourceManager. ResourceManager then assumes the responsibility of distributing the contribution
of software to the workers, scheduling tasks and monitoring them, and providing status and
diagnostic information to the job-client.
Though the Hadoop framework is implemented in Java, it is not needed to write MapReduce
applications in Java.
1. InputFiles
The data that is to be processed by the MapReduce task is stored in input files. These input files are
stored in the Hadoop Distributed File System. The file format is arbitrary, while the line-based log files
and the binary format can also be used.
2. InputFormat
It specifies the input-specification for the job. InputFormat validates the MapReduce job input-
specification and splits-up the input files into logical InputSplit instances. Each InputSplit is then
assigned to the individual Mapper. TextInputFormat is the default InputFormat.
Input files store the data for MapReduce job. Input files reside in HDFS. Although these files format is
arbitrary, we can also use line-based log files and binary format. Hence, In MapReduce, InputFormat
class is one of the fundamental classes which provides below functionality:
InputFormat selects the files or other objects for input.
It also defines the Data splits. It defines both the size of individual Map tasks and its potential
execution server.
Hadoop InputFormat defines the RecordReader. It is also responsible for reading actual records from
the input files.
C. KeyValueTextInputFormat
It is similar to TextInputFormat. This InputFormat also treats each line of input as a separate record.
While the difference is that TextInputFormat treats entire line as the value, but the
KeyValueTextInputFormat breaks the line itself into key and value by a tab character (‘/t’). Hence,
Key – Everything up to the tab character.
Value – It is the remaining part of the line after tab character.
D. SequenceFileInputFormat
It is an InputFormat which reads sequence files. Sequence files are binary files. These files also store
sequences of binary key-value pairs. These are block-compressed and provide direct serialization and
deserialization of several arbitrary data. Hence,
Key & Value both are user-defined.
E. SequenceFileAsTextInputFormat
It is the variant of SequenceFileInputFormat. This format converts the sequence file key values to Text
objects. So, it performs conversion by calling ‘tostring()’ on the keys and values. Hence,
SequenceFileAsTextInputFormat makes sequence files suitable input for streaming.
F. SequenceFileAsBinaryInputFormat
By using SequenceFileInputFormat we can extract the sequence file’s keys and values as an opaque
binary object.
G. NlineInputFormat
It is another form of TextInputFormat where the keys are byte offset of the line. And values are
contents of the line. So, each mapper receives a variable number of lines of input with
TextInputFormat and KeyValueTextInputFormat.
The number depends on the size of the split. Also, depends on the length of the lines. So, if want our
mapper to receive a fixed number of lines of input, then we use NLineInputFormat.
N- It is the number of lines of input that each mapper receives.
By default (N=1), each mapper receives exactly one line of input.
Suppose N=2, then each split contains two lines. So, one mapper receives the first two Key-Value pairs.
Another mapper receives the second two key-value pairs.
H. DBInputFormat
This InputFormat reads data from a relational database, using JDBC. It also loads small datasets,
perhaps for joining with large datasets from HDFS using MultipleInputs. Hence,
Key – LongWritables
Value – DBWritables.
3. InputSplit
It represents the data for processing by the individual Mapper. InputSplit typically presents the byte-
oriented view of the input. It is the RecordReader responsibility to process and present the record-
oriented view. The default InputSplit is the FileSplit.
InputSplit is the logical representation of data in Hadoop MapReduce. It represents the data which
individual mapper processes. Thus the number of map tasks is equal to the number of InputSplits.
Framework divides split into records, which mapper processes.
MapReduce InputSplit length has measured in bytes. Every InputSplit has storage locations (hostname
strings). The MapReduce system places map tasks as close to the split’s data as possible by using
storage locations.
Framework processes Map tasks in the order of the size of the splits so that the largest one gets
processed first (greedy approximation algorithm). This minimizes the job run time.
The main thing to focus is that Inputsplit does not contain the input data; it is just a reference to the
data.
4. RecordReader
RecordReader reads the <key, value> pairs from the InputSplit. It converts a byte-oriented view of the
input and presents a record-oriented view to the Mapper implementations for processing.
It is responsible for processing record boundaries and presenting the Map tasks with keys and values.
The record reader breaks the data into the <key, value> pairs for input to the Mapper.
MapReduce is a simple model of data processing. Inputs and outputs for the map and reduce
functions are key-value pairs. Following is the general form of the map and reduce functions:
Hadoop RecordReader in MapReduce job execution uses the data within the boundaries that are
being created by the inputsplit. And it then creates Key-value pairs for the mapper. The “start” is the
byte position in the file.
At the Start, Hadoop RecordReader starts generating key/value pairs. The “end” is where RecorReader
stops reading records. In RecordReader, the data is loaded from its source.
Then the data are converted into key-value pairs suitable for reading by the Mapper. It communicates
with the inputsplit till the file reading is not completed.
How RecorReader works in Hadoop?
It is more than iterator over the records. The map task uses one record to generate key-value pair
which it passes to the map function. We can also see this by using the mapper’s run function given
below:
public void run(Context context ) throws IOException, InterruptedException{
setup(context);
while([Link]())
{
map([Link](),[Link](),context)
}
cleanup(context);
}
Although it is not mandatory for RecordReader to stay in between the boundaries created by the
inputsplit to generate key-value pairs it usually stays. Also, custom implementation can even read
more data outside of the inputsplit.
Then, after running setup(), the nextKeyValue() will repeat on the context. This populates the key and
value objects for the mapper. By way of context, framework retrieves key-value from record reader.
Then pass to the map() method to do its work.
Hence, input (key-value) to the map function processes as per the logic mentioned in the map code.
When the record gets to the end of the record, the nextKeyValue() method returns false.
Types of Hadoop RecordReader
A. LineRecordReader
It is the default RecordReader. TextInputFormat provides this RecordReader. It also treats each line of
the input file as the new value. Then the associated key is byte offset. It always skips the first line in
the split (or part of it), if it is not the first split. It always reads one line after the boundary of the split
in the end (if data is available, so it is not the last split).
B. SequenceFileRecordReader
This Hadoop RecorReader reads data specified by the header of a sequence file.
The maximum size of the single Record. By using below parameter we set maximum value.
[Link]("[Link]", Integer.MAX_VALUE);
5. Mapper
Mapper maps the input <key, value> pairs to a set of intermediate <key, value> pairs. It processes the
input records from the RecordReader and generates the new <key, value> pairs. The <key, value>
pairs generated by Mapper are different from the input <key, value> pairs.
The generated <key, value> pairs is the output of Mapper known as intermediate output. These
intermediate outputs of the Mappers are written to the local disk.
The Mappers output is not stored on the Hadoop Distributed File System because this is the
temporary data, and writing this data on HDFS will create unnecessary copies. The output of the
Mappers is then passed to the Combiner for further processing.
Mapper Class in MapReduce
Hadoop Mapper processes input record produced by the RecordReader and generates intermediate
key-value pairs. The intermediate output is completely different from the input pair.
The output of the mapper is the full collection of key-value pairs. Before writing the output for each
mapper task, partitioning of output take place on the basis of the key. Thus partitioning itemizes that
all the values for each key are grouped together.
Hadoop MapReduce generates one map task for each InputSplit. Hadoop MapReduce only
understands key-value pairs of data. So, before sending data to the mapper, Hadoop framework
should covert data into the key-value pair.
InputSplit converts the physical representation of the blocks into logical for the Mapper. For example,
to read the 100MB file, it will require 2 InputSplit. For each block, the framework creates one
InputSplit. Each InputSplit create one mapper.
MapReduce InputSplit not always depends on the number of data blocks. We can change the number
of a split by setting [Link] property during job execution.
MapReduce RecordReader is responsible for reading/converting data into key-value pairs till the end
of the file. RecordReader assigns Byte offset to each line present in the file.
Then Mapper receives this key pair. Mapper produces the intermediate output (key-value pairs which
are understandable to reduce).
The primary job of Combiner a “Mini-Reducer is to process the output data from the Mapper, before
passing it to Reducer. It runs after the mapper and before the Reducer. Its usage is optional.
How does Combiner work in Hadoop?
Now let us learn how things change when we use the combiner in MapReduce?
As we see in above diagram no combiner is there. Input is split into two mappers. The framework
generates 9 keys from the mappers.
So, now we have (9 key/value) intermediate data. Further mapper sends this key-value directly to the
reducer. While sending data to the reducer, it consumes some network bandwidth. It takes more time
to transfer data to reducer if the size of data is big.
Now from the above diagram, if we use a combiner in between mapper and reducer. Then combiner
will shuffle 9 key/value before sending it to the reducer. And then generates 4 key/value pair as an
output.
Now, Reducer needs to process only 4 key/value pair data which are generated from 2 combiners.
Therefore reducer gets executed only 4 times to produce the final output. Thus, this increases the
overall performance.
Advantages of Combiner in MapReduce
Use of combiner reduces the time taken for data transfer between mapper and reducer.
Combiner improves the overall performance of the reducer.
It decreases the amount of data that reducer has to process.
Disadvantages of Combiner in MapReduce
7. Partitioner
When we are working on the MapReduce program with more than one Reducer then only the
Partitioner comes into the picture. For only one reducer, we do not use Partitioner.
It partitions the keyspace. It controls the partitioning of keys of the Mapper intermediate outputs.
Partitioner takes the output from the Combiner and performs partitioning. Key is for deriving the
partition typically through the hash function. The number of partitions is similar to the number of
reduce tasks. HashPartitioner is the default Partitioner.
On the basis of key value, framework partitions, each mapper output. Records as having the same key
value go into the same partition (within each mapper). Then each partition is sent to a reducer.
Partition class decides which partition a given (key, value) pair will go. Partition phase in MapReduce
data flow takes place after map phase and before reduce phase.
Shuffling is the process by which it transfers mappers intermediate output to the reducer. Reducer
gets 1 or more keys and associated values on the basis of reducers.
The intermediated key – value generated by mapper is sorted automatically by key. In Sort phase
merging and sorting of map output takes place.
The process of transferring data from the mappers to reducers is shuffling. It is also the process by
which the system performs the sort. Then it transfers the map output to the reducer as input. This is
the reason shuffle phase is necessary for the reducers.
Otherwise, they would not have any input (or input from every mapper). Since shuffling can start even
before the map phase has finished. So this saves some time and completes the tasks in lesser time.
Sorting in MapReduce
MapReduce Framework automatically sort the keys generated by the mapper. Thus, before starting of
reducer, all intermediate key-value pairs get sorted by key and not by value. It does not sort values
passed to each reducer. They can be in any order.
Sorting in a MapReduce job helps reducer to easily distinguish when a new reduce task should start.
This saves time for the reducer. Reducer in MapReduce starts a new reduce task when the next key in
the sorted input data is different than the previous. Each reduce task takes key value pairs as input
and generates key-value pair as output.
The important thing to note is that shuffling and sorting in Hadoop MapReduce are will not take place
at all if you specify zero reducers (setNumReduceTasks(0)).
If reducer is zero, then the MapReduce job stops at the map phase. And the map phase does not
include any kind of sorting (even the map phase is faster).
If we want to sort reducer values, then we use a secondary sorting technique. This technique enables
us to sort the values (in ascending or descending order) passed to each reducer.
9. Reducer
Reducer then reduces the set of intermediate values who shares a key to the smaller set of values.
The output of reducer is the final output. This output is stored in the Hadoop Distributed File System.
Reducer in Hadoop MapReduce reduces a set of intermediate values which share a key to a smaller
set of values.
In MapReduce job execution flow, Reducer takes a set of an intermediate key-value pair produced by
the mapper as the input. Then, Reducer aggregate, filter and combine key-value pairs and this
requires a wide range of processing.
One-one mapping takes place between keys and reducers in MapReduce job execution. They run in
parallel since they are independent of one another. The user decides the number of reducers in
MapReduce.
Phases of Hadoop Reducer
Three phases of Reducer are as follows:
1. Shuffle Phase
This is the phase in which sorted output from the mapper is the input to the reducer. The framework
with the help of HTTP fetches the relevant partition of the output of all the mappers in this [Link]
phase
2. Sort Phase
This is the phase in which the input from different mappers is again sorted based on the similar keys in
different Mappers.
Both Shuffle and Sort occur concurrently.
3. Reduce Phase
This phase occurs after shuffle and sort. Reduce task aggregates the key-value pairs. With the
[Link]() property, the output of the reduce task is written to the FileSystem. Reducer
output is not sorted.
10. RecordWriter
RecordWriter writes the output (key, value pairs) of Reducer to an output file. It writes the
MapReduce job outputs to the FileSystem.
11. OutputFormat
The OutputFormat specifies the way in which these output key-value pairs are written to the output
files. It validates the output specification for a MapReduce job.
OutputFormat basically provides the RecordWriter implementation used for writing the output files of
the MapReduce job. The output files are stored in a FileSystem.
.
OutputFormat check the output specification for execution of the Map-Reduce job. It describes how
RecordWriter implementation is used to write output to output files.
Before we start with OutputFormat, let us first learn what is RecordWriter and what is the work of
RecordWriter in MapReduce?
As we know, Reducer takes Mappers intermediate output as input. Then it runs a reducer function on
them to generate output that is again zero or more key-value pairs.
So, RecordWriter in MapReduce job execution writes these output key-value pairs from the Reducer
phase to output files.
2. Hadoop OutputFormat
From above it is clear that RecordWriter takes output data from Reducer. Then it writes this data to
output files. OutputFormat determines the way these output key-value pairs are written in output
files by RecordWriter.
The OutputFormat and InputFormat functions are similar. OutputFormat instances are used to write
to files on the local disk or in HDFS. In MapReduce job execution on the basis of output specification;
Hadoop MapReduce job checks that the output directory does not already present.
OutputFormat in MapReduce job provides the RecordWriter implementation to be used to
write the output files of the job. Then the output files are stored in a FileSystem.
A. TextOutputFormat
The default OutputFormat is TextOutputFormat. It writes (key, value) pairs on individual lines of text
files. Its keys and values can be of any type. The reason behind is that TextOutputFormat turns them
to string by calling toString() on them.
It separates key-value pair by a tab character. By using
[Link] property we can also change it.
B. SequenceFileOutputFormat
This OutputFormat writes sequences files for its output. SequenceFileInputFormat is also intermediate
format use between MapReduce jobs. It serializes arbitrary data types to the file.
And the corresponding SequenceFileInputFormat will deserialize the file into the same types. It
presents the data to the next mapper in the same manner as it was emitted by the previous reducer.
Static methods also control the compression.
C. SequenceFileAsBinaryOutputFormat
It is another variant of SequenceFileInputFormat. It also writes keys and values to sequence file in
binary format.
D. MapFileOutputFormat
It is another form of FileOutputFormat. It also writes output as map files. The framework adds a key in
a MapFile in order. So we need to ensure that reducer emits keys in sorted order.
E. MultipleOutputs
This format allows writing data to files whose names are derived from the output keys and values.
F. LazyOutputFormat
In MapReduce job execution, FileOutputFormat sometimes create output files, even if they are empty.
LazyOutputFormat is also a wrapper OutputFormat.
G. DBOutputFormat
It is the OutputFormat for writing to relational databases and HBase. This format also sends the
reduce output to a SQL table. It also accepts key-value pairs. In this, the key has a type extending
DBwritable
How MapReduce Works
1. Input Splitting & Record Reading: The large input data is divided into smaller, fixed-size pieces
called "splits" (e.g., each line or a block of lines is a record). Each split is processed by a
separate Map task.
2. Map Phase: A user-defined Map function processes each input record (key-value pair, e.g.,
<byte\_offset, line\_content>) and generates intermediate key-value pairs.
o Action: Each mapper tokenizes its assigned text and outputs each word with a count of
one, as <word, 1>.
o Example Output (from T[0]): <"it", 1>, <"is", 1>, <"what", 1>, <"it", 1>, <"is", 1>
3. Shuffle and Sort Phase: The framework automatically groups all intermediate values with the
same key together. This data is sorted and distributed to the appropriate Reducer task.
o Action: All <"it", 1> pairs are sent to the same reducer, all <"is", 1> pairs to another,
and so on.
o Example Input to Reducers:
"a": [1]
"banana": [1]
"is": [1, 1, 1]
"it": [1, 1, 1]
"what": [1, 1]
4. Reduce Phase: A user-defined Reduce function aggregates the values for each unique key to
produce the final output.
"a": 1
"banana": 1
"is": 3
"it": 3
"what": 2
5. Final Output: The final key-value pairs are collected and written to an output file in the
distributed file system (e.g., HDFS).
Real-World Applications
Beyond word counting, MapReduce is used in various large-scale
applications:
Search Engine Indexing: Crawling web pages and organizing keywords and metadata into an
efficient, searchable index.
Log Analysis: Analyzing server logs to identify popular pages, peak traffic times, or system
errors.
Recommendation Systems: Processing user behavior (views, clicks, purchases) to suggest
products or media (e.g., Netflix, Amazon).
Fraud Detection: Analyzing millions of financial transactions to flag unusual spending patterns
or suspicious activity.
Genomic Data Analysis: Processing vast amounts of genetic sequence data to uncover
variations or mutations.