Map Reduce
Map Reduce
Debapriyo Majumdar
Data Mining – Fall 2014
Indian Statistical Institute Kolkata
Traditional parallelism
MapReduce
Final
original output
Input Input <Key,Value>
chunks pairs
<Key,Value> Output May not
May be pairs grouped chunks need to
already by keys combine
split in
filesystem The user needs to write the map() and the reduce() 3
An example: word frequency counting
Split Map Shuffle and sort Reduce Final
each w
Final
original output
Input Input <Key,Value>
chunks pairs
<Key,Value> Output
Problem: Given a collection
of documents, count the
pairs grouped chunks
number of times each by keys
word occurs in the map: for each word w, reduce: count the number (n) of
collection emit pairs (w,1) pairs for each w, make it (w,n) 4
An example: word frequency counting
Split Map Shuffle and sort Reduce Final
(apple,1)
(apple,2)
(apple,1)
apple (apple,1)
orange (orange,1) (orange,1)
apple orange peach (peach,1) (orange,1) (orange,3)
peach (orange,1)
orange (orange,1)
plum (plum,1) (guava,1) (guava,1) (apple,2)
orange plum
(orange,3)
orange (orange,1) (guava,1)
orange apple (plum,1)
apple (apple,1) (plum,2) (plum,2)
guava (plum,1)
guava (guava,1) (cherry,2)
(fig,2)
cherry fig (cherry,1) (cherry,1)
cherry fig (cherry,2) (peach,3)
(fig,1) (cherry,1)
peach fig
peach (peach,1) (fig,1)
peach fig
(fig,1) (fig,1)
(fig,2) Final
original peach
(peach,1) (peach,1) output
Input Input <Key,Value> (peach,1) (peach,3)
(peach,1)
chunks pairs
<Key,Value> Output
Problem: Given a collection
of documents, count the
pairs grouped chunks
number of times each by keys
word occurs in the map: for each word w, reduce: count the number (n) of
collection output pairs (w,1) pairs for each w, make it (w,n) 5
Apache Hadoop
An open source MapReduce framework
HADOOP
6
Hadoop
Two main components
– Hadoop Distributed File System (HDFS): to store data
– MapReduce engine: to process data
Master – slave architecture using commodity servers
The HDFS
– Master: Namenode
– Slave: Datanode
MapReduce
– Master: JobTracker
– Slave: TaskTracker
7
HDFS: Blocks
Datanode 1
Block 1 Block 2
Block 1 Block 2 Block 3
Datanode 2
Block 1 Block 3
Big File Block 3 Block 4 Block 4
Datanode 3
Block 2 Block 6
Block 5 Block 6
Block 5
Datanode 4
Runs on top of existing filesystem
Blocks are 64MB (128MB recommended) Block 4 Block 6
Single file can be > any single disk Block 5
POSIX based permissions
8
Fault tolerant
HDFS: Namenode and Datanode
Namenode
– Only one per Hadoop Cluster
– Manages the filesystem namespace
– The filesystem tree
– An edit log
– For each block block i, the datanode(s) in which block i is saved
– All the blocks residing in each datanode
Secondary Namenode
– Backup namenode
Datanodes
– Many per Hadoop cluster
– Controls block operations
– Physically puts the block in the nodes
– Do the physical replication
9
HDFS: an example
10
MapReduce: JobTracker and TaskTracker
12
Fault Tolerance
If the master fails
– MapReduce would fail, have to restart the entire job
A map worker node fails
– Master detects (periodic ping would timeout)
– All the map tasks for this node have to be restarted
• Even if the map tasks were done, the output were at the node
A reduce worker fails
– Master sets the status of its currently executing reduce
tasks to idle
– Reschedule these tasks on another reduce worker
13
Some algorithms using MapReduce
USING MAPREDUCE
14
Matrix – Vector Multiplication
Multiply M = (mij) (an n × n matrix) and v = (vi) (an n-vector)
If n = 1000, no need of MapReduce!
n
M v n
Case 1: Large n, M does not fit into main memory, but v does
Since v fits into main memory, v is available to every map task
Map: for each matrix element mij, emit key value pair (i, mijvj)
Shuffle and sort: groups all mijvj values together for the same i
Reduce: sum mijvj for all j for the same i
15
Matrix – Vector Multiplication
Multiply M = (mij) (an n × n matrix) and v = (vi) (an n-vector)
If n = 1000, no need of MapReduce!
This much will fit into main
memory
Case 2: Very large n, even v does not fit into main memory
For every map, many accesses to disk (for parts of v) required!
Solution:
– How much of v will fit in?
– Partition v and rows of M so that each partition of v fits into memory
– Take dot product of one partition of v and the corresponding partition of M
– Map and reduce same as before
16
Relational Alegebra
Relation R(A1, A3, …, An) is Attr1 Attr2 Attr3 Attr4
xyz abc 1 true
a relation with attributes Ai
abc xyz 1 true
Schema: set of attributes xyz def 1 false
Selection on condition C: bcd def 2 true
apply C on each tuple in R,
output only those which
satisfy C Links between URLs
17
Selection using MapReduce
Trivial Links between URLs
Map: For each tuple t in R, test if t URL1 URL2
satisfies C. If so, produce the key-value url1 url2
pair (t, t). url2 url1
Reduce: The identity function. It simply url3 url5
passes each key-value pair to the output. url1 url3
18
Union using MapReduce
Union of two relations R and S Links between URLs
Suppose R and S have the same schema URL1 URL2
Map tasks are generated from chunks of url1 url2
both R and S url2 url1
Map: For each tuple t, produce the key- url3 url5
value pair (t, t) url1 url3
Reduce: Only need to remove duplicates
– For all key t, there would be either one or
two values
– Output (t, t) in either case
19
Natural join using MapReduce
Join R(A,B) with S(B,C) on attribute B R
Map:
A B
– For each tuple t = (a,b) of R, emit key value pair
(b,(R,a)) x a
– For each tuple t = (b,c) of S, emit key value pair y b
(b,(S,c)) z c
Reduce: w d
– Each key b would be associated with a list of
values that are of the form (R,a) or (S,c)
S
– Construct all pairs consisting of one with first
component R and the other with first component B C
S , say (R,a ) and (S,c ). The output from this key a 1
and value list is a sequence of key-value pairs c 3
– The key is irrelevant. Each value is one of the
d 4
triples (a, b, c ) such that (R,a ) and (S,c) are on
the input list of values g 7
20
Grouping and Aggregation using MapReduce
Group and aggregate on a relation R
m A n = C
B m
(m × n) (m × l)
(n × l)
26