You are on page 1of 52

UC Berkeley

Introduction to MapReduce and Hadoop


Matei Zaharia
UC Berkeley RAD Lab
matei@eecs.berkeley.edu

What is MapReduce?
Data-parallel programming model for clusters of commodity machines Pioneered by Google
Processes 20 PB of data per day

Popularized by open-source Hadoop project


Used by Yahoo!, Facebook, Amazon,

What is MapReduce used for?


At Google:
Index building for Google Search Article clustering for Google News Statistical machine translation

At Yahoo!:
Index building for Yahoo! Search Spam detection for Yahoo! Mail

At Facebook:
Data mining Ad optimization Spam detection

Example: Facebook Lexicon

www.facebook.com/lexicon

Example: Facebook Lexicon

www.facebook.com/lexicon

What is MapReduce used for?


In research:
Analyzing Wikipedia conflicts (PARC) Natural language processing (CMU) Bioinformatics (Maryland) Astronomical image analysis (Washington) Ocean climate simulation (Washington) <Your application here>

Outline
MapReduce architecture Fault tolerance in MapReduce Sample applications Getting started with Hadoop Higher-level languages on top of Hadoop: Pig and Hive

MapReduce Design Goals


1. Scalability to large data volumes:
Scan 100 TB on 1 node @ 50 MB/s = 23 days Scan on 1000-node cluster = 33 minutes

2. Cost-efficiency:
Commodity nodes (cheap, but unreliable) Commodity network Automatic fault-tolerance (fewer admins) Easy to use (fewer programmers)

Typical Hadoop Cluster


Aggregation switch Rack switch

40 nodes/rack, 1000-4000 nodes in cluster 1 GBps bandwidth in rack, 8 GBps out of rack Node specs (Yahoo terasort): 8 x 2.0 GHz cores, 8 GB RAM, 4 disks (= 4 TB?)

Typical Hadoop Cluster

Image from http://wiki.apache.org/hadoop-data/attachments/HadoopPresentations/attachments/aw-apachecon-eu-2009.pdf

Challenges
Cheap nodes fail, especially if you have many
Mean time between failures for 1 node = 3 years MTBF for 1000 nodes = 1 day Solution: Build fault-tolerance into system

Commodity network = low bandwidth


Solution: Push computation to the data

Programming distributed systems is hard


Solution: Data-parallel programming model: users write map and reduce functions, system handles work distribution and fault tolerance

Hadoop Components
Distributed file system (HDFS)
Single namespace for entire cluster Replicates data 3x for fault-tolerance

MapReduce implementation
Executes user jobs specified as map and reduce functions Manages work distribution & fault-tolerance

Hadoop Distributed File System


Files split into 128MB blocks Blocks replicated across several datanodes (usually 3) Single namenode stores metadata (file names, block locations, etc) Optimized for large files, sequential reads Files are append-only

Namenode
File1
1 2 3 4

1 2 4

2 1 3

1 4 3

3 2 4

Datanodes

MapReduce Programming Model


Data type: key-value records Map function: (Kin, Vin) list(Kinter, Vinter) Reduce function: (Kinter, list(Vinter)) list(Kout, Vout)

Example: Word Count


def mapper(line): foreach word in line.split(): output(word, 1) def reducer(key, values): output(key, sum(values))

Word Count Execution


Input Map Shuffle & Sort Reduce Output
the, 1 brown, 1 fox, 1

the quick brown fox

Map

Reduce
the, 1 fox, 1 the, 1

brown, 2 fox, 2 how, 1 now, 1 the, 3

the fox ate the mouse

Map
quick, 1 how, 1 now, 1 brown, 1

ate, 1 mouse, 1 cow, 1

Reduce

how now brown cow

Map

ate, 1 cow, 1 mouse, 1 quick, 1

MapReduce Execution Details


Single master controls job execution on multiple slaves as well as user scheduling Mappers preferentially placed on same node or same rack as their input block
Push computation to data, minimize network use

Mappers save outputs to local disk rather than pushing directly to reducers
Allows having more reducers than nodes Allows recovery if a reducer crashes

An Optimization: The Combiner


