Transcript PPT

Bigtable, Hive, and Pig
Based on the slides by
Jimmy Lin
University of Maryland
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States
See http://creativecommons.org/licenses/by-nc-sa/3.0/us/ for details
Bigtable
Data Model

A table in Bigtable is a sparse, distributed, persistent
multidimensional sorted map

Map indexed by a row key, column key, and a timestamp


(row:string, column:string, time:int64)  uninterpreted byte array
Supports lookups, inserts, deletes

Single row transactions only
Image Source: Chang et al., OSDI 2006
Rows and Columns

Rows maintained in sorted lexicographic order



Applications can exploit this property for efficient row scans
Row ranges dynamically partitioned into tablets
Columns grouped into column families



Column key = family:qualifier
Column families provide locality hints
Unbounded number of columns
Bigtable Building Blocks

GFS

Chubby

SSTable
SSTable

Basic building block of Bigtable

Persistent, ordered immutable map from keys to values


Sequence of blocks on disk plus an index for block lookup


Stored in GFS
Can be completely mapped into memory
Supported operations:


Look up value associated with key
Iterate key/value pairs within a key range
64K
block
64K
block
64K
block
SSTable
Index
Source: Graphic from slides by Erik Paulson
Tablet

Dynamically partitioned range of rows

Built from multiple SSTables
Tablet
64K
block
Start:aardvark
64K
block
64K
block
End:apple
SSTable
Index
Source: Graphic from slides by Erik Paulson
64K
block
64K
block
64K
block
SSTable
Index
Architecture

Client library

Single master server

Tablet servers
Bigtable Master

Assigns tablets to tablet servers

Detects addition and expiration of tablet servers

Balances tablet server load. Tablets are distributed
randomly on nodes of the cluster for load balancing.

Handles garbage collection

Handles schema changes
Bigtable Tablet Servers

Each tablet server manages a set of tablets


Typically between ten to a thousand tablets
Each 100-200 MB by default

Handles read and write requests to the tablets

Splits tablets that have grown too large
Tablet Location
Using a 3-level B+-tree
Upon discovery, clients cache tablet locations
Image Source: Chang et al., OSDI 2006
Tablet Assignment

Master keeps track of:




Each tablet is assigned to one tablet server at a time



Set of live tablet servers
Assignment of tablets to tablet servers
Unassigned tablets
Tablet server maintains an exclusive lock on a file in Chubby
Master monitors tablet servers and handles assignment
Changes to tablet structure



Table creation/deletion (master initiated)
Tablet merging (master initiated)
Tablet splitting (tablet server initiated)
Tablet Serving
“Log Structured Merge Trees”
Image Source: Chang et al., OSDI 2006
Compactions

Minor compaction



Merging compaction



Converts the memtable into an SSTable
Reduces memory usage and log traffic on restart
Reads the contents of a few SSTables and the memtable, and
writes out a new SSTable
Reduces number of SSTables
Major compaction


Merging compaction that results in only one SSTable
No deletion records, only live data
Lock server

Chubby
– Highly-available & persistent distributed lock service
– Five active replicas; one acts as master to serve requests

Chubby is used to:
– Ensure there is only one active master
– Store bootstrap location of BigTable data
– Discover tablet servers
– Store BigTable schema information
– Store access control lists

If Chubby dies for a long period of time… Bigtable dies too….

But this almost never happens…
Optimizations

Log of tablets in the same server are merged in one log
per tablet server (node)

Locality groups: separate SSTables are created for each
locality group of column families that form the locality
groups.

Use efficient and lightweight compression to reduce the
size of SSTable blocks. Since data are organized by
column(s) very good compression is achieved (similar
values together)

Tablet servers use two levels of caching

Bloom filters are used to skip some SSTables and reduce
read overhead.
HBase

Open-source clone of Bigtable

Implementation hampered by lack of file append in HDFS
Image Source: http://www.larsgeorge.com/2009/10/hbase-architecture-101-storage.html
Hive and Pig
Need for High-Level Languages

Hadoop is great for large-data processing!



But writing Java programs for everything is verbose and slow
Not everyone wants to (or can) write Java code
Solution: develop higher-level data processing languages


Hive: HQL is like SQL
Pig: Pig Latin is a bit like Perl
Hive and Pig

