750 likes | 763 Views
This resource provides an overview of MapReduce, data types in Hadoop, input and output, shuffle and sort operations, and mapreduce algorithm design. It also covers graph algorithms, page rank, processing relational data, and aggregation and join operations.
E N D
MapReduce and Data Management Based on slides from Jimmy Lin’s lecture slides (http://www.umiacs.umd.edu/~jimmylin/cloud-2010-Spring/index.html) . Some ideas from Chapter 2 from the book by Anand Rajaraman and Jeff Ullman: "Mining of Massive Datasets“ (http://i.stanford.edu/~ullman/mmds.html)
Overview:- MapReduce ReCap- Data Types in Hadoop- Input and Output- Shuffle and Sort- MapReduce Algorithm Design: - Graph Algorithms - Page Rank - Processing Relational Data - Projection, Selection - Union, Intersection, Set Difference - GroupBy Aggregation, Relational Join
MapReduce: Recap Programmers must specify: map (k, v) → list(<k’, v’>) reduce (k’, list(v’)) → <k’’, v’’> All values with the same key are reduced together Optionally, also: partition (k’, number of partitions) → partition for k’ Often a simple hash of the key, e.g., hash(k’) mod n Divides up key space for parallel reduce operations combine (k’, v’) → <k’, v’>* Mini-reducers that run in memory after the map phase Used as an optimization to reduce network traffic The execution framework handles everything else…
k1 r2 Shuffle and Sort: aggregate values by keys r3 r1 b a b a a b a a c c c 7 5 5 7 1 1 9 3 s3 s1 s2 v1 2 2 1 c c c c 9 5 7 b b c k2 8 8 2 2 2 2 6 8 v2 k3 v3 k4 v4 k5 v5 k6 v6 partition partition partition partition combine combine combine combine reduce reduce reduce map map map map
“Everything Else” The execution framework handles everything else… Scheduling: assigns workers to map and reduce tasks “Data distribution”: moves processes to data Synchronization: gathers, sorts, and shuffles intermediate data Errors and faults: detects worker failures and restarts Limited control over data and execution flow All algorithms must expressed in m, r, c, p You don’t know: Where mappers and reducers run When a mapper or reducer begins or finishes Which input a particular mapper is processing Which intermediate key a particular reducer is processing
Tools for Synchronization Cleverly-constructed data structures Bring partial results together Sort order of intermediate keys Control order in which reducers process keys Partitioner Control which reducer processes which keys Preserving state in mappers and reducers Capture dependencies across multiple keys and values
Basic Hadoop API Mapper void map(K1 key, V1 value, OutputCollector<K2, V2> output, Reporter reporter) void configure(JobConf job) void close() throws IOException Reducer/Combiner void reduce(K2 key, Iterator<V2> values, OutputCollector<K3,V3> output, Reporter reporter) void configure(JobConf job) void close() throws IOException Partitioner void getPartition(K2 key, V2 value, int numPartitions) *Note: forthcoming API changes…
Data Types in Hadoop Writable Defines a de/serialization protocol. Every data type in Hadoop is a Writable. WritableComprable Defines a sort order. All keys must be of this type (but not values). Concrete classes for different data types. IntWritableLongWritable Text … Binary encoded of a sequence of key/value pairs SequenceFiles
Scalable Hadoop Algorithms: Themes Avoid object creation Inherently costly operation Garbage collection Avoid buffering Limited heap size Works for small datasets, but won’t scale!
Hadoop Map Reduce Example See the word count example from Hadoop Tutorial: http://hadoop.apache.org/common/docs/current/mapred_tutorial.html#Overview
Basic Cluster Components One of each: Namenode (NN) Jobtracker (JT) Set of each per slave machine: Tasktracker (TT) Datanode (DN)
Putting everything together… jobtracker datanode daemon Linux file system tasktracker slave node datanode daemon Linux file system tasktracker slave node datanode daemon Linux file system tasktracker slave node namenode namenode daemon job submission node … … …
Anatomy of a Job MapReduce program in Hadoop = Hadoop job Jobs are divided into map and reduce tasks An instance of running a task is called a task attempt Multiple jobs can be composed into a workflow Job submission process Client (i.e., driver program) creates a job, configures it, and submits it to job tracker JobClient computes input splits (on client end) Job data (jar, configuration XML) are sent to JobTracker JobTracker puts job data in shared location, enqueues tasks TaskTrackers poll for tasks Off to the races…
InputSplit RecordReader InputSplit InputSplit InputSplit RecordReader RecordReader InputSplit RecordReader Mapper Mapper Mapper Mapper Mapper RecordReader Input File Input File InputFormat Intermediates Intermediates Intermediates Intermediates Intermediates Source: redrawn from a slide by Cloduera, cc-licensed
Partitioner Partitioner Mapper Mapper Mapper Mapper Mapper Partitioner Reducer Partitioner Partitioner Reduce Reducer Intermediates Intermediates Intermediates Intermediates Intermediates (combiners omitted here) Intermediates Intermediates Intermediates Source: redrawn from a slide by Cloduera, cc-licensed
Reducer Reducer Reduce RecordWriter RecordWriter RecordWriter Output File Output File Output File OutputFormat Source: redrawn from a slide by Cloduera, cc-licensed
Input and Output InputFormat: TextInputFormat KeyValueTextInputFormat SequenceFileInputFormat … OutputFormat: TextOutputFormat SequenceFileOutputFormat …
Shuffle and Sort in Hadoop Probably the most complex aspect of MapReduce! Map side Map outputs are buffered in memory in a circular buffer When buffer reaches threshold, contents are “spilled” to disk Spills merged in a single, partitioned file (sorted within each partition): combiner runs here Reduce side First, map outputs are copied over to reducer machine “Sort” is a multi-pass merge of map outputs (happens in memory and on disk): combiner runs here Final merge pass goes directly into reducer
Shuffle and Sort Combiner Combiner Mapper Reducer intermediate files (on disk) mergedspills (on disk) circular buffer (in memory) other reducers spills(on disk) other mappers
Hadoop Workflow Hadoop Cluster 1. Load data into HDFS 2. Develop code locally 3. Submit MapReduce job 3a. Go back to Step 2 You 4. Retrieve data from HDFS
On Amazon: With EC2 0. Allocate Hadoop cluster 1. Load data into HDFS EC2 2. Develop code locally 3. Submit MapReduce job 3a. Go back to Step 2 Your Hadoop Cluster You 4. Retrieve data from HDFS 5. Clean up! Uh oh. Where did the data go?
On Amazon: EC2 and S3 Copy from S3 to HDFS S3(Persistent Store) EC2(The Cloud) Your Hadoop Cluster Copy from HFDS to S3
Graph Algorithms in MapReduce G = (V,E), where V represents the set of vertices (nodes) E represents the set of edges (links) Both vertices and edges may contain additional information
Graphs and MapReduce Graph algorithms typically involve: Performing computations at each node: based on node features, edge features, and local link structure Propagating computations: “traversing” the graph Key questions: How do you represent graph data in MapReduce? How do you traverse a graph in MapReduce?
Representing Graphs G = (V, E) Two common representations Adjacency matrix Adjacency list
Adjacency Matrices Represent a graph as an n x n square matrix M n = |V| Mij = 1 means a link from node i to j 4 1 3 2
Adjacency Matrices: Critique Advantages: Amenable to mathematical manipulation Iteration over rows and columns corresponds to computations on outlinks and inlinks Disadvantages: Lots of zeros for sparse matrices Lots of wasted space
Adjacency Lists Take adjacency matrices… and throw away all the zeros 1: 2, 4 2: 1, 3, 4 3: 1 4: 1, 3
Adjacency Lists: Critique Advantages: Much more compact representation Easy to compute over outlinks Disadvantages: Much more difficult to compute over inlinks
Finding the Shortest Path Consider simple case of equal edge weights Solution to the problem can be defined inductively Here’s the intuition: Define: b is reachable from a if b is on adjacency list of a DISTANCETO(s) = 0 For all nodes p reachable from s, DISTANCETO(p) = 1 For all nodes n reachable from some other set of nodes M, DISTANCETO(n) = 1 + min(DISTANCETO(m), m∈M)
Visualizing Parallel BFS n7 n4 n9 n2 n1 n5 n8 n0 n3 n6
From Intuition to Algorithm Data representation: Key: node n Value: d (distance from start), adjacency list (list of nodes reachable from n) Initialization: for all nodes except for start node, d = ∞ Mapper: ∀m∈ adjacency list: emit (m, d + 1) Sort/Shuffle Groups distances by reachable nodes Reducer: Selects minimum distance path for each reachable node Additional bookkeeping needed to keep track of actual path
Multiple Iterations Needed Each MapReduce iteration advances the “known frontier” by one hop Subsequent iterations include more and more reachable nodes as frontier expands Multiple iterations are needed to explore entire graph Preserving graph structure: Problem: Where did the adjacency list go? Solution: mapper emits (n, adjacency list) as well
Stopping Criterion How many iterations are needed in parallel BFS (equal edge weight case)? Convince yourself: when a node is first “discovered”, we’ve found the shortest path Now answer the question... Six degrees of separation?
Graphs and MapReduce Graph algorithms typically involve: Performing computations at each node: based on node features, edge features, and local link structure Propagating computations: “traversing” the graph Generic recipe: Represent graphs as adjacency lists Perform local computations in mapper Pass along partial results via outlinks, keyed by destination node Perform aggregation in reducer on inlinks to a node Iterate until convergence: controlled by external “driver” Don’t forget to pass the graph structure between iterations
Random Walks Over the Web Random surfer model: User starts at a random Web page User randomly clicks on links, surfing from page to page PageRank Characterizes the amount of time spent on any given page Mathematically, a probability distribution over pages PageRank captures notions of page importance One of thousands of features used in web search Note: query-independent
Given page x with inlinks t1…tn, where C(t) is the out-degree of t α is probability of random jump N is the total number of nodes in the graph PageRank: Defined
Computing PageRank Properties of PageRank Can be computed iteratively Effects at each iteration are local Sketch of algorithm: Start with seed PRi values Each page distributes PRi “credit” to all pages it links to Each target page adds up “credit” from multiple in-bound links to compute PRi+1 Iterate until values converge
Simplified PageRank First, tackle the simple case: No random jump factor No dangling links Then, factor in these complexities… Why do we need the random jump? Where do dangling links come from?
Sample PageRank Iteration (1) n2 (0.166) n1 (0.066) n5 (0.3) n3 (0.166) n4 (0.3) Iteration 1 n2 (0.2) 0.1 n1 (0.2) 0.1 0.1 0.1 0.066 0.066 0.066 n5 (0.2) n3 (0.2) 0.2 0.2 n4 (0.2)
Sample PageRank Iteration (2) n2 (0.133) n1 (0.1) n5 (0.383) n3 (0.183) n4 (0.2) Iteration 2 n2 (0.166) 0.033 0.083 n1 (0.066) 0.083 0.033 0.1 0.1 0.1 n5 (0.3) n3 (0.166) 0.3 0.166 n4 (0.3)
PageRank in MapReduce n3 n2 n1 n3 n2 n1 n5 n2 n4 n3 n3 n4 n5 n2 n4 n5 n4 n5 n1 [n2, n4] n2 [n3, n5] n3 [n4] n4 [n5] n5 [n1, n2, n3] Map Reduce n1 [n2, n4] n2 [n3, n5] n3 [n4] n4 [n5] n5 [n1, n2, n3]
Complete PageRank Two additional complexities What is the proper treatment of dangling nodes? How do we factor in the random jump factor? Solution: Second pass to redistribute “missing PageRank mass” and account for random jumps p is PageRank value from before, p' is updated PageRank value |G| is the number of nodes in the graph m is the missing PageRank mass
PageRank Convergence Alternative convergence criteria Iterate until PageRank values don’t change Iterate until PageRank rankings don’t change Fixed number of iterations Convergence for web graphs?
Beyond PageRank Link structure is important for web search PageRank is one of many link-based features: HITS, SALSA, etc. One of many thousands of features used in ranking… Adversarial nature of web search Link spamming Spider traps Keyword stuffing …
Efficient Graph Algorithms Sparse vs. dense graphs Graph topologies
Power Laws are everywhere! Figure from: Newman, M. E. J. (2005) “Power laws, Pareto distributions and Zipf's law.” Contemporary Physics 46:323–351.
Local Aggregation Use combiners! In-mapper combining design pattern also applicable Maximize opportunities for local aggregation Simple tricks: sorting the dataset in specific ways