A combiner is a local aggregation function for repeated keys produced by same map For associative ops. like sum, count, max Decreases size of intermediate data Example: local counting for Word Count:
def combiner(key, values): output(key, sum(values))

Word Count with Combiner


Input Map & Combine Shuffle & Sort Reduce Output
the, 1 brown, 1 fox, 1

the quick brown fox

Map

Reduce
the, 2 fox, 1

brown, 2 fox, 2 how, 1 now, 1 the, 3

the fox ate the mouse

Map
quick, 1 how, 1 now, 1 brown, 1

ate, 1 mouse, 1 cow, 1

Reduce

how now brown cow

Map

ate, 1 cow, 1 mouse, 1 quick, 1

Outline
MapReduce architecture Fault tolerance in MapReduce Sample applications Getting started with Hadoop Higher-level languages on top of Hadoop: Pig and Hive

Fault Tolerance in MapReduce


1. If a task crashes:
Retry on another node
Okay for a map because it had no dependencies Okay for reduce because map outputs are on disk

If the same task repeatedly fails, fail the job or ignore that input block (user-controlled)

Note: For this and the other fault tolerance features to work, your map and reduce tasks must be side-effect-free

Fault Tolerance in MapReduce


2. If a node crashes:
Relaunch its current tasks on other nodes Relaunch any maps the node previously ran
Necessary because their output files were lost along with the crashed node

Fault Tolerance in MapReduce


3. If a task is going slowly (straggler):
Launch second copy of task on another node Take the output of whichever copy finishes first, and kill the other one

Critical for performance in large clusters: stragglers occur frequently due to failing hardware, bugs, misconfiguration, etc

Takeaways
By providing a data-parallel programming model, MapReduce can control job execution in useful ways:
Automatic division of job into tasks Automatic placement of computation near data Automatic load balancing Recovery from failures & stragglers

User focuses on application, not on complexities of distributed computing

Outline
MapReduce architecture Fault tolerance in MapReduce Sample applications Getting started with Hadoop Higher-level languages on top of Hadoop: Pig and Hive

1. Search
Input: (lineNumber, line) records Output: lines matching a given pattern Map:
if(line matches pattern): output(line)

Reduce: identify function


Alternative: no reducer (map-only job)

2. Sort
Input: (key, value) records Output: same records, sorted by key
Map: identity function Reduce: identify function Trick: Pick partitioning function h such that k1<k2 => h(k1)<h(k2)
Map
zebra cow ant, bee

Reduce [A-M]
aardvark ant bee cow elephant

Map
pig aardvark, elephant

Reduce [N-Z]
sheep, yak pig sheep yak zebra

Map

3. Inverted Index
Input: (filename, text) records Output: list of files containing each word Map:
foreach word in text.split(): output(word, filename)

Combine: uniquify filenames for each word Reduce:


def reduce(word, filenames): output(word, sort(filenames))

Inverted Index Example


hamlet.txt to be or not to be to, hamlet.txt be, hamlet.txt or, hamlet.txt not, hamlet.txt

12th.txt
be not afraid of greatness

be, 12th.txt not, 12th.txt afraid, 12th.txt of, 12th.txt greatness, 12th.txt

afraid, (12th.txt) be, (12th.txt, hamlet.txt) greatness, (12th.txt) not, (12th.txt, hamlet.txt) of, (12th.txt) or, (hamlet.txt) to, (hamlet.txt)

4. Most Popular Words


Input: (filename, text) records Output: the 100 words occurring in most files Two-stage solution:
Job 1:
Create inverted index, giving (word, list(file)) records

Job 2:
Map each (word, list(file)) to (count, word) Sort these records by count as in sort job

Optimizations:
Map to (word, 1) instead of (word, file) in Job 1 Estimate count distribution in advance by sampling

Outline
MapReduce architecture Fault tolerance in MapReduce Sample applications Getting started with Hadoop Higher-level languages on top of Hadoop: Pig and Hive

Getting Started with Hadoop


Download from hadoop.apache.org To install locally, unzip and set JAVA_HOME Details: hadoop.apache.org/core/docs/current/quickstart.html Three ways to write jobs:
Java API Hadoop Streaming (for Python, Perl, etc) Pipes API (C++)