Hive: data warehousing application in Hadoop




Pig: large-scale data processing system




Query language is HQL, variant of SQL
Tables stored on HDFS as flat files
Developed by Facebook, now open source
Scripts are written in Pig Latin, a dataflow language
Developed by Yahoo!, now open source
Roughly 1/3 of all Yahoo! internal jobs
Common idea:


Provide higher-level language to facilitate large-data processing
Higher-level language “compiles down” to Hadoop jobs
Hive: Background

Started at Facebook

Data was collected by nightly cron jobs into Oracle DB

“ETL” via hand-coded python

Grew from 10s of GBs (2006) to 1 TB/day new data
(2007), now 10x that
Source: cc-licensed slide by Cloudera
Hive Components

Shell: allows interactive queries

Driver: session handles, fetch, execute

Compiler: parse, plan, optimize

Execution engine: DAG of stages (MR, HDFS, metadata)

Metastore: schema, location in HDFS, etc
Source: cc-licensed slide by Cloudera
Data Model

Tables



Partitions


Typed columns (int, float, string, boolean)
Also, list: map (for JSON-like data)
For example, range-partition tables by date
Buckets

Hash partitions within ranges (useful for sampling, join
optimization)
Source: cc-licensed slide by Cloudera
Metastore

Database: namespace containing a set of tables

Holds table definitions (column types, physical layout)

Holds partitioning information

Can be stored in Derby, MySQL, and many other
relational databases
Source: cc-licensed slide by Cloudera
Physical Layout

Warehouse directory in HDFS


Tables stored in subdirectories of warehouse


E.g., /user/hive/warehouse
Partitions form subdirectories of tables
Actual data stored in flat files


Control char-delimited text, or SequenceFiles
With custom SerDe, can use arbitrary format
Source: cc-licensed slide by Cloudera
Hive: Example

Hive looks similar to an SQL database

Relational join on two tables:


