Top Banner
MapReduce Programming
93

MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Dec 29, 2015

Download

Documents

Cory Todd
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

MapReduce Programming

Page 2: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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 operationscombine (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…

Page 3: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

combinecombine combine combine

ba 1 2 c 9 a c5 2 b c7 8

partition partition partition partition

mapmap map map

k1 k2 k3 k4 k5 k6v1 v2 v3 v4 v5 v6

ba 1 2 c c3 6 a c5 2 b c7 8

Shuffle and Sort: aggregate values by keys

reduce reduce reduce

a 1 5 b 2 7 c 2 9 8

r1 s1 r2 s2 r3 s3

Page 4: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

“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

Page 5: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

Page 6: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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…

Page 7: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Data Types in HadoopWritable Defines a de/serialization protocol.

Every data type in Hadoop is a Writable.

WritableComparable Defines a sort order. All keys must be of this type (but not values).

IntWritableLongWritableText…

Concrete classes for different data types.

SequenceFiles Binary encoded of a sequence of key/value pairs

Page 8: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Hadoop Map Reduce Example

• See the word count example from Hadoop Tutorial

Page 9: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Word Count in Java public class MapClass extends MapReduceBase implements Mapper<LongWritable, Text, Text,

IntWritable> { private final static IntWritable ONE = new

IntWritable(1); public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> out, Reporter reporter) throws IOException { String line = value.toString(); StringTokenizer itr = new StringTokenizer(line); while (itr.hasMoreTokens()) { out.collect(new text(itr.nextToken()), ONE); } } }

Page 10: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Word Count in Java public class ReduceClass extends MapReduceBase implements Reducer<Text, IntWritable, Text,

IntWritable> { public void reduce(Text key, Iterator<IntWritable>

values, OutputCollector<Text, IntWritable>

out, Reporter reporter) throws

IOException { int sum = 0; while (values.hasNext()) { sum += values.next().get(); } out.collect(key, new IntWritable(sum)); } }