Word Count in Java


public static 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> output, Reporter reporter) throws IOException { String line = value.toString(); StringTokenizer itr = new StringTokenizer(line); while (itr.hasMoreTokens()) { output.collect(new text(itr.nextToken()), ONE); } } }

Word Count in Java


public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { int sum = 0; while (values.hasNext()) { sum += values.next().get(); } output.collect(key, new IntWritable(sum)); } }

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(Reduce.class); conf.setReducerClass(Reduce.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); }

Word Count in Python with Hadoop Streaming


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

Reducer.py:

import sys counts = {} 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)

Outline
MapReduce architecture Fault tolerance in MapReduce Sample applications Getting started with Hadoop Higher-level languages on top of Hadoop: Pig and Hive

Motivation
MapReduce is great, as many algorithms can be expressed by a series of MR jobs But its low-level: must think about keys, values, partitioning, etc Can we capture common job patterns?

Pig
Started at Yahoo! Research Now runs about 30% of Yahoo!s jobs Features:
Expresses sequences of MapReduce jobs Data model: nested bags of items Provides relational (SQL) operators (JOIN, GROUP BY, etc) Easy to plug in Java functions Pig Pen dev. env. for Eclipse

An Example Problem
Suppose you have user data in one file, website data in another, and you need to find the top 5 most visited pages by users aged 18 - 25.
Load Users Filter by age Join on name Group on url Count clicks Order by clicks Take top 5
Example from http://wiki.apache.org/pig-data/attachments/PigTalksPapers/attachments/ApacheConEurope09.ppt

Load Pages