Table of word counts from Shakespeare collection
Table of word counts from Homer
SELECT s.word, s.freq, k.freq FROM shakespeare s
JOIN homer k ON (s.word = k.word) WHERE s.freq >= 1 AND k.freq >= 1
ORDER BY s.freq DESC LIMIT 10;
the
I
and
to
of
a
you
my
in
is
25848
23031
19671
18038
16700
14170
12702
11297
10797
8882
Source: Material drawn from Cloudera training VM
62394
8854
38985
13526
34654
8057
2720
4135
12445
6884
Hive: Behind the Scenes
SELECT s.word, s.freq, k.freq FROM shakespeare s
JOIN homer k ON (s.word = k.word) WHERE s.freq >= 1 AND k.freq >= 1
ORDER BY s.freq DESC LIMIT 10;
(Abstract Syntax Tree)
(TOK_QUERY (TOK_FROM (TOK_JOIN (TOK_TABREF shakespeare s) (TOK_TABREF homer k) (= (. (TOK_TABLE_OR_COL
s) word) (. (TOK_TABLE_OR_COL k) word)))) (TOK_INSERT (TOK_DESTINATION (TOK_DIR TOK_TMP_FILE)) (TOK_SELECT
(TOK_SELEXPR (. (TOK_TABLE_OR_COL s) word)) (TOK_SELEXPR (. (TOK_TABLE_OR_COL s) freq)) (TOK_SELEXPR (.
(TOK_TABLE_OR_COL k) freq))) (TOK_WHERE (AND (>= (. (TOK_TABLE_OR_COL s) freq) 1) (>= (. (TOK_TABLE_OR_COL k)
freq) 1))) (TOK_ORDERBY (TOK_TABSORTCOLNAMEDESC (. (TOK_TABLE_OR_COL s) freq))) (TOK_LIMIT 10)))
(one or more of MapReduce jobs)
Pig-Latin
Example Data Analysis Task
Find users who tend to visit “good” pages.
Visits
Pages
url
time
url
Amy
www.cnn.com
8:00
www.cnn.com
0.9
Amy
www.crap.com
8:05
www.flickr.com
0.9
Amy
www.myblog.com
10:00
www.myblog.com
0.7
Amy
www.flickr.com
10:05
www.crap.com
0.2
Fred
cnn.com/index.htm 12:00
...
Pig Slides adapted from Olston et al.
pagerank
...
user
Conceptual Dataflow
Load
Visits(user, url, time)
Load
Pages(url, pagerank)
Canonicalize URLs
Join
url = url
Group by user
Compute Average Pagerank
Filter
avgPR > 0.5
Pig Slides adapted from Olston et al.
System-Level Dataflow
Visits
load
Pages
...
...
load
canonicalize
join by url
...
group by user
...
the answer
Pig Slides adapted from Olston et al.
compute average pagerank
filter
MapReduce Code
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
( S t r i n
f o r
(
S
o
r
c r
s
t r
r i
. c
p o
o
1
i
n
o
r
s s
p r
:
f i
n g
s 2
g
o u t
l l e c t
t e r . s
o d u
r s t
:
v a l
( n u
e t S
c t
a
)
{
s e c o
=
k
l l ,
t a t u
g
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 ;
S
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 ;
t
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 ;
c
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 ;
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 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 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 p
n u
p b
u l
t i
F c
o r s
m t
a a
t t
; i c
c l a s s
L o a d J o i n e d
i m p o r t p oa rc gh .e a. h a d o o p . m a p r e d . M a p p e r ;
i m p l e m e n t s
M a p p e r < T e x t ,
T e x
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 ;
p u b l i c
v o i d
m a p (
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 ;
T e x t
k ,
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 ;
T e x t
v a 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 . R e p o r t e r ;
c t Oo ur t< pT ue tx Ct o, l lL eo n g W r i t a b
i m op 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 I n p u t F o r m a t ;
R e p o r t e r
r e p o r t 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 . S e q u e n c e F i l e O u t p u t F o r /
m /
a t F
; i n d
t h e
u r 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 . T e x t I n p u t F o r m a t ;
S t r i n g
l i n e
=
v a l . t o S t 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 . j o b c o n t r o l . J o b ;
i n t
f i r s t C o m m a
=
l i n e . i
i m p o r t
o r g . a p a c h e . h a d o o p . om na tp rr oe ld ;. j o b c o n t r o l . J o b C
i n t
s e c o n d C o m m a
C
= o m
l m
i a
n )
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 . l i b . I d e n t i t y M a p p e r ;
S t r i n g
k e y
=
l i n e . s u b s t
/ /
d r o p
t h e
r e s t
o f
t h e
p u b l i c
c l a s s
M R E x a m p l e
{
/ /
j u s t
p a s s
a
1
f o r
t h
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 B a T
s e
e x t
o u t K e y
=
n e w
T e x t (
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 ,
o
T c
e .
x c
t o
> l l
{ e c t ( o u t K e y ,
n e w
L
}
p u b l i c
v o i d
m a p ( L o n g W r i t a b l e
k ,
T e x t
}
v a l ,
O u t p u t C o l l e c t o r < T e x t ,
T e x t >
o c ,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
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 i
t m
i p
o l
n e m
{ e n t s
R e d u c e r < T e x t ,
L o
/ /
P u l l
t h e
k e y
o u t
W r i t a b l e >
{
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 ( ' , ' ) ;
p u b l i c
v o i d
r e d u c e (
S t r i n gs t kr ei yn g =( 0 l, i nf ei .r ss ut bC o m m a ) ;
y ,
T e x t
k e
S t r i n g
v a l u e
=
l i n e . s u b s t r i n g ( f i r s t C o m m a
+
1 ) I
; t e r a t o r < L o n g W r i t a b
T e x t
o u t K e y
=
n e w
T e x t ( k e y ) ;
O u t p u t C o l l e c t o r < W r i
/ /
P r e p e n d
a n
i n d e x
t o
t h e
v a l u e
s o
w e
k n o w
w R
h e
i p
c o
h r t
f e
i r
l e r e p o r t e r )
/ /
i t
c a m e
f r o m .
/ /
A d d
u p
a l l
t h e
v a l u e
T e x t
o u t"V a+l
v=a lnueew) ;T e x t ( " 1
o c . c o l l e c t ( o u t K e y ,
o u t V a l ) ;
l o n g
s u m
=
0 ;
}
i l e
( wi ht e r . h a s N e x t ( ) )
{
}
s u m
+ =
i t e r . n e x t ( ) .
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
M a p R e d r
u e
c p
e o
B r
a t
s e
e r . s e t S t a t u 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 ,
T }
e x t >
{
p u b l i c
v o i d
m a p ( L o n g W r i t
O u t p u t C o l l e c t o r < T e x t ,
R e p o r t e r
r e p o r t e
/ /
P u l l
t h e
k e y
o u t
S t r i n g
l i n e
=
v a l . t o
i n t
f i r s t C o m m a
=
l i n
S t r i n g
v a l u
f e
i r =
s t l
C i
o n
m e
m .
a
i n t
a g e
=
I n t e g e r . p a
i f
( a g e
<
1 8
| |
a g e
S t r i n g
k e y
=
l i n e . s u
T e x t
o u t K e y
=
n e w
T e
/ /
P r e p e n d
a n
ei n kd ne ox w
/ /
i t
c a m e
f r o m .
T e x t
o u t V a l
=
n e w
T e
o c . c o l l e c t ( o u t K e y ,
o
}
a b l e
k ,
T e x t
T e x t >
o c ,
r )
t h r o w s
I O E
S
e
s
r
>
b
x
t
t
.
u
+
s
r
i
b
e
2
s t
t (
wo h
i n g
n d e
s
1 t
) r
;
I n t
5 )
r i n
k e y
it ch he
(
x
i
(
r
g
)
)
O
n
v
e
(
;
fv
x t ( " 2 "
+
u t V a l ) ;
;
f
g
a
t
0
T
( e
'
(
l u
u r
,
x
, t
'
e )
n ;
f i
ia ll eu e
v a l u
}
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
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 ,
p u b l i c
/ /
s t o r e
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
t h r o w s
I O E
r e
o u t
w h i
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
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
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 e r . h a s N
x t
t
=
i t e
rS it nr gi n vg a( l) u; e
( v a l u e . c h
b s t r i n g ( 1 )
s e
s e c o n d .
Pig Slides adapted from Olston et al.
e x t
r . n
=
a r A
) ;
a d d
(
e
t
t
)
x
.
(
)
{
t ( ) ;
t o
0 )
= =
' 1 ' )
( v a l u e . s u b s t r i
n d
c o
P
n d )
{
e y
+
n e w
T
s ( " O K
l p . s e t O u t p u t K e y C
l p . s e t O u t p u t V a l u
l p . s e t M a p p e r C l a s
l l e c t
t F
h i
e l e
v I
a n
l p
u u
e t
s F o r m a t .
a t h u( s" e/ r / g a t e s / p a g e s " ) ) ;
F i l e O u t p u t F o r m a t
" , "
+
s 1
+
n
" e
, w
"
P
+ a t
s h
2 (
; " / u
e x t ( o u t l
v p
a .
l s
) e
) t
; N u m R e d u c e T
" ) ;
J o b
l o a d P a g e s
=
l
e
s
a
a
C
(
d
s
l
L
d
s
a
o
I
(
s
a
n
T
s
d
p
e
(
P
u
.
s
a
n
s
e
s
e
e t O u t
r / g a t
k s ( 0 )
w
J o b
x
T
a
t
t
e
g
P
.
x
e
a
c
t
s
t
l
.
.
h
a
c
c
(
p u t P a t
e s / t m p
;
( l p ) ;
J o b C o n f
l f u
=
n e w
J o b C o n f ( M R E
e t
l J
f o
u b
. N
s a m e ( " L o a d
a n d
F i l t e r
U s e
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
e x t e n d s
M a p R e d l
u f
c u
e .
B s
a e
s t
e O u t p u t K e y C l a s s ( T e x t . c l
t ,
T e x t ,
L o n g W l
r f
i u
t .
a s
b e
l t
e O
> u t
{ p u t V a l u e C l a s s ( T e x t .
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
F i l e I n pI un tp Fu ot rP ma at th .( al df du ,
n e w
P a t h ( " / u s e r / g a t e s / u s e r s " ) ) ;
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
l e >
o c ,
n e w
P a t h ( " / u s e r / g a t e s / t m p
t h r o w s
I O E x c e p l
t f
i u
o .
n s e
{ t N u m R e d u c e T a s k s ( 0 ) ;
J o b
l o a d U s e r s
=
n e w
J o b ( l f u ) ;
i n g ( ) ;
n d e x O f ( ' , ' ) ;
J o b C o n f
j o iM nR E =x a nm ep wl e J. oc bl Ca os ns f) (;
i n d e x O f ( ' , ' ,
f j
i o
r i
s n
t . s e t J o b N a m e ( " J o i n
U s e r s
a
r i n g ( f i r s t C o m m j
a o
, i n
s .
e s
c e
o t
n I
d n
C p
o u
m t
m F
a o
) r
; m a t ( K e y V a l u e T
r e c o r d ,
I
d o n j
' o
t i n
n .
e s
e e
d t O
i u
t t p
a u
n t
y K
m e
o y
r C
e l
, a s s ( T e x t . c
e
c o m b i n e r / r e d j
u o
c i
e n
r . s
t e
o t O
s u
u t
m p u
i t
n V
s a
t l
e u
a e
d C
. l a s s ( T e x t
k e y ) ;
j o i n . s e t M a p p e pr eC rl .a cs ls a( sI sd )e ;n t i t y M
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
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 (
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 " )
e x t e n d s
M a p R e d F
u i
c l
e e
B I
a n
s p
e u t F o r m a t . a d d I n p u t P a t h (
n g W r i t P
a a
b t
l h
e (
, " /
W u
r s
i e
t r
a /
b g
l a
e t
C e
o s
m /
p t
a m
r p
a /
b f
l i
e l
, t e r e d _ u s e r s "
F i l e O u tt pO uu tt Fp ou rt mP aa tt .h s( ej o i n ,
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 " ) ) ;
j o i n . s e t N u m R e d u c e T a s k s ( 5 0 ) ;
J o b
j o i n J o b
=
n e w
J o b ( j o i n ) ;
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
t a b l e C o m p a r a b l j
e o
, i n
W J
r o
i b
t .
a a
b d
l d
e D
> e p
o e
c n
, d i n g J o b ( l o a d U
t h r o w s
I O E x c e p t i o n
{
s
w e
s e e
J o b C o n f
g r o u p x a= m pn le ew . cJ lo ab sC so )n ;f ( M
g r o u p . s e t J o b N a m e ( " G r o u p
U R L s "
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
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 .
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
" O K " ) ;
g r o u p . s e t O u t p u
l t
e F
O o
u r
t m
p a
u t
t (
F S
o e
r q
m u
a e
t n
. c
c
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
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
v a l ,
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
}
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 (
}
x c e p t i 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 RF ei dl ue cO eu Bt ap su et F o r m a t . s e t O u t p u t P a t h ( g
m p l ei m e n t s
M a p p e r < W r i t a b l e C o m p a r a b l e , P W
a r
t i
h t
( a
" b
/ l
u e
s ,
e r L
/ o
g n
a g
t W
e r
s i
/ t
t a
m b
p l
/ e
g ,
r o u p e d " ) ) ;
>
) ; {
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
W r i t a b l e C o m p a r a b l e
k e y ,
r s t C o m 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 (
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 t
x o
t p
> 1 0
o 0
c .
, s e t J o b N a m e ( " T o p
1 0 0
s i t
s o
w
R e pt oh rr to ew rs
rI eO pE ox rc te ep rt )i o n
{
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
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 t
e o
y 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
e ) ;
}
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
}
t o p 1 0 0 . s e t O u t p u t F o ro mr am ta (t S. ec ql ua es ns
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 t
d o
u p
c 1
e 0
B 0
a .
s s
e e t M a p p e r C l a s s ( L o a d C l i
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 t
n o
g p
W 1
r 0
i 0
t .
a s
b e
l t
e C
, o m
T b
e i
x n
t e
> r C
{ l a s s ( L i m i t
e B a s e
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
T e x t 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 (
pv uo bi ld i cr 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
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
c ,
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 t
c o
, p 1 0 0 . s e t N u m R e d u c e T a s k s ( 1 ) ;
x c e p t i 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 ) ;
c h
f i l 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
/ /
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
1 e
0 0 ( c
& o
& u n
i t
t e r . h a s N e x t ( ) )
{
J o b C o n t r o l
j c
=
n e w1 0 J0 o bs Ci ot ne ts r of
t < S t r i 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 " ) ;
s t < S t r 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 j c
I .
O a
E d
x d
c J
e o
p b
t (
i l
o i
n m i
{ t ) ;
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 ( ) ;
t J
l o
p b
. N
s a
e m e ( " L o a d
P a g e s " ) ;
}
n g ( 1 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 ) ;
Pig Latin Script
Visits = load
‘/data/visits’ as (user, url, time);
Visits = foreach Visits generate user, Canonicalize(url), time;
Pages = load
‘/data/pages’ as (url, pagerank);
VP = join
Visits by url, Pages by url;
UserVisits = group
VP by user;
UserPageranks = foreach UserVisits generate user,
AVG(VP.pagerank) as avgpr;
GoodUsers = filter UserPageranks by avgpr > ‘0.5’;
store
GoodUsers into '/data/good_users';
Pig Slides adapted from Olston et al.
Java vs. Pig Latin
1/20 the lines of code
1/16 the development time
300
180
160
140
120
100
80
60
40
20
0
Minutes
250
200
150
100
50
0
Hadoop
Pig
Hadoop
Performance on par with raw Hadoop!
Pig Slides adapted from Olston et al.
Pig
Pig takes care of…

Schema and type checking

Translating into efficient physical dataflow


Exploiting data reduction opportunities


(e.g., early partial aggregation via a combiner)
Executing the system-level dataflow


(i.e., sequence of one or more MapReduce jobs)
(i.e., running the MapReduce jobs)
Tracking progress, errors, etc.

Another Pig Script:

Pig Script 2: Temporal Query Phrase Popularity
The Temporal Query Phrase Popularity script (script2local.pig or script2-hadoop.pig) processes a search query
log file from the Excite search engine and compares the
occurrence of frequency of search phrases across two
time periods separated by twelve hours.

Use the PigStorage function to load the excite log file (excite.log or excite-small.log) into the “raw”
bag as an array of records with the fields user, time, and query.
raw = LOAD 'excite.log' USING PigStorage('\t') AS (user, time, query);

Call the NonURLDetector UDF to remove records if the query field is empty or a URL.
clean1 = FILTER raw BY org.apache.pig.tutorial.NonURLDetector(query);

Call the ToLower UDF to change the query field to lowercase.
clean2 = FOREACH clean1 GENERATE user, time, org.apache.pig.tutorial.ToLower(query) as query;

Because the log file only contains queries for a single day, we are only interested in the hour. The
excite query log timestamp format is YYMMDDHHMMSS. Call the ExtractHour UDF to extract the
hour from the time field.
houred = FOREACH clean2 GENERATE user, org.apache.pig.tutorial.ExtractHour(time) as hour, query;

Call the NGramGenerator UDF to compose the n-grams of the query.
ngramed1 = FOREACH houred GENERATE user, hour,
flatten(org.apache.pig.tutorial.NGramGenerator(query)) as ngram;

Use the DISTINCT operator to get the unique n-grams for all records.
ngramed2 = DISTINCT ngramed1;

Use the GROUP operator to group the records by n-gram and hour.
hour_frequency1 = GROUP ngramed2 BY (ngram, hour);

Use the COUNT function to get the count (occurrences) of each n-gram.
hour_frequency2 = FOREACH hour_frequency1 GENERATE flatten($0), COUNT($1) as count;

Use the FOREACH-GENERATE operator to assign names to the fields.
hour_frequency3 = FOREACH hour_frequency2 GENERATE $0 as ngram, $1 as hour, $2
as count;

Use the FILTERoperator to get the n-grams for hour ‘00’
hour00 = FILTER hour_frequency2 BY hour eq '00';

Uses the FILTER operators to get the n-grams for hour ‘12’
hour12 = FILTER hour_frequency3 BY hour eq '12';

Use the JOIN operator to get the n-grams that appear in both hours.
same = JOIN hour00 BY $0, hour12 BY $0;

Use the FOREACH-GENERATE operator to record their frequency.
same1 = FOREACH same GENERATE hour_frequency2::hour00::group::ngram as
ngram, $2 as count00, $5 as count12;

Use the PigStorage function to store the results. The output file contains a list of ngrams with the following fields: hour, count00, count12.
STORE same1 INTO '/tmp/tutorial-join-results' USING PigStorage();