Page 11: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Word Count in Java public static void main(String[] args) throws Exception { JobConf conf = new JobConf(WordCount.class); conf.setJobName("wordcount");

conf.setMapperClass(MapClass.class); conf.setCombinerClass(ReduceClass.class); conf.setReducerClass(ReduceClass.class); FileInputFormat.setInputPaths(conf, args[0]); FileOutputFormat.setOutputPath(conf, new Path(args[1]));

conf.setOutputKeyClass(Text.class); // out keys are words (strings)

conf.setOutputValueClass(IntWritable.class); // values are counts JobClient.runJob(conf); }

Page 12: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Word Count in Python with Hadoop Streaming

import sysfor line in sys.stdin: for word in line.split(): print(word.lower() + "\t" + 1)

import syscounts = {}for line in sys.stdin: word, count = line.split("\t”) dict[word] = dict.get(word, 0) +

int(count)for word, count in counts: print(word.lower() + "\t" + 1)

Mapper.py:

Reducer.py:

Page 13: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Basic Cluster Components

• One of each:– Namenode (NN)– Jobtracker (JT)

• Set of each per slave machine:– Tasktracker (TT)– Datanode (DN)

Page 14: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Putting everything together…

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

jobtracker

Page 15: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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…

Page 16: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Source: redrawn from a slide by Cloduera, cc-licensed

Mapper Mapper Mapper Mapper Mapper

Partitioner Partitioner Partitioner Partitioner Partitioner

Intermediates Intermediates Intermediates Intermediates Intermediates

Reducer Reducer Reduce

Intermediates Intermediates Intermediates

(combiners omitted here)

Page 17: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Source: redrawn from a slide by Cloduera, cc-licensed

Reducer Reducer Reduce

Output File

RecordWriter

Ou

tpu

tFo

rmat

Output File

RecordWriter

Output File

RecordWriter

Page 18: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Input and Output• InputFormat:

– TextInputFormat

– KeyValueTextInputFormat

– SequenceFileInputFormat

– …

• OutputFormat:– TextOutputFormat

– SequenceFileOutputFormat

– …

Page 19: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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 appropriate reducer machine

– “Sort” is a multi-pass merge of map outputs (happens in memory and on disk): partitioner runs here

– Final merge pass goes directly into reducer

Page 20: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Shuffle and Sort

Mapper

Reducer

other mappers

other reducers

circular buffer (in memory)

spills (on disk)

merged spills (on disk)

intermediate files (on disk)

Combiner

Partitioner

Page 21: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

Page 22: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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?

Page 23: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Representing Graphs

• G = (V, E)

• Two common representations– Adjacency matrix– Adjacency list

Page 24: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

1 2 3 41 0 1 0 12 1 0 1 13 1 0 0 04 1 0 1 0

1

2

3

4

Page 25: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

Page 26: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Adjacency Lists

Take adjacency matrices… and throw away all the zeros

1: 2, 42: 1, 3, 43: 14: 1, 3

1 2 3 41 0 1 0 12 1 0 1 13 1 0 0 04 1 0 1 0

Page 27: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Adjacency Lists: Critique

• Advantages:– Much more compact representation– Easy to compute over outlinks

• Disadvantages:– Much more difficult to compute over inlinks

Page 28: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Single Source Shortest Path

• Problem: find shortest path from a source node to one or more target nodes– Shortest might also mean lowest weight or cost

• First, a refresher: Dijkstra’s Algorithm

Page 29: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Dijkstra’s Algorithm Example

0

10

5

2 3

2

1

9

7

4 6

Example from CLR

Page 30: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Dijkstra’s Algorithm Example

0

10

5

Example from CLR

10

5

2 3

2

1

9

7

4 6

Page 31: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Dijkstra’s Algorithm Example

0

8

5

14

7

Example from CLR

10

5

2 3

2

1

9

7

4 6

Page 32: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Dijkstra’s Algorithm Example

0

8

5

13

7

Example from CLR

10

5

2 3

2

1

9

7

4 6

Page 33: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Dijkstra’s Algorithm Example

0

8

5

9

7

1

Example from CLR

10

5

2 3

2

1

9

7

4 6

Page 34: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Dijkstra’s Algorithm Example

0

8

5

9

7

Example from CLR

10

5

2 3

2

1

9

7

4 6

Page 35: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Single Source Shortest Path

• Problem: find shortest path from a source node to one or more target nodes– Shortest might also mean lowest weight or cost

• Single processor machine: Dijkstra’s Algorithm

• MapReduce: parallel Breadth-First Search (BFS)

Page 36: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Source: Wikipedia (Wave)

Page 37: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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)

s

m3

m2

m1

n

d1

d2

d3

Page 38: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Visualizing Parallel BFS

n0

n3n2

n1

n7

n6

n5

n4

n9

n8

Page 39: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

Page 40: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

Page 41: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

BFS Pseudo-Code

Page 42: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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?

• Practicalities of implementation in MapReduce

Page 43: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Comparison to Dijkstra

• Dijkstra’s algorithm is more efficient – At any step it only pursues edges from the

minimum-cost path inside the frontier

• MapReduce explores all paths in parallel– Lots of “waste”– Useful work is only done at the “frontier”

• Why can’t we do better using MapReduce?

Page 44: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Weighted Edges

• Now add positive weights to the edges– Why can’t edge weights be negative?

• Simple change: adjacency list now includes a weight w for each edge– In mapper, emit (m, d + wp) instead of (m, d +

1) for each node m

• That’s it?

Page 45: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Stopping Criterion

• How many iterations are needed in parallel BFS (positive edge weight case)?

• Convince yourself: when a node is first “discovered”, we’ve found the shortest path

Not true!

Page 46: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Additional Complexities

s

pq

r

search frontier

10

n1

n2

n3

n4

n5

n6 n7

n8

n9

1

11

1

1

11

1

Page 47: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Stopping Criterion

• How many iterations are needed in parallel BFS (positive edge weight case)?

• Practicalities of implementation in MapReduce

Page 48: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

Page 49: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

n

i i

i

tC

tPR

NxPR

1 )(

)()1(

1)(

X

t1

t2

tn

Page 50: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Example

Yahoo

M’softAmazon

y 1/2 1/2 0a 1/2 0 1m 0 1/2 0

y a m

Page 51: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Simulating a Random Walk

• Start with the vector v = [1,1,…,1] representing the idea that each Web page is given one unit of importance.

• Repeatedly apply the matrix M to v, allowing the importance to flow like a random walk.

• Limit exists, but about 50 iterations is sufficient to estimate final distribution.

Page 52: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Example

• Equations v = M v :y = y /2 + a /2a = y /2 + mm = a /2

ya =m

111

13/21/2

5/4 13/4

9/811/81/2

6/56/53/5

. . .

Page 53: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Solving The Equations

• Because there are no constant terms, these 3 equations in 3 unknowns do not have a unique solution.

• Add in the fact that y +a +m = 3 to solve.• In Web-sized examples, we cannot solve by

Gaussian elimination; we need to use relaxation (= iterative solution).

Page 54: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Real-World Problems

• Some pages are “dead ends” (have no links out).– Such a page causes importance to leak out.

• Other (groups of) pages are spider traps (all out-links are within the group).– Eventually spider traps absorb all importance.

Page 55: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Microsoft Becomes Dead End

Yahoo

M’softAmazon

y 1/2 1/2 0a 1/2 0 0m 0 1/2 0

y a m

Page 56: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Example

• Equations v = M v :y = y /2 + a /2a = y /2m = a /2

ya =m

111

11/21/2

3/41/21/4

5/83/81/4

000

. . .

Page 57: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

M’soft Becomes Spider Trap

Yahoo

M’softAmazon

y 1/2 1/2 0a 1/2 0 0m 0 1/2 1

y a m

Page 58: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Example

• Equations v = M v :y = y /2 + a /2a = y /2m = a /2 + m

ya =m

111

11/23/2

3/41/27/4

5/83/82

003

. . .

Page 59: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Google Solution to Traps, Etc.

• “Tax” each page a fixed percentage at each interation.

• Add the same constant to all pages.• Models a random walk with a fixed

probability of going to a random place next.

Page 60: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Example: Previous with 20% Tax• Equations v = 0.8(M v ) + 0.2:

y = 0.8(y /2 + a/2) + 0.2a = 0.8(y /2) + 0.2m = 0.8(a /2 + m) + 0.2

ya =m

111

1.000.601.40

0.840.601.56

0.7760.5361.688

7/11 5/1121/11

. . .

Page 61: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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

Page 62: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Sample PageRank Iteration (1)

n1 (0.2)

n4 (0.2)

n3 (0.2)n5 (0.2)

n2 (0.2)

0.1

0.1

0.2 0.2

0.1 0.1

0.066 0.0660.066

n1 (0.066)

n4 (0.3)

n3 (0.166)n5 (0.3)

n2 (0.166)Iteration 1

Page 63: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Sample PageRank Iteration (2)

n1 (0.066)

n4 (0.3)

n3 (0.166)n5 (0.3)

n2 (0.166)

0.033

0.033

0.3 0.166

0.083 0.083

0.1 0.10.1

n1 (0.1)

n4 (0.2)

n3 (0.183)n5 (0.383)

n2 (0.133)Iteration 2

Page 64: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

PageRank in MapReduce

n5 [n1, n2, n3]n1 [n2, n4] n2 [n3, n5] n3 [n4] n4 [n5]

n2 n4 n3 n5 n1 n2 n3n4 n5

n2 n4n3 n5n1 n2 n3 n4 n5

n5 [n1, n2, n3]n1 [n2, n4] n2 [n3, n5] n3 [n4] n4 [n5]

Map

Reduce

Page 65: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

PageRank Pseudo-Code

Page 66: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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?

Page 67: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

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– …

Page 68: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Mapreduce and Databases

Page 69: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Relational Databases vs. MapReduce• Relational databases:

– Multipurpose: analysis and transactions; batch and interactive

– Data integrity via ACID transactions

– Lots of tools in software ecosystem (for ingesting, reporting, etc.)

– Supports SQL (and SQL integration, e.g., JDBC)

– Automatic SQL query optimization

• MapReduce (Hadoop):

– Designed for large clusters, fault tolerant

– Data is accessed in “native format”

– Supports many query languages

– Programmers retain control over performance

– Open source

Source: O’Reilly Blog post by Joseph Hellerstein (11/19/2008)

Page 70: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Database Workloads• OLTP (online transaction processing)

– Typical applications: e-commerce, banking, airline reservations– User facing: real-time, low latency, highly-concurrent

– Tasks: relatively small set of “standard” transactional queries– Data access pattern: random reads, updates, writes (involving

relatively small amounts of data)• OLAP (online analytical processing)

– Typical applications: business intelligence, data mining– Back-end processing: batch workloads, less concurrency

– Tasks: complex analytical queries, often ad hoc– Data access pattern: table scans, large amounts of data involved per

query

Page 71: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

OLTP/OLAP Architecture

OLTP OLAP

ETL(Extract, Transform, and Load)

Page 72: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

OLTP/OLAP Integration

• OLTP database for user-facing transactions

– Retain records of all activity

– Periodic ETL (e.g., nightly)

• Extract-Transform-Load (ETL)

– Extract records from source

– Transform: clean data, check integrity, aggregate, etc.

– Load into OLAP database

• OLAP database for data warehousing

– Business intelligence: reporting, ad hoc queries, data mining, etc.

– Feedback to improve OLTP services

Page 73: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

OLTP/OLAP/Hadoop Architecture

OLTP OLAP

ETL(Extract, Transform, and Load)

Hadoop

Why does this make sense?

Page 74: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

ETL Bottleneck

• Reporting is often a nightly task:– ETL is often slow: why?– What happens if processing 24 hours of data takes longer than 24

hours?• Hadoop is perfect:

– Most likely, you already have some data warehousing solution– Ingest is limited by speed of HDFS– Scales out with more nodes– Massively parallel– Ability to use any processing tool– Much cheaper than parallel databases– ETL is a batch process anyway!

Page 75: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

MapReduce algorithms for processing relational data

Page 76: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Relational Algebra

• Primitives

– Projection ()

– Selection ()

– Cartesian product ()

– Set union ()

– Set difference ()– Rename ()

• Other operations

– Join ( )⋈– Group by… aggregation

– …

Page 77: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Projection

R1

R2

R3

R4

R5

R1

R2

R3

R4

R5

Page 78: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Projection in MapReduce• Easy!

– Map over tuples, emit new tuples with appropriate attributes

– Reduce: take tuples that appear many times and emit only one version (duplicate elimination)

• Tuple t in R: Map(t, t) -> (t’,t’)

• Reduce (t’, [t’, …,t’]) -> [t’,t’]

• Basically limited by HDFS streaming speeds

– Speed of encoding/decoding tuples becomes important

– Relational databases take advantage of compression

– Semistructured data? No problem!

Page 79: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Selection

R1

R2

R3

R4

R5

R1

R3

Page 80: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Selection in MapReduce

• Easy!

– Map over tuples, emit only tuples that meet criteria

– No reducers, unless for regrouping or resorting tuples (reducers are the identity function)

– Alternatively: perform in reducer, after some other processing

• But very expensive!!! Has to scan the database– Better approaches?

Page 81: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Union, Set Intersection and Set Difference

• Similar ideas: each map outputs the tuple pair (t,t). For union, we output it once, for intersection only when in the reduce we have (t, [t,t])

• For Set difference?

Page 82: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Set Difference

- Map Function: For a tuple t in R, produce key-value pair (t, R), and for a tuple t in S, produce key-value pair (t, S).

- Reduce Function: For each key t, do the following.

1. If the associated value list is [R], then produce (t, t).

2. If the associated value list is anything else, which could only be [R, S], [S, R], or [S], produce (t, NULL).

Page 83: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Group by… Aggregation

• Example: What is the average time spent per URL?

• In SQL:

– SELECT url, AVG(time) FROM visits GROUP BY url

• In MapReduce:

– Map over tuples, emit time, keyed by url

– Framework automatically groups values by keys

– Compute average in reducer

– Optimize with combiners

Page 84: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Relational JoinsR1

R2

R3

R4

S1

S2

S3

S4

R1 S2

R2 S4

R3 S1

R4 S3

Page 85: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Join Algorithms in MapReduce

• Reduce-side join

• Map-side join

• In-memory join– Striped variant– Memcached variant

Page 86: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Reduce-side Join

• Basic idea: group by join key

– Map over both sets of tuples

– Emit tuple as value with join key as the intermediate key

– Execution framework brings together tuples sharing the same key

– Perform actual join in reducer

– Similar to a “sort-merge join” in database terminology

Page 87: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Map-Reduce Join

87

Page 88: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Map-side Join: Parallel Scans

• If datasets are sorted by join key, join can be accomplished by a scan over both datasets

• How can we accomplish this in parallel?

– Partition and sort both datasets in the same manner

• In MapReduce:

– Map over one dataset, read from other corresponding partition

– No reducers necessary (unless to repartition or resort)

• Consistently partitioned datasets: realistic to expect?

Page 89: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

In-Memory Join• Basic idea: load one dataset into memory, stream over other dataset

– Works if R << S and R fits into memory

– Called a “hash join” in database terminology

• MapReduce implementation

– Distribute R to all nodes

– Map over S, each mapper loads R in memory, hashed by join key

– For every tuple in S, look up join key in R

– No reducers, unless for regrouping or resorting tuples

Page 90: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

In-Memory Join: Variants• Striped variant:

– R too big to fit into memory?

– Divide R into R1, R2, R3, … s.t. each Rn fits into memory

– Perform in-memory join: n, Rn S⋈– Take the union of all join results

• Memcached join:– Load R into memcached

– Replace in-memory hash lookup with memcached lookup

Page 91: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Memcached Join• Memcached join:

– Load R into memcached

– Replace in-memory hash lookup with memcached lookup

• Capacity and scalability?

– Memcached capacity >> RAM of individual node

– Memcached scales out with cluster

• Latency?

– Memcached is fast (basically, speed of network)

– Batch requests to amortize latency costs

Source: See tech report by Lin et al. (2009)

Page 92: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Which join to use?

• In-memory join > map-side join > reduce-side join– Why?

• Limitations of each?– In-memory join: memory– Map-side join: sort order and partitioning– Reduce-side join: general purpose

Page 93: MapReduce Programming. MapReduce: Recap Programmers must specify: map (k, v) → list( ) reduce (k’, list(v’)) → –All values with the same key are reduced.

Processing Relational Data: Summary

• MapReduce algorithms for processing relational data:

– Group by, sorting, partitioning are handled automatically by shuffle/sort in MapReduce

– Selection, projection, and other computations (e.g., aggregation), are performed either in mapper or reducer

– Multiple strategies for relational joins

• Complex operations require multiple MapReduce jobs

– Example: top ten URLs in terms of average time spent

– Opportunities for automatic optimization