In MapReduce
i i i i m m m m p p p p o o o o r r r r t t t t j j j j a a a a v v v v a a a a . . . . i u u u o t t t . i i i I l l l O . . . E A I L x r t i c r e s e a r t p t i o n ; y L i s t ; a t o r ; ; r e p o r t e r . s e t S t a t u s ( " O K " ) ; } / / f o r D o t h e c ( S t r i n g f o r ( S t S t r o c . r e p r o s s 1 r i n i n g c o l o r t s : g p r o d u c t f i r s t ) s 2 : s e o u t v a l = l e c t ( n u l l e r . s e t S t a { c o k , t u a n d n e n s i m p o r t o r g . a p a c h e . h a d o o p . f s . P a t h ; i m p o r t o r g . a p a c h e . h a d o o p . i o . L o n g W r i t a b l e ; i m p o r t o r g . a p a c h e . h a d o o p . i o . T e x t ; i m p o r t o r g . a p a c h e . h a d o o p . i o . W r i t a b l e ; im p o r t o r g . a p a c h e . h a d o o p . i o . W r i t a b l e C o m p a r a b l e ; i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . F i l e I n p u t F o r m a t i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . F i l e O u t p u t F o r m a i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . J o b C o n f ; i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . K e y V a l u e T e x t I n p i m p o r t o p a c h e . h a d o o p . m a p r e d . M a p p e r ; r g . a i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . M a p R e d u c e B a s e ; i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . O u t p u t C o l l e c t o r i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . R e c o r d R e a d e r ; i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . R e d u c e r ; i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . R e p o r t e r ; i m p r t o o r g . a p a c h e . h a d o o p . m a p r e d . S e q u e n c e F i l e I n p u i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . S e q u e n c e F i l e O u t i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . T e x t I n p u t F o r m a t i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . j o b c o n t r o l . J o b ; i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . j o b c o n t r o l . J o b C o n t r o l ; i m p o r t o r g . a p a c h e . h a d o o p . m a p r e d . l i b . I d e n t i t y M a p l p . s e t O u t p u t K e y C l l p . s e t O u t p u t V a l u e l p . s e t M a p p e r C l a s s v a l u e s F i l e I n p u t F o r m a t . a P a t h (u s e r / g a t e s / p a g e s " ) ) ; " / d ) { F i l e O u t p u t F o r m a t . y + " , " + s 1 + " , " + s 2 ; n e w P a t h ( " / u s e w T e x t ( o u t v a l ) ) ; l p . s e t N u m R e d u c e T a ( " O K " ) ; J o b l o a d P a g e s = n c o l l e c t t h e a C ( d s e s e s l L d s a o I ( s a n T s d p e ( P u x T a t t e g P . x e a c t s t l . . h a c c ( s l l l s a a p ) ; s s ) ; s s ) ; , n e w e t O u t r / g a t k s ( 0 ) w J o b } } } } u t F o r m a t ;p u b l i c s t a t i c c l a s s L o a d J o i n e d e x t e n d s M a p R e d u c e B a s e i m p l e m e n t s M a p p e r < T e x t , T e x t , T e x t , L o n g W r i t a b l e > ; t ; ; J o b C o n f l f u = n e w J o b C o n f ( M R E x a m p l e . c l a s s le t J o b N a m e ( " L o a d f u . s a n d F i l t e r U s e r s " ) ; l f u . s e t I n p u t F o r m a t ( T e x t I n p u t F o r m a t . c l a s s ) l f u . s e t O u t p u t K e y C l a s s ( T e x t . c l a s s ) ; { l f u . s e t O u t p u t V a l u e C l a s s ( T e x t . c l a s s ) ; l f u . s e t M a p p e r C l a s s ( L o a d A n d F i l t e r U s e r s . c l a p u b l i c v o i d m a p ( F i l e I n p u t F I n p u t P a t h ( l f u , o r m a . d d n e w T e x t k , P a t h ( " / u s e r / g a t e s / u s e r s " ) ) ; T e x t v a l , F i l e O u t p u t F o r m a t . s e t O u t p u t P a t h ( l f u , O u c t o r < T e x t , t p u t C o l l e L o n g W r i t a b l e > o c , n e w P a t h ( " / u s e r / g a t e s / t m p / f i l t e r e d _ u s m a t ; R e p o r t e r r e p o r t e r ) t h r o w s I O E x c e p t i o n { l f u . s e t N u m R e d u c e T a s k s ( 0 ) ; o r m a t ; / / F i n d t h e u r l J o b l o a d U s e r s = n e w J o b ( l f u ) ; S t r i n g l i n e = v a l . t o S t r i n g ( ) ; i n t f i r s t C o m m a = l i n e . i n d e x O f ( ' , ' ) ; J o b C o n f j o i n = n e w xJ o b C o n f ( a s s ) ; M R E a m p l e . c l i n t s e c o n d C o m m a = l i n e . i n d e x O f ( ' , ' , C o m m a ) ; f i r s t j o i n . s e t J o b N a m e ( " J o i n U s e r s a n d P a g e s " ) ; S t r i n g k e y = l i n e . s u b s t r i n g ( f i r s t C o m m a , s e c o n d C oj o i n . s e t I n p u t F o r m a t ( K e y V a l u e T e x t I n p u t F o r m m m a ) ; / / d r o p t h e r e s t o f t h e r e c o r d , I d o n ' t n e e d i t j o i n . s e t O u t p u t K e y C l a s s ( T e x t . c l a s s ) ; a n y m o r e , / / j u s t p a s s a 1 f o r t h e c o m b i n e r / r e d u c e r t o s u mj o i n . s e t O u t p u t V a l u e C l a s s ( T e x t . c l a s s ) ; i n s t e a d . B a s e T e x t o u t K e y = n e w T e x t ( k e y ) ; j o i n . s e t M a p p e r C l a s p e r . c l a s s ) ; a p s ( I d e n t i t y M T e x t > { o c . c o l l e c t ( o u t K e y , n e w L o n g W r i t a b l e ( 1 L ) ) ; j o i n . s e t R e d u c e r C l a s s ( J o i n . c l a s s ) ; } F i l e I n p u t F o r m a t . a d d I n p u t P a t h ( j o i n , n e w } P a t h ( " / u s e r / g a t e s / t m p / i n d e x e d _ p a g e s " ) ) ; p u b l i c s t a t i c c l a s s R e d u c e U r l s e x t e n d s M a p R e d u c e B a s e F i l e I n p u t F o r m a t . a d d I n p u t P a t h ( j o i n , n e w o n { i m p l e m e n t s R e d u c e r < T e x t , L o n g W r i t a b l e , W r i t aP a t h ( " / u s e r / g a t e s / t m p / f i l t e r e d _ u s e r s " ) ) ; b l e C o m p a r a b l e , W r i t a b l e > { F i l e O u t p u t t O u t p u t P a t h ( j o i n , F o r m a t . s e n e w P a t h ( " / u s e r / g a t e s / t m p / j o i n e d " ) ) ; p u b l i c v o i d r e d u c e ( j o i n . s e t N u m R e d u c e T a s k s ( 5 0 ) ; yT e x t , k e J o b j o i n J o b = n e w J o b ( j o i n ) ; a + 1 ) ; I t e r a t o r < L o n g W r i t a b l e > i t e r , j o i n J o b . a d d D e p e n d i n g J o b ( l o a d P a g e s ) ; O u t p u t C o l l e c t o r < W r i t a b l e C o m p a r a b l e , W r i t a b l ej o i n J o b . a d d D e p e n d i n g J o b ( l o a d U s e r s ) ; > o c , k n o w w h i c h f i l e R e p o r t e r r e p o r t e r ) t h r o w s I O E x c e p t i o n { / / A d d u p a l l t h e v a l u e s w e s e e J o b C o n f g r o u p = n e x a m p l e . c l a s s ) ; w J o b C o n f ( M R E g r o u p . s e t J o b N a m e ( " G r o u p U R L s " ) ; l o n g s u m = 0 ; g r o u p . s e t I n p u t F o r m a t ( K e y V a l u e T e x t I n p u t F o r i l ew h i t e r . h a s N e x t ( ) ) ( { g r o u p . s e t O u t p u t K e y C l a s s ( T e x t . c l a s s ) ; s u m + = i t e r . n e x t ( ) . g e t ( ) ; g r o u p . s e t O u t p u t V a l u e C l a s s ( L o n g W r i t a b l e . c l M a p R e d u c e B a s e r e p o r t e r . s e t S t a t u s ( " O K " ) ; g r o u p . s e t O u t p u t F o r ml e O u t p u t F o r m a t . c l a s s ) ; a t ( S e q u e n c e F i T e x t > { } g r o u p . s e t M a p p e r C l a s s ( L o a d J o i n e d . c l a s s ) ; g r o u p . s e t C o m b i n e r C l a s s ( R e d u c e U r l s . c l a s s ) ; o c . c o l l e c t ( k e y , n e w L o n g W r i t a b l e ( s u m ) ) ; g r o u p . s e t R e d u c e r C l a s s ( R e d u c e U r l s . c l a s s ) ; } F i l e I n p u t F o r m a t . a d d I n p u t P a t h ( g r o u p , n e w o n { } P a t h ( " / u s e r / g a t e s / t m p / j o i n e d " ) ) ; p u b l i c s t a t i c c l a s s L o a d C l i c k s e x t e n d s M a p R e d u c e B a s e F i l e O u t p u t F o r m a t . s e t O u t p u t P a t h ( g r o u p , n e w m p l e m e n t s i M a p p e r < W r i t a b l e C o m p a r a b l e , W r i t a b l e ,P a t h ( " / u s e r / g a t e s / t m p / g r o u p e d " ) ) ; L o n g W r i t a b l e , T e x t > { g r o u p . s e t N u m R e d u c e T a s k s ( 5 0 ) ; J o b g r o u p J o b = n e w J o b ( g r o u p ) ; p u b l i c v o i d m a p ( g r o u p J o b . a d d D e p e n d i n g J o b ( j o i n J o b ) ; W r i t a b l e C o m p a r a b l e k e y , m a ) ; W r i t a b l e v a l , J o b C o n f t o p 1 0 0 = n e w J o b C o n f ( M R E x a m p l e . c l O u t p u t C o l l e c t o r < L o n g W r i t a b l e , T e x t > o c , t o p 1 0 0 . s e t J o b N a m e ( " T o p 1 0 0 s i t e s " ) ; R e p o r t e r hr e p o r t e r ) c e p t i o n t r o w s I O E x { t o p 1 0 0 . s e t I n p u t F o r m a t ( S e q u e n c e F i l e I n p u t F o o c . c o l l e c t ( ( L o n g W r i t a b l e ) v a l , ( T e x t ) k e y ) ; t o p 1 0 0 . s e t O u t p u t K e y C l a s s ( L o n g W r i t a b l e . c l a } t o p 1 0 0 . s e t O u t p u t V a l u e C l a s s ( T e x t . c l a s s ) ; } t o p 1 0 0 . s e t O u t p u t F o r m a t ( S e q u e n c e F i l e O u t p u t o r m a t . c l a s s ) ; p u b l i c s t a t i c c l a s s L i m i t C l i c k s e x t e n d s M a p R e d u c e B a s e t o p 1 0 0 . s e t M a p p e r C l a s s ( L o a d C l i c k s . c l a s s ) ; i m p l e m e n t s R e d u c e r < L o n g W r i t a b l e , T e x t , L o n g W r i t a b l e ,t o p 1 0 0 . s e t C o m b i n e r C l a s s ( L i m i t C l i c k s . c l a s s T e x t > { t o p 1 0 0 . s e t R e d u c e r C l a s s ( L i m i t C l i c k s . c l a s s ) { i n t c o u n t = 0 ; F i l e I n p u t F o r m a t . a d d I n p u t P a t h ( t o p 1 0 0 , n e w p u b l i c v o i d r e d u c e ( P a t h ( " / u s e r / g a t e s / t m p / g r o u p e d " ) ) ; L o n g W r i t a b l e k e y , F i l e O u t p u t F o r m a t . s e t O u t p u t P a t h ( t o p 1 0 0 , n e w I t e r a t o r < T e x t > i t e r , P a t h ( " / u s e r / g a t e s / t o p 1 0 0 s i t e s f o r u s e r s 1 8 t o 2 5 " ) ) ; O u t p u t C o l l e c t o r < L o n g W r i t a b l e , T e x t > o c , t o p 1 0 0 . s e t N u m R e d u c e T a s k s ( 1 ) ; o n { R e p o r t e r r e p o r t e r ) t h r o w s I O E x c e p t i o n { J o b l i m i t = n e w J o b ( t o p 1 0 0 ) ; e i t ' s f r o m a n d l i m i t . a d d D e p e n d i n g J o b ( g r o u p J o b ) ; / / O n l y o u t p u t t h e f i r s t 1 0 0 r e c o r d s w h i l e< (1 0 0 n& & c o u t i t e r . h a s N e x t ( ) ) { J o b C o n t r o l j c = n e w J o b C o 1 0 0 o s i t e s n f o r o u s n t r l ( " F i d t p n g > ( ) ; o c . c o l l e c t ( k e y , i t e r . n e x t ( ) ) ; 1 8 t o 2 5 " ) ; i n g > ( ) ; c o u n t + + ; j c . a d d J o b ( l o a d P a g e s ) ; } j c . a d d J o b ( l o a d U s e r s ) ; } j c . a d d J o b ( j o i n J o b ) ; } j c . a d d J o b ( g r o u p J o b ) ; p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) t h r o w s I O E x c e p t i oj c . a d d J o b ( l i m i t ) ; n { J o b C o n f l p = n e w J o b C o n f ( M R E x a m p l e . c l a s s ) ; j c . r u n ( ) ; lt J o b N a m e ( " L o a d p . s e P a g e s " ) ; } ; l p . s e t I n p u t F o r m a t ( T e x t I n p u t F o r m a t . c l a s s ) ; } ) ; ; s s ) ;

