Hadoop

MapReduce Flow Chart Sample Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>In this mapreduce tutorial we will explain mapreduce sample example with its flow chart&period; How to work mapreduce for a job&period;<br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<h2><b>A SIMPLE EXAMPLE FOR WORD COUNT<&sol;b><&sol;h2>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li style&equals;"list-style-type&colon; none&semi;">&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>We have a large collection of text documents in a folder&period; &lpar;Just to give a feel size&period;&period; we have 1000 documents each with average of 1 Millions words&rpar;<&sol;li>&NewLine;<li><i><b>What we need to calculate&colon;-<&sol;b><&sol;i>&NewLine;<ul>&NewLine;<li><i><b>Count the frequency of each distinct word in the documents&quest;<&sol;b><&sol;i><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li style&equals;"list-style-type&colon; none&semi;">&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><i><b>How would you solve this using simple Java program&quest;<&sol;b><&sol;i><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li style&equals;"list-style-type&colon; none&semi;">&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><i><b>How many lines of codes will u write&quest;<&sol;b><&sol;i><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><i><b>How much will be the program execution time&quest;<&sol;b><&sol;i><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>To overcome listed above problems into some line using mapreduce program&period; Now we look into below mapreduce function for understanding how to its work on large dataset&period;<&sol;p>&NewLine;<h2><b>MAP FUNCTION<&sol;b><&sol;h2>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Map Functions operate on every key&comma; value pair of data and transformation logic provided in the map function&period;<&sol;li>&NewLine;<li>Map Function always emits a Key&comma; Value Pair as output<br &sol;>&NewLine;<b>Map&lpar;Key1&comma; Valiue1&rpar; &&num;8211&semi;&gt&semi; List&lpar;Key2&comma; Value2&rpar;<&sol;b><&sol;li>&NewLine;<li>Map Function transformation is similar to Row Level Function in Standard SQL<&sol;li>&NewLine;<li>For Each File&NewLine;<ul>&NewLine;<li><b>Map Function is<&sol;b>&NewLine;<ul>&NewLine;<li>Read each line from the input file&NewLine;<ul>&NewLine;<li>Tokenize and get each word&NewLine;<ul>&NewLine;<li>Emit the word&comma; 1 for every word found<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p><b>The emitted word&comma; 1 will from the List that is output from the mapper<&sol;b><&sol;p>&NewLine;<p><b>So who take ensuring the file is distributed and each line of the file is passed to each of the map function&quest;<&sol;b>-Hadoop Framework take care about this&comma; no need to worry about the distributed system&period;<&sol;p>&NewLine;<h2><b>REDUCE FUNCTION<&sol;b><&sol;h2>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Reduce Functions takes list of value for every key and transforms the data based on the &lpar;aggregation&rpar; logic provided in the reduce function&period;<&sol;li>&NewLine;<li>Reduce Function<br &sol;>&NewLine;<b>Reduce&lpar;Key2&comma; List&lpar;Value2&rpar;&rpar; &&num;8211&semi;&gt&semi; List&lpar;Key3&comma; Value3&rpar;<&sol;b><&sol;li>&NewLine;<li>Reduce Functions is similar to Aggregate Functions in Standard SQL<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>Reduce&lpar;Key2&comma; List&lpar;Value2&rpar;&rpar; &&num;8211&semi;&gt&semi; List&lpar;Key3&comma; Value3&rpar;<&sol;b><&sol;p>&NewLine;<p>For the List&lpar;key&comma; value&rpar; output from the mapper Shuffle and Sort the data by key<br &sol;>&NewLine;Group by Key and create the list of values for a key<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>Reduce Function is<&sol;b>&NewLine;<ul>&NewLine;<li>Read each key &lpar;word&rpar; and list of values &lpar;1&comma;1&comma;1&period;&period;&rpar; associated with it&period;&NewLine;<ul>&NewLine;<li>For each key add the list of values to calculate sum&NewLine;<ul>&NewLine;<li>Emit the word&comma; sum for every word found<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>So who is ensuring the shuffle&comma; sort&comma; group by etc&quest;<&sol;b><&sol;p>&NewLine;<h2><b>MAP FUNCTION FOR WORD COUNT<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">private final static IntWritable one &equals; new IntWritable&lpar;1&rpar;&semi; &NewLine;private Text word &equals; new Text&lpar;&rpar;&semi; &NewLine; &NewLine;public void map&lpar;LongWritable key&comma; Text value&comma; Context context&rpar; throws IOException&comma; InterruptedException &lbrace; &NewLine;String line &equals; value&period;toString&lpar;&rpar;&semi; &NewLine;StringTokenizer tokenizer &equals; new StringTokenizer&lpar;line&rpar;&semi; &NewLine;While&lpar;tokenizer&period;hasMoreTokens&lpar;&rpar;&rpar;&lbrace; &NewLine; &NewLine;word&period;set&lpar;tokenizer&period;nextToken&lpar;&rpar;&rpar;&semi; &NewLine;context&period;write&lpar;word&comma; one&rpar;&semi; &NewLine;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>REDUCE FUNCTION FOR WORD COUNT<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">public void reduce&lpar;Text key&comma; Iterable &lt&semi;IntWritable&gt&semi; values&comma; Context context&rpar; throws IOException&comma; InterruptedException&lbrace; &NewLine; &NewLine;int sum &equals; 0&semi; &NewLine;for&lpar;IntWritable val &colon; values&rpar;&lbrace; &NewLine;sum &plus;&equals; val&period;get&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine;context&period;write&lpar;key&comma; new IntWritable&lpar;sum&rpar;&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>ANATOMY OF A MAPREDUCE PROGRAM<&sol;b><&sol;h2>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;11&sol;map-reduce&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>FLOW CHART OF A MAPREDUCE PROGRAM<&sol;b><&sol;h2>&NewLine;<p>Suppose we have a file with size about 200 MB&comma; suppose content as follows<&sol;p>&NewLine;<p><b>&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8211&semi;file&period;txt&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;<&sol;b><br &sol;>&NewLine;<b>&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;File&lpar;200 MB&rpar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;<&sol;b><br &sol;>&NewLine;<i>hi how are you<&sol;i><br &sol;>&NewLine;<i>how is your job &lpar;64 MB&rpar; 1-Split<&sol;i><br &sol;>&NewLine;<i>&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;<&sol;i><br &sol;>&NewLine;<i>&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;-<&sol;i><br &sol;>&NewLine;<i>&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;<&sol;i><br &sol;>&NewLine;<i>how is your family<&sol;i><br &sol;>&NewLine;<i>how is your brother &lpar;64 MB&rpar; 2-Split<&sol;i><br &sol;>&NewLine;<i>&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;<&sol;i><br &sol;>&NewLine;<i>&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;-<&sol;i><br &sol;>&NewLine;<i>&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;<&sol;i><br &sol;>&NewLine;<i>how is your sister<&sol;i><br &sol;>&NewLine;<i>what is the time now &lpar;64 MB&rpar; 3-Split<&sol;i><br &sol;>&NewLine;<i>&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;<&sol;i><br &sol;>&NewLine;<i>&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;-<&sol;i><br &sol;>&NewLine;<i>&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;<&sol;i><br &sol;>&NewLine;<i>what is the strength of hadoop &lpar;8 MB&rpar; 4-Split<&sol;i><br &sol;>&NewLine;<i>&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;&lowbar;<&sol;i><br &sol;>&NewLine;<i>&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;-<&sol;i><&sol;p>&NewLine;<p>In above file we have divided this file into 4 splits with sizes three splits with size 64 MB and last fourth split with size 8 MB&period;<&sol;p>&NewLine;<p><b>Input File Formats&colon;<&sol;b><br &sol;>&NewLine;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;-<br &sol;>&NewLine;<b>1&period; TextInputFormat<&sol;b><br &sol;>&NewLine;<b>2&period; KeyValueTextInputFormat<&sol;b><br &sol;>&NewLine;<b>3&period; SequenceFileInputFormat<&sol;b><br &sol;>&NewLine;<b>4&period; SequenceFileAsTextInputFormat <&sol;b><br &sol;>&NewLine;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;&&num;8212&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;11&sol;mapreduce-flow-chart&period;jpg" width&equals;"640" height&equals;"538" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>Lets see in another following figure to understand the process of MAPREDUCE&period;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;11&sol;mapreduce-process&period;jpg" width&equals;"640" height&equals;"289" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-mapreduce&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;mapreduce-programming-hello-world-job&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '175'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago