<p>Related to my <a href="https://stackoverflow.com/questions/28975/anyone-using-couchdb">CouchDB</a> question.</p>

<p>Can anyone explain MapReduce in terms a numbnuts could understand?</p>

[{'answer_id': 28991, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 5, 'selected': False, 'text': '<ol>\n<li>Take a bunch of data</li>\n<li>Perform some kind of transformation that converts every datum to another kind of datum</li>\n<li>Combine those new data into yet simpler data</li>\n</ol>\n\n<p>Step 2 is Map. Step 3 is Reduce.</p>\n\n<p>For example,</p>\n\n<ol>\n<li>Get time between two impulses on a pair of pressure meters on the road</li>\n<li>Map those times into speeds based upon the distance of the meters</li>\n<li>Reduce those speeds to an average speed</li>\n</ol>\n\n<p>The reason MapReduce is split between Map and Reduce is because different parts can easily be done in parallel. (Especially if Reduce has certain mathematical properties.)</p>\n\n<p>For a complex but good description of MapReduce, see: <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.104.5859&amp;rep=rep1&amp;type=pdf" rel="noreferrer">Google\'s MapReduce Programming Model -- Revisited (PDF)</a>.</p>\n'}, {'answer_id': 29029, 'author': 'Damien B', 'author_id': 3069, 'author_profile': 'https://Stackoverflow.com/users/3069', 'pm_score': 4, 'selected': False, 'text': '<p>Let\'s take the example from the <a href="https://static.googleusercontent.com/media/research.google.com/en//archive/mapreduce-osdi04.pdf" rel="noreferrer">Google paper</a>. The goal of MapReduce is to be able to use efficiently a load of processing units working in parallels for some kind of algorithms. The exemple is the following: you want to extract all the words and their count in a set of documents.</p>\n\n<p>Typical implementation:</p>\n\n<pre><code>for each document\n    for each word in the document\n        get the counter associated to the word for the document\n        increment that counter \n    end for\nend for\n</code></pre>\n\n<p>MapReduce implementation:</p>\n\n<pre><code>Map phase (input: document key, document)\nfor each word in the document\n    emit an event with the word as the key and the value "1"\nend for\n\nReduce phase (input: key (a word), an iterator going through the emitted values)\nfor each value in the iterator\n    sum up the value in a counter\nend for\n</code></pre>\n\n<p>Around that, you\'ll have a master program which will partition the set of documents in "splits" which will be handled in parallel for the Map phase. The emitted values are written by the worker in a buffer specific to the worker. The master program then delegates other workers to perform the Reduce phase as soon as it is notified that the buffer is ready to be handled.</p>\n\n<p>Every worker output (being a Map or a Reduce worker) is in fact a file stored on the distributed file system (GFS for Google) or in the distributed database for CouchDB.</p>\n'}, {'answer_id': 29086, 'author': 'Johnno Nolan', 'author_id': 1116, 'author_profile': 'https://Stackoverflow.com/users/1116', 'pm_score': 6, 'selected': False, 'text': '<p>MapReduce is a method to process vast sums of data in parallel without requiring the developer to write any code other than the mapper and reduce functions.</p>\n<p>The <strong>map</strong> function takes data in and churns out a result, which is held in a barrier. This function can run in parallel with a large number of the same <strong>map</strong> task. The dataset can then be <strong>reduced</strong> to a scalar value.</p>\n<p>So if you think of it like a SQL statement</p>\n<pre><code>SELECT SUM(salary)\nFROM employees\nWHERE salary &gt; 1000\nGROUP by deptname\n</code></pre>\n<p>We can use <strong>map</strong> to get our subset of employees with salary &gt; 1000\nwhich map emits to the barrier into group size buckets.</p>\n<p><strong>Reduce</strong> will sum each of those groups. Giving you your result set.</p>\n<p><em>just plucked this from my <a href="http://www.cs.man.ac.uk/" rel="nofollow noreferrer">university</a> study notes of the google paper</em></p>\n'}, {'answer_id': 30655, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 8, 'selected': False, 'text': '<p>Going all the way down to the basics for Map and Reduce.</p>\n\n<hr>\n\n<p><strong>Map</strong> is a function which "transforms" items in some kind of list to another kind of item and put them back in the same kind of list.</p>\n\n<p>suppose I have a list of numbers: [1,2,3] and I want to double every number, in this case, the function to "double every number" is function x = x * 2. And without mappings, I could write a simple loop, say</p>\n\n<pre><code>A = [1, 2, 3]\nforeach (item in A) A[item] = A[item] * 2\n</code></pre>\n\n<p>and I\'d have A = [2, 4, 6] but instead of writing loops, if I have a map function I could write</p>\n\n<pre><code>A = [1, 2, 3].Map(x =&gt; x * 2)\n</code></pre>\n\n<p>the x => x * 2 is a function to be executed against the elements in [1,2,3]. What happens is that the program takes each item, execute (x => x * 2) against it by making x equals to each item, and produce a list of the results.</p>\n\n<pre><code>1 : 1 =&gt; 1 * 2 : 2  \n2 : 2 =&gt; 2 * 2 : 4  \n3 : 3 =&gt; 3 * 2 : 6  \n</code></pre>\n\n<p>so after executing the map function with (x => x * 2) you\'d have [2, 4, 6].</p>\n\n<hr>\n\n<p><strong>Reduce</strong> is a function which "collects" the items in lists and perform some computation on <em>all</em> of them, thus reducing them to a single value.</p>\n\n<p>Finding a sum or finding averages are all instances of a reduce function. Such as if you have a list of numbers, say [7, 8, 9] and you want them summed up, you\'d write a loop like this</p>\n\n<pre><code>A = [7, 8, 9]\nsum = 0\nforeach (item in A) sum = sum + A[item]\n</code></pre>\n\n<p>But, if you have access to a reduce function, you could write it like this</p>\n\n<pre><code>A = [7, 8, 9]\nsum = A.reduce( 0, (x, y) =&gt; x + y )\n</code></pre>\n\n<p>Now it\'s a little confusing why there are 2 arguments (0 and the function with x and y) passed. For a reduce function to be useful, it must be able to take 2 items, compute something and "reduce" that 2 items to just one single value, thus the program could reduce each pair until we have a single value.</p>\n\n<p>the execution would follows:</p>\n\n<pre><code>result = 0\n7 : result = result + 7 = 0 + 7 = 7\n8 : result = result + 8 = 7 + 8 = 15\n9 : result = result + 9 = 15 + 9 = 24\n</code></pre>\n\n<p>But you don\'t want to start with zeroes all the time, so the first argument is there to let you specify a seed value specifically the value in the first <code>result =</code> line.</p>\n\n<p>say you want to sum 2 lists, it might look like this:</p>\n\n<pre><code>A = [7, 8, 9]\nB = [1, 2, 3]\nsum = 0\nsum = A.reduce( sum, (x, y) =&gt; x + y )\nsum = B.reduce( sum, (x, y) =&gt; x + y )\n</code></pre>\n\n<p>or a version you\'d more likely to find in the real world:</p>\n\n<pre><code>A = [7, 8, 9]\nB = [1, 2, 3]\n\nsum_func = (x, y) =&gt; x + y\nsum = A.reduce( B.reduce( 0, sum_func ), sum_func )\n</code></pre>\n\n<hr>\n\n<p>Its a good thing in a DB software because, with Map\\Reduce support you can work with the database without needing to know how the data are stored in a DB to use it, thats what a DB engine is for.</p>\n\n<p>You just need to be able to "tell" the engine what you want by supplying them with either a Map or a Reduce function and then the DB engine could find its way around the data, apply your function, and come up with the results you want all without you knowing how it loops over all the records.</p>\n\n<p>There are indexes and keys and joins and views and a lot of stuffs a single database could hold, so by shielding you against how the data is actually stored, your code are made easier to write and maintain.</p>\n\n<p>Same goes for parallel programming, if you only specify what you want to do with the data instead of actually implementing the looping code, then the underlying infrastructure could "parallelize" and execute your function in a simultaneous parallel loop for you.</p>\n'}, {'answer_id': 762443, 'author': 'Rainer Joswig', 'author_id': 69545, 'author_profile': 'https://Stackoverflow.com/users/69545', 'pm_score': 5, 'selected': False, 'text': "<p>MAP and REDUCE are old Lisp functions from a time when man killed the last dinosaurs.</p>\n\n<p>Imagine you have a list of cities with informations about the name, number of people living there and the size of the city:</p>\n\n<pre><code>(defparameter *cities*\n  '((a :people 100000 :size 200)\n    (b :people 200000 :size 300)\n    (c :people 150000 :size 210)))\n</code></pre>\n\n<p><strong>Now you may want to find the city with the highest population density.</strong></p>\n\n<p>First we create a list of city names and population density using MAP:</p>\n\n<pre><code>(map 'list\n     (lambda (city)\n         (list (first city)\n               (/ (getf (rest city) :people)\n                  (getf (rest city) :size))))\n     *cities*)\n\n=&gt;   ((A 500) (B 2000/3) (C 5000/7))\n</code></pre>\n\n<p>Using REDUCE we can now find the city with the largest population density.</p>\n\n<pre><code>(reduce (lambda (a b)\n          (if (&gt; (second a) (second b))\n             a\n             b))\n        '((A 500) (B 2000/3) (C 5000/7)))\n\n =&gt;   (C 5000/7)\n</code></pre>\n\n<p>Combining both parts we get the following code:</p>\n\n<pre><code>(reduce (lambda (a b)\n          (if (&gt; (second a) (second b))\n             a\n             b))\n        (map 'list\n             (lambda (city)\n                (list (first city)\n                   (/ (getf (rest city) :people)\n                      (getf (rest city) :size))))\n             *cities*))\n</code></pre>\n\n<p>Let's introduce functions:</p>\n\n<pre><code>(defun density (city)\n   (list (first city)\n         (/ (getf (rest city) :people)\n            (getf (rest city) :size))))\n\n(defun max-density (a b)\n   (if (&gt; (second a) (second b))\n          a\n          b))\n</code></pre>\n\n<p>Then we can write our MAP REDUCE code as:</p>\n\n<pre><code>(reduce 'max-density\n        (map 'list 'density *cities*))\n\n =&gt;   (C 5000/7)\n</code></pre>\n\n<p>It calls <code>MAP</code> and <code>REDUCE</code> (evaluation is inside out), so it is called <em>map reduce</em>.</p>\n"}, {'answer_id': 6584690, 'author': 'Mike Dewar', 'author_id': 270572, 'author_profile': 'https://Stackoverflow.com/users/270572', 'pm_score': 2, 'selected': False, 'text': "<p>I don't want to sound trite, but this helped me so much, and it's pretty simple:</p>\n\n<pre><code>cat input | map | reduce &gt; output\n</code></pre>\n"}, {'answer_id': 26993598, 'author': 'Prometheus', 'author_id': 2587178, 'author_profile': 'https://Stackoverflow.com/users/2587178', 'pm_score': 4, 'selected': False, 'text': '<p>A really <strong>easy</strong>, <strong>quick</strong> and <strong>&quot;for dummies&quot;</strong> introduction to MapReduce is available at: <a href="http://www.marcolotz.com/?p=67" rel="noreferrer">http://www.marcolotz.com/?p=67</a></p>\n<p>Posting some of it\'s content:</p>\n<p><strong>First of all, why was MapReduce originally created?</strong></p>\n<p>Basically Google needed a solution for making large computation jobs easily parallelizable, allowing data to be distributed in a number of machines connected through a network. Aside from that, it had to handle the machine failure in a transparent way and manage load balancing issues.</p>\n<p><strong>What are MapReduce true strengths?</strong></p>\n<p>One may say that MapReduce magic is based on the Map and Reduce functions application. I must confess mate, that I strongly disagree. The main feature that made MapReduce so popular is its capability of automatic parallelization and distribution, combined with the simple interface. These factor summed with transparent failure handling for most of the errors made this framework so popular.</p>\n<p><strong>A little more depth on the paper:</strong></p>\n<p>MapReduce was originally mentioned in a Google paper (Dean &amp; Ghemawat, 2004 – link here) as a solution to make computations in Big Data using a parallel approach and commodity-computer clusters. In contrast to Hadoop, that is written in Java, the Google’s framework is written in C++. The document describes how a parallel framework would behave using the Map and Reduce functions from functional programming over large data sets.</p>\n<p>In this solution there would be two main steps – called Map and Reduce –, with an optional step between the first and the second – called Combine. The Map step would run first, do computations in the input key-value pair and generate a new output key-value. One must keep in mind that the format of the input key-value pairs does not need to necessarily match the output format pair. The Reduce step would assemble all values of the same key, performing other computations over it. As a result, this last step would output key-value pairs. One of the most trivial applications of MapReduce is to implement word counts.</p>\n<p><strong>The pseudo-code for this application, is given bellow:</strong></p>\n<pre><code>map(String key, String value):\n\n// key: document name\n// value: document contents\nfor each word w in value:\nEmitIntermediate(w, “1”);\n\nreduce(String key, Iterator values):\n\n// key: a word\n// values: a list of counts\nint result = 0;\nfor each v in values:\n    result += ParseInt(v);\nEmit(AsString(result));\n</code></pre>\n<p>As one can notice, the map reads all the words in a record (in this case a record can be a line) and emits the word as a key and the number 1 as a value.\nLater on, the reduce will group all values of the same key. Let’s give an example: imagine that the word ‘house’ appears three times in the record. The input of the reducer would be [house,[1,1,1]]. In the reducer, it will sum all the values for the key house and give as an output the following key value: [house,[3]].</p>\n<p>Here’s an image of how this would look like in a MapReduce framework:</p>\n<p><img src="https://i.stack.imgur.com/EH8tY.jpg" alt="Image from the Original MapReduce Google paper" /></p>\n<p>As a few other classical examples of MapReduce applications, one can say:</p>\n<p>•Count of URL access frequency</p>\n<p>•Reverse Web-link Graph</p>\n<p>•Distributed Grep</p>\n<p>•Term Vector per host</p>\n<p>In order to avoid too much network traffic, the paper describes how the framework should try to maintain the data locality. This means that it should always try to make sure that a machine running Map jobs has the data in its memory/local storage, avoiding to fetch it from the network. Aiming to reduce the network through put of a mapper, the optional combiner step, described before, is used. The Combiner performs computations on the output of the mappers in a given machine before sending it to the Reducers – that may be in another machine.</p>\n<p>The document also describes how the elements of the framework should behave in case of faults. These elements, in the paper, are called as worker and master. They will be divided into more specific elements in open-source implementations.\nSince the Google has only described the approach in the paper and not released its proprietary software, many open-source frameworks were created in order to implement the model. As examples one may say Hadoop or the limited MapReduce feature in MongoDB.</p>\n<p>The run-time should take care of non-expert programmers details, like partitioning the input data, scheduling the program execution across the large set of machines, handling machines failures (in a transparent way, of  course) and managing the inter-machine communication. An experienced user may tune these parameters, as how the input data will be partitioned between workers.</p>\n<p><strong>Key Concepts:</strong></p>\n<p>•<strong>Fault Tolerance:</strong> It must tolerate machine failure gracefully. In order to perform this, the master pings the workers periodically. If the master does not receive responses from a given worker in a definite time lapse, the master will define the work as failed in that worker. In this case, all map tasks completed by the faulty worker are thrown away and are given to another available worker. Similar happens if the worker was still processing a map or a reduce task. Note that if the worker already completed its reduce part, all computation was already finished by the time it failed and does not need to be reset. As a primary point of failure, if the master fails, all the job fails. For this reason, one may define periodical checkpoints for the master, in order to save its data structure. All computations that happen between the last checkpoint and the master failure are lost.</p>\n<p>•<strong>Locality:</strong> In order to avoid network traffic, the framework tries to make sure that all the input data is locally available to the machines that are going to perform computations on them. In the original description, it uses Google File System (GFS) with replication factor set to 3 and block sizes of 64 MB. This means that the same block of 64 MB (that compose a file in the file system) will have identical copies in three different machines. The master knows where are the blocks and try to schedule map jobs in that machine. If that fails, the master tries to allocate a machine near a replica of the tasks input data (i.e. a worker machine in the same rack of the data machine).</p>\n<p>•<strong>Task Granularity:</strong> Assuming that each map phase is divided into M pieces and that each Reduce phase is divided into R pieces, the ideal would be that M and R are a lot larger than the number of worker machines. This is due the fact that a worker performing many different tasks improves dynamic load balancing. Aside from that, it increases the recovery speed in the case of worker fail (since the many map tasks it has completed can be spread out across all the other machines).</p>\n<p>•<strong>Backup Tasks:</strong> Sometimes, a Map or Reducer worker may behave a lot more slow than the others in the cluster. This may hold the total processing time and make it equal to the processing time of that single slow machine. The original paper describes an alternative called Backup Tasks, that are scheduled by the master when a MapReduce operation is close to completion. These are tasks that are scheduled by the Master of the in-progress tasks. Thus, the MapReduce operation completes when the primary or the backup finishes.</p>\n<p>•<strong>Counters:</strong> Sometimes one may desire to count events occurrences. For this reason, counts where created. The counter values in each workers are periodically propagated to the master. The master then aggregates (Yep. Looks like Pregel aggregators came from this place ) the counter values of a successful map and reduce task and return them to the user code when the MapReduce operation is complete. There is also a current counter value available in the master status, so a human watching the process can keep track of how it is behaving.</p>\n<p>Well, I guess with all the concepts above, Hadoop will be a piece of cake for you. If you have any question about the original MapReduce article or anything related, please let me know.</p>\n'}, {'answer_id': 35546776, 'author': 'Rafay', 'author_id': 569085, 'author_profile': 'https://Stackoverflow.com/users/569085', 'pm_score': 3, 'selected': False, 'text': "<p>If you are familiar with Python, following is the simplest possible explanation of MapReduce:</p>\n\n<pre><code>In [2]: data = [1, 2, 3, 4, 5, 6]\nIn [3]: mapped_result = map(lambda x: x*2, data)\n\nIn [4]: mapped_result\nOut[4]: [2, 4, 6, 8, 10, 12]\n\nIn [10]: final_result = reduce(lambda x, y: x+y, mapped_result)\n\nIn [11]: final_result\nOut[11]: 42\n</code></pre>\n\n<p>See how each segment of raw data was processed individually, in this case, multiplied by 2 (the <strong>map</strong> part of MapReduce). Based on the <code>mapped_result</code>, we concluded that the result would be <code>42</code> (the <strong>reduce</strong> part of MapReduce).</p>\n\n<p>An important conclusion from this example is the fact that each chunk of processing doesn't depend on another chunk. For instance, if <code>thread_1</code> maps <code>[1, 2, 3]</code>, and <code>thread_2</code> maps <code>[4, 5, 6]</code>, the eventual result of both the threads would still be <code>[2, 4, 6, 8, 10, 12]</code> but we have <strong>halved</strong> the processing time for this. The same can be said for the reduce operation and is the essence of how MapReduce works in parallel computing.</p>\n"}]
['https://Stackoverflow.com/questions/28982', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2745/']