p u t P a t h ( l p , e s / t m p / i n d e x e d _ p a g e s " ) ) ; ( l p ) ;

e r s " )

t F o r p u t F ; p e r ;

a t . c l

p u b l i c c l a s s M R E x a m p l e { p u b l i c s t a t i c c l a s s L o a d P a g e s e x t e n d s M a p R e d u c e i m p l e m e n t s M a p p e r < L o n g W r i t a b l e , T e x t , T e x t , p u b l i c / S i S S T / / T o / t n t t e / / e c v o i O u t R e P u l i n g f i i n g i n g t o P r e i t t o c o l d m a p u t C p o r t l t h l i n r s t C k e y v a l u t K e p e n d c a m e u t V a l e c t p o e e e o s u y ( L o n g l l e c t r r e p k e y = v a m m a = t r i n g = l i n e = l = n e a n i n f r o m . l =" n+ e ( o u t K e r r r u . l ( 0 e . i n w d e W o o o l i < t t t i , s e T x t a b l e k , T e x t v a l , T e x t , T e x t > o c , e r ) t h r o w s I O E x c e p t i o n u . e S e f b s x t t . i u t o r i r b ( i n s s k t n d t t e h g e C r y e ( x o i ) ) O m n ; v ; f ( ' , ' ) ; m a ) ; g ( f i r s t C o m m a l u e s o w e

r t r r x x .

wv a l u e ) ; 1 T e x t ( " y , o u t V a l ) ;

m a t . c

} } p u b l i c s t a t i c c l a s s L o a d A n d F i l t e r U s e r s e x t e n d s i m p l e m e n t s M a p p e r < L o n g W r i t a b l e , T e x t , T e x t , p u b l i c v o O u t p u R / / P u S t r i n i n t f S t r i n i n t a i f ( a S t r i n T e x t / / P r / / i t T e x t o c . c o } i t e l g i g g g g o e d m a C o l l p o r t l t h l i n r s t C v a l e = e < k e y u t K e p e n d c a m e o u t V a l l e c t p e e e e o u I 1 ( L o n c t o r r r e k e y = v m m a e = n t e g 8 | | = l i y = n a n i f r o m l = n ( o u t K g W r i < T e x p o r t o u t a l . t = l i l f i r i n e e r . p a g e n e . s e w T n d e x . e w T e y , t a b l e k , T e x t v a l , t , T e x t > o c , e r ) t h r o w s I O E x c e p t i o n s . a S t e . t C s u r s > u b s e x t te o r i o b e 2 t ( i n g ( n d e x m m a i s t r I n t ( 5 ) r r i n g k e y ) k n o w t h e ) ; O f ( ' , ' + g 1 ) ; n ( v a l u e ) e t u r n ; ( 0 , f i ; vw h i c h a l u e ) ; ; r s t C o m sf i l e o w

a s s ) ;

a s s ) ;

