Module 1
Module 1
What is Mapreduce?
MapReduce is a programming paradigm that runs in the background of Hadoop
to provide scalability and easy data-processing solutions. This tutorial explains
the features of MapReduce and how it works to analyze Big Data.
What is Hadoop?
Hadoop is an open-source framework that allows to store and process big data in
a distributed environment across clusters of computers using simple
programming models. It is designed to scale up from single servers to thousands
of machines, each offering local computation and storage.
Why MapReduce?
Traditional Enterprise Systems normally have a centralized server to store
and process data. The following illustration depicts a schematic view of a
traditional enterprise system. Traditional model is certainly not suitable to
process huge volumes of scalable data and cannot be accommodated by
standard database servers. Moreover, the centralized system creates too
much of a bottleneck while processing multiple files simultaneously.
Google solved this bottleneck issue using an algorithm called MapReduce.
MapReduce divides a task into small parts and assigns them to many
computers. Later, the results are collected at one place and integrated to
form the result dataset.
The Map task takes a set of data and converts it into another set of data,
where individual elements are broken down into tuples (key-value pairs).
The Reduce task takes the output from the Map as an input and combines
those data tuples (key-value pairs) into a smaller set of tuples.
Let us now take a close look at each of the phases and try to understand
their significance.
Input Phase − Here we have a Record Reader that translates each
record in an input file and sends the parsed data to the mapper in the
form of key-value pairs.
Map − Map is a user-defined function, which takes a series of key-value
pairs and processes each one of them to generate zero or more key-value
pairs.
Intermediate Keys − They key-value pairs generated by the mapper are
known as intermediate keys.
Combiner − A combiner is a type of local Reducer that groups similar
data from the map phase into identifiable sets. It takes the intermediate
keys from the mapper as input and applies a user-defined code to
aggregate the values in a small scope of one mapper. It is not a part of
the main MapReduce algorithm; it is optional.
Shuffle and Sort − The Reducer task starts with the Shuffle and Sort
step. It downloads the grouped key-value pairs onto the local machine,
where the Reducer is running. The individual key-value pairs are sorted by
key into a larger data list. The data list groups the equivalent keys
together so that their values can be iterated easily in the Reducer task.
Reducer − The Reducer takes the grouped key-value paired data as input
and runs a Reducer function on each one of them. Here, the data can be
aggregated, filtered, and combined in a number of ways, and it requires a
wide range of processing. Once the execution is over, it gives zero or
more key-value pairs to the final step.
Output Phase − In the output phase, we have an output formatter that
translates the final key-value pairs from the Reducer function and writes
them onto a file using a record writer.
Let us try to understand the two tasks Map &f Reduce with the help of a
small diagram −
MapReduce-Example
Let us take a real-world example to comprehend the power of MapReduce.
Twitter receives around 500 million tweets per day, which is nearly 3000
tweets per second. The following illustration shows how Tweeter manages
its tweets with the help of MapReduce.
Tokenize − Tokenizes the tweets into maps of tokens and writes them as
key-value pairs.
Filter − Filters unwanted words from the maps of tokens and writes the
filtered maps as key-value pairs.
Count − Generates a token counter per word.
Aggregate Counters − Prepares an aggregate of similar counter values
into small manageable units.
The MapReduce algorithm contains two important tasks, namely Map and
Reduce.
Mapper class takes the input, tokenizes it, maps and sorts it. The output of
Mapper class is used as input by Reducer class, which in turn searches
matching pairs and reduces them.
Sorting
Searching
Indexing
TF-IDF
Sorting
Sorting is one of the basic MapReduce algorithms to process and analyze
data. MapReduce implements sorting algorithm to automatically sort the
output key-value pairs from the mapper by their keys.
Searching
Searching plays an important role in MapReduce algorithm. It helps in the
combiner phase (optional) and in the Reducer phase. Let us try to
understand how Searching works with the help of an example.
Example
The following example shows how MapReduce employs Searching
algorithm to find out the details of the employee who draws the highest
salary in a given employee dataset.
The Map phase processes each input file and provides the employee
data in key-value pairs (<k, v> : <emp name, salary>). See the following
illustration.
The combiner phase (searching technique) will accept the input from
the Map phase as a key-value pair with employee name and salary. Using
searching technique, the combiner will check all the employee salary to
find the highest salaried employee in each file. See the following snippet.
<k: employee name, v: salary>
Max= the salary of an first employee. Treated as max salary
if(v(second employee).salary > Max){
Max = v(salary);
}
else{
Continue checking;
}
Reducer phase − Form each file, you will find the highest salaried
employee. To avoid redundancy, check all the <k, v> pairs and eliminate
duplicate entries, if any. The same algorithm is used in between the four
<k, v> pairs, which are coming from four input files. The final output
should be as follows −
<gopal, 50000>
Indexing
Normally indexing is used to point to a particular data and its address. It
performs batch indexing on the input files for a particular Mapper.
"a": {2}
"banana": {2}
"is": {0, 1, 2}
"it": {0, 1, 2}
"what": {0, 1}
Here "a": {2} implies the term "a" appears in the T[2] file. Similarly, "is":
{0, 1, 2} implies the term "is" appears in the files T[0], T[1], and T[2].
TF-IDF
TF-IDF is a text processing algorithm which is short for Term Frequency −
Inverse Document Frequency. It is one of the common web analysis
algorithms. Here, the term 'frequency' refers to the number of times a
term appears in a document.
While computing TF, all the terms are considered equally important. That
means, TF counts the term frequency for normal words like is, a, what,
etc. Thus we need to know the frequent terms while scaling up the rare
ones, by computing the following −
Example
Consider a document containing 1000 words, wherein the
word hive appears 50 times. The TF for hive is then (50 / 1000) = 0.05.
Now, assume we have 10 million documents and the word hive appears
in 1000 of these. Then, the IDF is calculated as log(10,000,000 / 1,000) =
4.
With growing data velocity the data size easily outgrows the storage limit
of a machine. A solution would be to store the data across a network of
machines. Such filesystems are called distributed filesystems. Since
data is stored across a network all the complications of a network come
in.
This is where Hadoop comes in. It provides one of the most reliable
filesystems. HDFS (Hadoop Distributed File System) is a unique design
that provides storage for extremely large files with streaming data access
pattern, and it runs on commodity hardware. Let's elaborate on the
terms:
Extremely large files: Here, we are talking about the data in a range
of petabytes (1000 TB).
Streaming Data Access Pattern: HDFS is designed on principle
of write-once and read-many-times. Once data is written large portions
of dataset can be processed any number times.
Commodity hardware: Hardware that is inexpensive and easily
available in the market. This is one of the features that especially
distinguishes HDFS from other file systems.
Nodes: Master-slave nodes typically form the HDFS cluster.
1. NameNode(MasterNode):
Note: MasterNode has the record of everything, it knows the location and
info of each and every single data nodes and the blocks they contain, i.e.
nothing is done without the permission of masternode.
Answer: Let's assume that we don't divide, now it's very difficult to store a
100 TB file on a single machine. Even if we store, then each read and
write operation on that whole file is going to take very high seek time. But
if we have multiple blocks of size 128MB then its become easy to perform
various read and write operations on it compared to doing it on a whole
file at once. So we divide the file to have faster data access i.e. reduce
seek time.
Answer: Let's assume we don't replicate and only one yellow block is
present on datanode D1. Now if the data node D1 crashes we will lose the
block and which will make the overall data inconsistent and faulty. So we
replicate the blocks to achieve fault-tolerance.
Note: No two replicas of the same block are present on the same
datanode.
Features:
Distributed data storage.
Blocks reduce seek time.
The data is highly available as the same block is present at multiple
datanodes.
Even if multiple datanodes are down we can still do our work, thus
making it highly reliable.
High fault tolerance.
Limitations: Though HDFS provide many features there are some areas
where it doesn't work well.
Low latency data access: Applications that require low-latency
access to data i.e in the range of milliseconds will not work well with
HDFS, because HDFS is designed keeping in mind that we need high-
throughput of data even at the cost of latency.
Small file problem: Having lots of small files will result in lots of seeks
and lots of movement from one datanode to another datanode to
retrieve each small file, this whole process is a very inefficient data
access pattern.
A table of all the operations is shown below. The following conventions are
used for parameters −
All other files and path names refer to the objects inside HDFS.
Sr.N
Command & Description
o
-ls <path>
1 Lists the contents of the directory specified by path, showing the names, permissions, owner, size and
modification date for each entry.
-lsr <path>
2
Behaves like -ls, but recursively displays entries in all subdirectories of path.
-du <path>
3 Shows disk usage, in bytes, for all the files which match path; filenames are reported with the full HDFS
protocol prefix.
-dus <path>
4
Like -du, but prints a summary of disk usage of all files/directories in the path.
-mv <src><dest>
5
Moves the file or directory indicated by src to dest, within HDFS.
-rm <path>
7
Removes the file or empty directory identified by path.
-rmr <path>
8 Removes the file or directory identified by path. Recursively deletes any child entries (i.e., files or
subdirectories of path).
-cat <filen-ame>
14
Displays the contents of filename on stdout.
-mkdir <path>
17 Creates a directory named path in HDFS.
Creates any parent directories in path that are missing (e.g., mkdir -p in Linux).
Anatomy of File Read and Write in HDFS
Big data is nothing but a collection of data sets that are large, complex, and
which are difficult to store and process using available data management
tools or traditional data processing applications. Hadoop is a framework
(open source) for writing, running, storing, and processing large datasets in
a parallel and distributed manner. It is a solution that is used to overcome
the challenges faced by big data.
Fault-Tolerance
Scalability
Distributed Storage
Reliability
High availability
Cost-effective
High throughput
1. Name Node
2. Data Node
3. Secondary Name Node (SNN)
4. Job Tracker
5. Task Tracker
Let's get an idea of how data flows between the client interacting with
HDFS, the name node, and the data nodes with the help of a diagram.
Consider the figure:
Step 1: The client opens the file it wishes to read by calling open() on the
File System Object(which for HDFS is an instance of Distributed File
System).
Step 2: Distributed File System( DFS) calls the name node, using remote
procedure calls (RPCs), to determine the locations of the first few blocks in
the file. For each block, the name node returns the addresses of the data
nodes that have a copy of that block. The DFS returns an
FSDataInputStream to the client for it to read data from.
FSDataInputStream in turn wraps a DFSInputStream, which manages the
data node and name node I/O.
Step 3: The client then calls read() on the stream. DFSInputStream, which
has stored the info node addresses for the primary few blocks within the
file, then connects to the primary (closest) data node for the primary block
in the file.
Step 4: Data is streamed from the data node back to the client, which calls
read() repeatedly on the stream.
Step 5: When the end of the block is reached, DFSInputStream will close
the connection to the data node, then finds the best data node for the next
block. This happens transparently to the client, which from its point of view
is simply reading an endless stream. Blocks are read as, with the
DFSInputStream opening new connections to data nodes because the
client reads through the stream. It will also call the name node to retrieve
the data node locations for the next batch of blocks as needed.
Step 6: When the client has finished reading the file, a function is called,
close() on the FSDataInputStream.
Next, we'll check out how files are written to HDFS. Consider figure 1.2 to
get a better understanding of the concept.
Note: HDFS follows the Write once Read many times model. In HDFS we
cannot edit the files which are already stored in HDFS, but we can append
data by reopening the files.
Step 1: The client creates the file by calling create() on
DistributedFileSystem(DFS).
Step 2: DFS makes an RPC call to the name node to create a new file in
the file system's namespace, with no blocks associated with it. The name
node performs various checks to make sure the file doesn't already exist
and that the client has the right permissions to create the file. If these
checks pass, the name node prepares a record of the new file; otherwise,
the file can't be created and therefore the client is thrown an error i.e.
IOException. The DFS returns an FSDataOutputStream for the client to
start out writing data to.
Step 3: Because the client writes data, the DFSOutputStream splits it into
packets, which it writes to an indoor queue called the info queue. The data
queue is consumed by the DataStreamer, which is liable for asking the
name node to allocate new blocks by picking an inventory of suitable data
nodes to store the replicas. The list of data nodes forms a pipeline, and
here we'll assume the replication level is three, so there are three nodes in
the pipeline. The DataStreamer streams the packets to the primary data
node within the pipeline, which stores each packet and forwards it to the
second data node within the pipeline.
Step 4: Similarly, the second data node stores the packet and forwards it to
the third (and last) data node in the pipeline.
Step 5: The DFSOutputStream sustains an internal queue of packets that
are waiting to be acknowledged by data nodes, called an "ack queue".
Step 6: This action sends up all the remaining packets to the data node
pipeline and waits for acknowledgments before connecting to the name
node to signal whether the file is complete or not.
HDFS follows Write Once Read Many models. So, we can't edit files that
are already stored in HDFS, but we can include them by again reopening
the file. This design allows HDFS to scale to a large number of concurrent
clients because the data traffic is spread across all the data nodes in the
cluster. Thus, it increases the availability, scalability, and throughput of the
system.
Features:
It never stores the data that is present in the file.
As Namenode works on the Master System, the Master system should
have good processing power and more RAM than Slaves.
It stores the information of DataNode such as their Block id's and
Number of Blocks
How to start Name Node?
[Link] start namenode
How to stop Name Node?
[Link] stop namenode
2. DataNode
DataNode works on the Slave system. The NameNode always instructs
DataNode for storing the Data. DataNode is a program that runs on the
slave system that serves the read/write request from the client. As the
data is stored in this DataNode, they should possess high memory to
store more Data.
3. Secondary NameNode
Secondary NameNode is used for taking the hourly backup of the data. In
case the Hadoop cluster fails, or crashes, the secondary Namenode will
take the hourly backup or checkpoints of that data and store this data into
a file name fsimage. This file then gets transferred to a new system. A
new MetaData is assigned to that new system and a new Master is
created with this MetaData, and the cluster is made to run again
correctly.
This is the benefit of Secondary Name Node. Now in Hadoop2, we have
High-Availability and Federation features that minimize the importance of
this Secondary Name Node in Hadoop2.
Major Function Of Secondary NameNode:
It groups the Edit logs and Fsimage from NameNode together.
It continuously reads the MetaData from the RAM of NameNode and
writes into the Hard Disk.
As secondary NameNode keeps track of checkpoints in a Hadoop
Distributed File System, it is also known as the checkpoint Node.
A. ApplicationsManager
B. Scheduler
An Application Manager is responsible for accepting the request for a
client and also makes a memory resource on the Slaves in a Hadoop
cluster to host the Application Master. The scheduler is utilized for
providing resources for applications in a Hadoop cluster and for
monitoring this application.
5. Node Manager
The Node Manager works on the Slaves System that manages the
memory resource within the Node and Memory Disk. Each Slave Node in
a Hadoop cluster has a single NodeManager Daemon running in it. It also
sends this monitoring information to the Resource Manager.
ResourceManager 8088
NodeManager 8042