How does the MapReduce sort algorithm work?

65,656

Solution 1

Here are some details on Hadoop's implementation for Terasort:

TeraSort is a standard map/reduce sort, except for a custom partitioner that uses a sorted list of N − 1 sampled keys that define the key range for each reduce. In particular, all keys such that sample[i − 1] <= key < sample[i] are sent to reduce i. This guarantees that the output of reduce i are all less than the output of reduce i+1."

So their trick is in the way they determine the keys during the map phase. Essentially they ensure that every value in a single reducer is guaranteed to be 'pre-sorted' against all other reducers.

I found the paper reference through James Hamilton's Blog Post.

Solution 2

Google Reference: MapReduce: Simplified Data Processing on Large Clusters

Appeared in:
OSDI'04: Sixth Symposium on Operating System Design and Implementation,
San Francisco, CA, December, 2004.

That link has a PDF and HTML-Slide reference.

There is also a Wikipedia page with description with implementation references.

Also criticism,

David DeWitt and Michael Stonebraker, pioneering experts in parallel databases and shared nothing architectures, have made some controversial assertions about the breadth of problems that MapReduce can be used for. They called its interface too low-level, and questioned whether it really represents the paradigm shift its proponents have claimed it is. They challenge the MapReduce proponents' claims of novelty, citing Teradata as an example of prior art that has existed for over two decades; they compared MapReduce programmers to Codasyl programmers, noting both are "writing in a low-level language performing low-level record manipulation". MapReduce's use of input files and lack of schema support prevents the performance improvements enabled by common database system features such as B-trees and hash partitioning, though projects such as PigLatin and Sawzall are starting to address these problems.

Solution 3

I had the same question while reading Google's MapReduce paper. @Yuval F 's answer pretty much solved my puzzle.

One thing I noticed while reading the paper is that the magic happens in the partitioning (after map, before reduce).

The paper uses hash(key) mod R as the partitioning example, but this is not the only way to partition intermediate data to different reduce tasks.

Just add boundary conditions to @Yuval F 's answer to make it complete: suppose min(S) and max(S) is the minimum key and maximum key among the sampled keys; all keys < min(S) are partitioned to one reduce task; vice versa, all keys >= max(S) are partitioned to one reduce task.

There is no hard limitation on the sampled keys, like min or max. Just, more evenly these R keys distributed among all the keys, more "parallel" this distributed system is and less likely a reduce operator has memory overflow issue.

Share:
65,656

Related videos on Youtube

Niels Basjes
Author by

Niels Basjes

IT Architect, Data Scientist, Inventor, Speaker, Teacher, Coder, Hadoop, Flink, Kafka, Avro, Cloud, etc.

Updated on July 08, 2022

Comments

  • Niels Basjes
    Niels Basjes almost 2 years

    One of the main examples that is used in demonstrating the power of MapReduce is the Terasort benchmark. I'm having trouble understanding the basics of the sorting algorithm used in the MapReduce environment.

    To me sorting simply involves determining the relative position of an element in relationship to all other elements. So sorting involves comparing "everything" with "everything". Your average sorting algorithm (quick, bubble, ...) simply does this in a smart way.

    In my mind splitting the dataset into many pieces means you can sort a single piece and then you still have to integrate these pieces into the 'complete' fully sorted dataset. Given the terabyte dataset distributed over thousands of systems I expect this to be a huge task.

    So how is this really done? How does this MapReduce sorting algorithm work?

    Thanks for helping me understand.

  • Niels Basjes
    Niels Basjes almost 15 years
    I understand (most of) the concepts of MapReduce as described in the mentioned documents. I'm trying to understand the sorting algorithm.