Showing posts with label mapreduce. Show all posts
Showing posts with label mapreduce. Show all posts

Thursday, 31 March 2022

What is sorting phase in MapReduce?

Sorting is a phase that is done on Reducer machine, where all the <key, value> pairs are sorted and group by key before reducer start processing.

What is the advantage of sorting phase?

Since sorting phase sort and groups reducer inputs by keys, it is easy for the reducer to perform aggregate operations.

 

Before Sorting

(day, 2)
(good, 2)
(there, 1)
(the, 1)
(there, 2)
(good, 3)

 

After sorting above data is transformed like below.

(day, [2])
(good, [2, 3])
(the, [1])
(there, [1, 1])

 

What is the order of mapper, partition, shuffling, sorting and reducer phases?

Mapper -> Partitioner -> Shuffle -> Sort -> Reducer

 


Is the sorting phase done on reducer machine?

Yes

 

Previous                                                 Next                                                 Home

Wednesday, 30 March 2022

What is shuffling in MapReduce?

Shuffling is the process of transferring the data from mapper machine to the reducer machine.

 


What is the order of mapper, partition, shuffling, sorting and reducer phases?

Mapper -> Partitioner -> Shuffle -> Sort -> Reducer

 


Is shuffling process occur on MapOnly Job?

Shuffling do not occur when the number of reducers are set to 0.

 

Previous                                                 Next                                                 Home

What is the role of partitioning in Hadoop MapReduce?

Partitioning is the process of splitting the mapper outputs (<key, value> pairs) consistently such that, all the values of a key ‘k1’ goes to the same reducer always.

 

Let me explain it with an example. My organization ‘ABCCorp’ is located in two cities Bangalore, Chennai. Now I want to consolidate the login, logout times of employees by city. To perform this task, I created a MapReduce job with 3 mappers and 2 reducers (reducer1 to handle Bangalore employees data and reducer2 to handle Chennai employees data).

 


But the catch here is, How to transfer <key, value> pairs of Bangalore city to Reducer1 and <key, value> pairs of Chennai city to Reducer2. Here the partitioner to address this use case. Partitioner takes the mapper output and determine the reducer instance that this <key, value> pair transferred to.  


How to implement custom partitioner?

You can implement a custom partitioner by extending ‘Partitioner’ class.

public static class PatitionerByCity extends Partitioner<Text, Text> {

  @Override
  public int getPartition(Text key, Text value, int numPartitions) {
   /*
    * Send all Bangalore employees data to reducer1, and all other data to reducer2
    */
   if (key.toString().equalsIgnoreCase("Bangalore")) {
    return 0 % numPartitions;
   } else {
    return 1 % numPartitions;
   }
  }

 }

 

You can set this partitioner to the job using setPartitionerClass method.

job.setPartitionerClass(PatitionerByCity.class);

 

You can find complete working application here.

https://self-learning-java-tutorial.blogspot.com/2016/01/hadoop-partitioner.html

 

What is the default partitioner in Hadoop?

‘Hash Partitioner’ is the default one, it assign the partitions based on the key hash value.

 

Is partitioning process happen on mapper compute?

Yes, Partitioning always happen on mapper compute.

 

What is the order of mapper, partition and reducer processes?

Partitioning phase execute after the map phase, and before the reducer phase.


 

 

Previous                                                 Next                                                 Home

Tuesday, 29 March 2022

What is Map Only Job in Hadoop?

There are some situations, where you do not require a reducer to aggregate/process the results from mapper phase.

 

For example, I just want to print all the log messages that contains the string ‘maclicious login attempt’. In this example, I do not require a reducer phase, only map phase output is sufficient to me to get all the log messages.

 

How to turn off the reducer?

job.setNumReduceTasks(0);

 

Some other examples of Map only jobs

Example 1: Delete the documents that are older than 5 years from Hadoop.

 

Example 2: Get all the employees who are staying at Bangalore.

SELECT * FROM employees where city='Bangalore';

 

Reference

https://hadoop.apache.org/docs/r2.4.1/api/org/apache/hadoop/mapreduce/Job.html#setNumReduceTasks(int)

Previous                                                 Next                                                 Home

Friday, 25 March 2022

Quick guide to Hadoop MapReduce

In this post, I am going to explain how Hadoop MapReduce works.

 

What is the problem with traditional computing?

In traditional computing, you need to have all the data in the system upfront to perform some processing on it. But take this scenario where you have peta bytes of data, is it possible to store and process this information in one single system, the answer is NO. This is where distributed computing comes into picture.

 

How MapReduce address this problem?

In Hadoop, data is divided into multiple blocks and persisted on many data nodes (computes). Hadoop did most of the processing at data nodes and later point of time merge the results from data nodes.

 

What is MapReduce?

MapReduce is a computing technique to process the data in a distributed manner. There are two key phases in MapReduce.

a.   Map phase

b.   Reducer phase

 

One key point to note here is that, both map and reduce phase can only understand <key, value> pairs.

 