r m a t . s s ) ; F ) ; ;

e x t ( " 2 " + o u t V a l ) ;

v a l u e ) ;

} p u b l i c s t a t i c c l a s s J o i n e x t e n d s M a p R e d u c e B a s e i m p l e m e n t s R e d u c e r < T e x t , T e x t , T e x t , T e x t > p u b l i c v o I O R F o i t u e r d r e e r a t t p u t p o r t e a c d o C e h u c e ( T r < T e x o l l e c r r e p v a l u e t t o e x t k e y > i t e r o r < T e x r t e r ) , f i g u , , t , T e x t > o c , t h r o w s I O E x c e p t i r e o u t w h i c h f i l

/ / s t o r e i t

/ / a c c o r d i n g l y . L i s t < S t r i n g > f i r s t = n e w A r r a y L i s t < S t r i L i s t < S t r i n g > s e c o n d = n e w A r r a y L i s t < S t r w h i l e T S i f i r s t . a d d ( v a l u e . s e e t f u l ( i t x t r i n ( v b s t s e e t g a r s r . h a s N e x t ( ) = i t e r . n e x v a l u e g= )t . S t r i n ( ; l u e . c h a r A t ( i n g ( 1 ) ) ; e c o n d . a d d ( v ) { t ( ) ; t o 0 ) = =

e r s

' 1 ' )

a l u e . s u b s t r i n g ( 1 ) )

Example from http://wiki.apache.org/pig-data/attachments/PigTalksPapers/attachments/ApacheConEurope09.ppt

In Pig Latin
Users = load users as (name, age); Filtered = filter Users by age >= 18 and age <= 25; Pages = load pages as (user, url); Joined = join Filtered by name, Pages by user; Grouped = group Joined by url; Summed = foreach Grouped generate group, count(Joined) as clicks; Sorted = order Summed by clicks desc; Top5 = limit Sorted 5; store Top5 into top5sites;
Example from http://wiki.apache.org/pig-data/attachments/PigTalksPapers/attachments/ApacheConEurope09.ppt

Ease of Translation
Notice how naturally the components of the job translate into Pig Latin.
Load Users
Filter by age Join on name Group on url Count clicks Order by clicks Take top 5
Example from http://wiki.apache.org/pig-data/attachments/PigTalksPapers/attachments/ApacheConEurope09.ppt

Load Pages

Users = load Fltrd = filter Pages = load Joined = join Grouped = group Summed = count() Sorted = order Top5 = limit

Ease of Translation
Notice how naturally the components of the job translate into Pig Latin.
Load Users
Filter by age Join on name Job 1 Group on url Job 2 Count clicks Order by clicks Job 3 Take top 5
Example from http://wiki.apache.org/pig-data/attachments/PigTalksPapers/attachments/ApacheConEurope09.ppt

Load Pages

Users = load Fltrd = filter Pages = load Joined = join Grouped = group Summed = count() Sorted = order Top5 = limit

Hive
Developed at Facebook Used for majority of Facebook jobs Relational database built on Hadoop
Maintains list of table schemas SQL-like query language (HQL) Can call Hadoop Streaming scripts from HQL Supports table partitioning, clustering, complex data types, some optimizations

Creating a Hive Table


CREATE TABLE page_views(viewTime INT, userid BIGINT, page_url STRING, referrer_url STRING, ip STRING COMMENT 'User IP address') COMMENT 'This is the page view table' PARTITIONED BY(dt STRING, country STRING) STORED AS SEQUENCEFILE;

Partitioning breaks table into separate files for each (dt, country) pair
Ex: /hive/page_view/dt=2008-06-08,country=US
/hive/page_view/dt=2008-06-08,country=CA

Simple Query
Find all page views coming from xyz.com on March 31st:
SELECT page_views.* FROM page_views WHERE page_views.date >= '2008-03-01' AND page_views.date <= '2008-03-31' AND page_views.referrer_url like '%xyz.com';

Hive only reads partition 2008-03-01,* instead of scanning entire table

Aggregation and Joins


Count users who visited each page by gender:
SELECT pv.page_url, u.gender, COUNT(DISTINCT u.id) FROM page_views pv JOIN user u ON (pv.userid = u.id) GROUP BY pv.page_url, u.gender WHERE pv.date = '2008-03-03';

Sample output:
page_url home.php home.php photo.php photo.php gender MALE FEMALE MALE FEMALE count(userid) 12,141,412 15,431,579 23,941,451 21,231,314

Using a Hadoop Streaming Mapper Script


SELECT TRANSFORM(page_views.userid, page_views.date) USING 'map_script.py' AS dt, uid CLUSTER BY dt FROM page_views;

Conclusions
MapReduces data-parallel programming model hides complexity of distribution and fault tolerance Principal philosophies:
Make it scale, so you can throw hardware at problems Make it cheap, saving hardware, programmer and administration costs (but requiring fault tolerance)

Hive and Pig further simplify programming MapReduce is not suitable for all problems, but when it works, it may save you a lot of time

Yahoo! Super Computer Cluster - M45


Yahoo!s cluster is part of the Open Cirrus Testbed created by HP, Intel, and Yahoo! (see press release at http://research.yahoo.com/node/2328). The availability of the Yahoo! cluster was first announced in November 2007 (see press release at http://research.yahoo.com/node/1879). The cluster has approximately 4,000 processor-cores and 1.5 petabytes of disks. The Yahoo! cluster is intended to run the Apache open source software Hadoop and Pig. Each selected university will share the partition with up to three other universities. The initial duration of use is 6 months, potentially renewable for another 6 months upon written agreement.
For further Information, please contact: http://cloud.citris-uc.org/ Dr. Masoud Nikravesh CITRIS and LBNL, Executive Director, CSE Nikravesh@eecs.berkeley.edu Phone: (510) 643-4522

Resources
Hadoop: http://hadoop.apache.org/core/ Hadoop docs: http://hadoop.apache.org/core/docs/current/ Pig: http://hadoop.apache.org/pig Hive: http://hadoop.apache.org/hive Hadoop video tutorials from Cloudera: http://www.cloudera.com/hadoop-training