Four MapReduce Design Patterns
Last article, I wrote about creating a simple word count program in MapReduce along with a tutorial to run the program using Hadoop. Please go through it if you are not aware of how to write a program in MapReduce.
To solve any problem in MapReduce, we need to think in terms of MapReduce. It is not necessarily true that every time we have both a map and reduce job.
MapReduce Design Pattern
- Input-Map-Reduce-Output
- Input-Map-Output
- Input-Multiple Maps-Reduce-Output
- Input-Map-Combiner-Reduce-Output
Following is a real time scenario to understand when to use which design pattern.
- Input-Map-Reduce-Output
If we want to do some aggregation then this pattern is used:
Scenario |
Counting gender total/ average salary of employees |
Map (Key, Value) |
Key: Gender Value: Their Salary |
Reduce |
Group by Gender And Take Total of salary for each group |
- Input-Map-Output
If we want to change only the format of data then this pattern is used:
Scenario |
Some employees have gender entry as “Female”, “F”,”f”,0 |
Map (Key, Value) |
Key : Employee Id Value : Gender -> if Gender is Female/ F/ f/ 0 then converted to F else if Gender is Male/M/m/1 then convert to M |
- Input-Multiple Maps-Reduce-Output
In this design pattern, our input is taken from two files which have a different schema:
Scenario |
We have to find the total of gender-wide salary. But he have 2 files with different schema. Input File 1 Gender is given as a prefix to Name Eg. Ms. Shital Katkar Mr. Krishna Katkar Input File 2 There is a different column for gender. However, the format is mixed. Eg. Female/Male, 0/1, F/M |
Map (Key, Value) |
Map 1 (For input 1) We need to write a program to split prefix from Name and, according to the prefix, determine the gender. Then prepare the Key value pair (Gender,Salary). Map 2 (For input 2) Here the program will be straightforward. Resolve the mixed format and make a key value pair (Gender,Salary). |
Reduce |
Group by Gender And Take Total of salary for each group |
- Input-Map-Combiner-Reduce-Output
A Combiner, also known as a semi-reducer, is an optional class that operates by accepting the inputs from the Map class and thereafter passing the output key-value pairs to the Reducer class. Purpose of the combiner is to reduce workload of Reducer.
In MapReduce program, 20% of the work is done in the Map Stage, which is also known as the data preparation stage, which works in parallel.
80% of the work is done in Reduce stage which is known as the calculation stage, and it is not parallel. Therefore it is slower than the Map phase. To reduce time, some work in the Reduce phase can be done in the combiner phase.
Scenario
There are 5 departments. And we have to calculate the gender-wide total salary. However, there are certain rules to calculate the total. After calculating gender wise total for each department, if the salary is greater than 200K, add 20K in total, if the salary is greater than 100K, add 10K in total
Input Files (for each department there is 1 file) |
Map (Parallel) (, Value = Salary) |
Combiner (Parallel) |
Reducer (Not Parallel) |
Output |
Dept 1 |
Male<10,20,25,45,15,45,25,20> Female <10,30,20,25,35> |
Male <250,20> Female <120,10> |
Male < 250,20,155, 10,90,90,30> Female <120,10,175,10,135, 10,110,10,130,10> |
Male <645> Female <720> |
Dept 2 |
Male<15,30,40,25,45> Female <20,35,25,35,40> |
Male <155,10> Female <175,10> |
||
Dept 3 |
Male<10,20,20,40> Female <10,30,25,70> |
Male <90,00> Female <135,10> |
||
Dept 4 |
Male<45,25,20> Female <30,20,25,35> |
Male <90,00> Female <110,10> |
||
Dept 5 |
Male<10,20> Female <10,30,20,25,35> |
Male <30,00> Female <130,10> |
- The points mentioned above are basic patterns. You can make you own as per requirement of the problem.