Tuesday 7 April 2015


Basics of Map Reduce Algorithm Explained with a Simple Example While processing large set of data, we should definitely address scalability and efficiency in the application code that is processing the large amount of data. Map reduce algorithm (or flow) is highly effective in handling big data. Let us take a simple example and use map reduce to solve a problem. Say you are processing a large amount of data and trying to find out what percentage of your user base where talking about games. First, we will identify the keywords which we are going to map from the data to conclude that its something related to games. Next, we will write a mapping function to identify such patterns in our data. For example, the keywords can be Gold medals, Bronze medals, Silver medals, Olympic football, basketball, cricket, etc. Let us take the following chunks in a big data set and see how to process it. “Hi, how are you” “We love football” “He is an awesome football player” “Merry Christmas” “Olympics will be held in China” “Records broken today in Olympics” “Yes, we won 2 Gold medals” “He qualified for Olympics” Mapping Phase So our map phase of our algorithm will be as follows: 1. Declare a function “Map” 2. Loop: For each words equal to “football” 3. Increment counter 4. Return key value “football”=>counter In the same way, we can define n number of mapping functions for mapping various words words: “Olympics”, “Gold Medals”, “cricket”, etc. Reducing Phase The reducing function will accept the input from all these mappers in form of key value pair and then processing it. So, input to the reduce function will look like the following: reduce(“football”=>2) reduce(“Olympics”=>3) Our algorithm will continue with the following steps: 5. Declare a function reduce to accept the values from map function. 6. Where for each key-value pair, add value to counter. 7. Return “games”=> counter. At the end, we will get the output like “games”=>5. Now, getting into a big picture we can write n number of mapper functions here. Let us say that you want to know who all where wishing each other. In this case you will write a mapping function to map the words like “Wishing”, “Wish”, “Happy”, “Merry” and then will write a corresponding reducer function. Here you will need one function for shuffling which will distinguish between the “games” and “wishing” keys returned by mappers and will send it to the respective reducer function. Similarly you may need a function for splitting initially to give inputs to the mapper functions in form of chunks.