Let us try to understand more with some practical example.

 

Example: Let’s take a Word count program, that count how many times a word is repeated in the file ‘input.txt’.

 

input.txt

Hello How are you
are you going to canada
Hello are you there
ya i am doing work
Hi ptr How are you
Hi buddy good day
good day too
very very good morning
good night

 

Assume the file ‘input.txt’ is of size 500MB, persisted in HDFS. By assuming a block size of 128MB, this file is divided into 4 blocks and persisted on different data nodes.

 


 

Block B1

Hello How are you
are you going to canada

 

Block B2

Hello are you there
ya i am doing work

 

Block B3

Hi ptr How are you
Hi buddy good day
good day too

 

Block B4

very very good morning
good night

 

When a client write a Map-Reduce code and submit it to Hadoop, this code is shipped to respective data nodes where the file data blocks persisted and map phase gets executed.

 

 


 

Map Phase in detail

As I said previously, Map and Reduce phases can only understand the data which is in <key, value> pairs. But the data is in text format here. Here RecordReader comes into picture to help mapper program. RecordReader reads the data and create <key, value> pairs.

 

For example, LineRecordReader (it is the default record reader in Hadoop), read the input data line by line and convert it into <key, value> pairs, where key is the byte offset and value is the actual line data.

 

LineRecordReader output for the Block1 data looks like below.

 

<12324, Hello How are you>

<961245, are you going to canada>

 

12324 and 961245 are the byte offsets (addresses) of the content.

 

Once the data is converted to <key, value> format, mapper can start processing the data. Mapper transforms input into intermediate form. The output of mapper goes to reducer.

 

Mapper program split the data Block B1 like below

<Hello, 1>
<How, 1>
<are, 1>
<you, 1>
<are, 1>
<you, 1>
<going, 1>
<to, 1>
<Canada, 1>

In the above snippet,

a.   Key is the actual string

b.   Value is the count, how many times the string is repeated.

 

As you see above snippet, strings like ‘are’ and ‘you’ repeated twice. We can combine these duplicate keys using a combiner step. Combiner step is optional, and it gets executed after mapper phase in the same data node. Most of the times, it does the similar thing what a reducer do, so it is called as local reducer.

 

After combining these duplicated entries, intermediate results transformed like below.

<Hello, 1>
<How, 1>
<are, 2>
<you, 2>
<going, 1>
<to, 1>
<Canada, 1>

The Mapper outputs are sorted and then partitioned per Reducer. The total number of partitions is the same as the number of reduce tasks for the job. Users can control which keys (and hence records) go to which Reducer by implementing a custom Partitioner.

 

Map phase output for different data blocks is given below.

Block1

(Hello, 1)
(How, 1)
(are, 2)
(canada, 1)
(going, 1)
(to, 1)
(you, 2)

Block2

(Hello, 1)
(am, 1)
(are, 1)
(doing, 1)
(i, 1)
(there, 1)
(work, 1)
(ya, 1)
(you, 1)

Block3

(Hi, 2)
(How, 1)
(are, 1)
(buddy, 1)
(day, 2)
(good, 2)
(ptr, 1)
(too, 1)
(you, 1)

Block4

(good, 2)
(morning, 1)
(night, 1)
(very, 2)

Reducer Phase

Reducer phase works on the <key, value> pairs produced by map phase.





Reducer can run on one (or) more machines, these machines can be either the data nodes that holds this file blocks or some different data nodes. Values with the same Key always lands at the same Reducer.

 

Reducer takes the output of Mapper phase and gives you final result.

 

Reducer has three primary phases.

a.   Shuffle

b.   Sorting

c.    Reduce





a.Shuffle

Shuffling is the process of transferring the map phase output from the mapper data nodes to reducer data nodes.

 

b.Sorting

Sorting phase groups reducer inputs by keys (since different mappers may have output the same key) in this stage. This stage output can be like below.

(Hello, [1, 1])
(Hi, [2])
(How, [1, 1])
(am, [1])
(are, [2, 1, 1])
(buddy, [1])
(canada, [1])
(day, [2])
(doing, [1])
(going, [1])
(good, [2, 2])
(i, [1])
(morning, [1])
(night, [1])
(ptr, [1])
(there, [1])
(to, [1])
(too, [1])
(very, [2])
(work, [1])
(ya, [1])
(you, [2, 1, 1])

c. reduce

reduce method is called for each <key, (collection of values)> in the sorted inputs and produce the final result.

 

After reducer step final output, looks like below.

(Hello, 2)
(Hi, 2)
(How, 2)
(am, 1)
(are, 4)
(buddy, 1)
(canada, 1)
(day, 2)
(doing, 1)
(going, 1)
(good, 4)
(i, 1)
(morning, 1)
(night, 1)
(ptr, 1)
(there, 1)
(to, 1)
(too, 1)
(very, 2)
(work, 1)
(ya, 1)
(you, 4)


 

Previous                                                 Next                                                 Home