database and mapreduce based on slides from jimmy lins lecture slides (jimmylin/clou...
TRANSCRIPT
![Page 1: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/1.jpg)
Database and MapReduce
Based on slides from Jimmy Lin’s lecture slides (http://www.umiacs.umd.edu/~jimmylin/clou
d-2010-Spring/index.html) (licensed under Creation Commons Attribution 3.0 License)
![Page 2: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/2.jpg)
RoadMap
• Role of relational databases in today’s organizations– Where does MapReduce fit in?
• MapReduce algorithms for processing relational data– How do I perform a join, etc.?
• Evolving roles of relational databases and MapReduce– What’s in store for the future?
![Page 3: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/3.jpg)
Relational Database Basics
![Page 4: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/4.jpg)
Basic Structure
• Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai Di
• Example:
customer_name = {Jones, Smith, Curry, Lindsay}
customer_street = {Main, North, Park}
customer_city = {Harrison, Rye, Pittsfield}
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield) }
is a relation over customer_name , customer_street, customer_city
![Page 5: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/5.jpg)
Relation Schema
• A1, A2, …, An are attributes
• R = (A1, A2, …, An ) is a relation schema
Example:
Customer_schema = (customer_name, customer_street, customer_city)
• r(R) is a relation on the relation schema RExample:customer (Customer_schema)
![Page 6: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/6.jpg)
Relation Instance
• The current values (relation instance) of a relation are specified by a table
• An element t of r is a tuple, represented by a row in a table
JonesSmithCurryLindsay
customer_name
MainNorthNorthPark
customer_street
HarrisonRyeRyePittsfield
customer_city
customer
attributes(or columns)
tuples(or rows)
![Page 7: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/7.jpg)
Database• A database consists of multiple relations
• Information about an enterprise is broken up into parts, with each relation storing one part of the information
account : stores information about accounts depositor : stores information about which customer owns which account customer : stores information about customers
• Storing all information as a single relation such as bank(account_number, balance, customer_name, ..)results in repetition of information (e.g., two customers own an account) and the need for null values (e.g., represent a customer without an account)
![Page 8: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/8.jpg)
Banking Example
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
![Page 9: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/9.jpg)
Relational Algebra
• Primitives– Projection ()– Selection ()– Cartesian product ()– Set union ()– Set difference ()– Rename ()
• Other operations– Join ( )⋈– Group by… aggregation– …
![Page 10: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/10.jpg)
Big Data Analysis
• Peta-scale datasets are everywhere:– Facebook has 2.5 PB of user data + 15 TB/day (4/2009) – eBay has 6.5 PB of user data + 50 TB/day (5/2009)– …
• A lot of these datasets are (mostly) structured– Query logs– Point-of-sale records– User data (e.g., demographics)– …
• How do we perform data analysis at scale?– Relational databases and SQL– MapReduce (Hadoop)
![Page 11: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/11.jpg)
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 12: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/12.jpg)
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 13: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/13.jpg)
One Database or Two?
• Downsides of co-existing OLTP and OLAP workloads– Poor memory management– Conflicting data access patterns– Variable latency
• Solution: separate databases– User-facing OLTP database for high-volume
transactions– Data warehouse for OLAP workloads– How do we connect the two?
![Page 14: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/14.jpg)
OLTP/OLAP Architecture
OLTP OLAP
ETL(Extract, Transform, and Load)
![Page 15: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/15.jpg)
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 16: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/16.jpg)
Warehouse Models & Operators
• Data Models– relations– stars & snowflakes– cubes
• Operators– slice & dice– roll-up, drill down– pivoting– other
16
![Page 17: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/17.jpg)
Star
17
customer custId name address city53 joe 10 main sfo81 fred 12 main sfo
111 sally 80 willow la
product prodId name pricep1 bolt 10p2 nut 5
store storeId cityc1 nycc2 sfoc3 la
sale oderId date custId prodId storeId qty amto100 1/7/97 53 p1 c1 1 12o102 2/7/97 53 p2 c1 2 11105 3/8/97 111 p1 c3 5 50
![Page 18: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/18.jpg)
Star Schema
18
saleorderId
datecustIdprodIdstoreId
qtyamt
customercustIdname
addresscity
productprodIdnameprice
storestoreId
city
![Page 19: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/19.jpg)
Terms
• Fact table• Dimension tables• Measures
19
saleorderId
datecustIdprodIdstoreId
qtyamt
customercustIdname
addresscity
productprodIdnameprice
storestoreId
city
![Page 20: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/20.jpg)
Dimension Hierarchies
20
store storeId cityId tId mgrs5 sfo t1 joes7 sfo t2 freds9 la t1 nancy
city cityId pop regIdsfo 1M northla 5M south
region regId namenorth cold regionsouth warm region
sType tId size locationt1 small downtownt2 large suburbs
store
sType
city region
è snowflake schemaè constellations
![Page 21: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/21.jpg)
Cube
21
sale prodId storeId amtp1 c1 12p2 c1 11p1 c3 50p2 c2 8
c1 c2 c3p1 12 50p2 11 8
Fact table view:Multi-dimensional cube:
dimensions = 2
![Page 22: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/22.jpg)
3-D Cube
22
sale prodId storeId date amtp1 c1 1 12p2 c1 1 11p1 c3 1 50p2 c2 1 8p1 c1 2 44p1 c2 2 4
day 2 c1 c2 c3p1 44 4p2 c1 c2 c3
p1 12 50p2 11 8
day 1
dimensions = 3
Multi-dimensional cube:Fact table view:
![Page 23: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/23.jpg)
ROLAP vs. MOLAP
• ROLAP:Relational On-Line Analytical Processing
• MOLAP:Multi-Dimensional On-Line Analytical Processing
23
![Page 24: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/24.jpg)
24
Typical OLAP Queries
• The typical OLAP query will:1. Start with a star join.2. Select for interesting tuples, based on dimension
data.3. Group by one or more dimensions.4. Aggregate certain attributes of the result.
![Page 25: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/25.jpg)
Aggregates
25
sale prodId storeId date amtp1 c1 1 12p2 c1 1 11p1 c3 1 50p2 c2 1 8p1 c1 2 44p1 c2 2 4
• Add up amounts for day 1• In SQL: SELECT sum(amt) FROM SALE WHERE date = 1
81
![Page 26: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/26.jpg)
Aggregates
26
sale prodId storeId date amtp1 c1 1 12p2 c1 1 11p1 c3 1 50p2 c2 1 8p1 c1 2 44p1 c2 2 4
• Add up amounts by day• In SQL: SELECT date, sum(amt) FROM SALE GROUP BY date
ans date sum1 812 48
![Page 27: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/27.jpg)
Another Example
27
sale prodId storeId date amtp1 c1 1 12p2 c1 1 11p1 c3 1 50p2 c2 1 8p1 c1 2 44p1 c2 2 4
• Add up amounts by day, product• In SQL: SELECT date, sum(amt) FROM SALE GROUP BY date, prodId
sale prodId date amtp1 1 62p2 1 19p1 2 48
drill-down
rollup
![Page 28: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/28.jpg)
Aggregates
• Operators: sum, count, max, min, median, ave
• “Having” clause• Using dimension hierarchy
– average by region (within store)– maximum by month (within date)
28
![Page 29: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/29.jpg)
Cube Aggregation
29
day 2 c1 c2 c3p1 44 4p2 c1 c2 c3
p1 12 50p2 11 8
day 1
c1 c2 c3p1 56 4 50p2 11 8
c1 c2 c3sum 67 12 50
sump1 110p2 19
129
. . .
drill-down
rollup
Example: computing sums
![Page 30: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/30.jpg)
Cube Operators
30
day 2 c1 c2 c3p1 44 4p2 c1 c2 c3
p1 12 50p2 11 8
day 1
c1 c2 c3p1 56 4 50p2 11 8
c1 c2 c3sum 67 12 50
sump1 110p2 19
129
. . .
sale(c1,*,*)
sale(*,*,*)sale(c2,p2,*)
![Page 31: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/31.jpg)
Extended Cube
31
c1 c2 c3 *p1 56 4 50 110p2 11 8 19* 67 12 50 129day 2 c1 c2 c3 *
p1 44 4 48p2* 44 4 48
c1 c2 c3 *p1 12 50 62p2 11 8 19* 23 8 50 81
day 1
*
sale(*,p2,*)
![Page 32: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/32.jpg)
Aggregation Using Hierarchies
32
day 2 c1 c2 c3p1 44 4p2 c1 c2 c3
p1 12 50p2 11 8
day 1
region A region Bp1 56 54p2 11 8
customer
region
country
(customer c1 in Region A;customers c2, c3 in Region B)
![Page 33: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/33.jpg)
Pivoting
33
sale prodId storeId date amtp1 c1 1 12p2 c1 1 11p1 c3 1 50p2 c2 1 8p1 c1 2 44p1 c2 2 4
day 2 c1 c2 c3p1 44 4p2 c1 c2 c3
p1 12 50p2 11 8
day 1
Multi-dimensional cube:Fact table view:
c1 c2 c3p1 56 4 50p2 11 8
![Page 34: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/34.jpg)
Business Intelligence
• Premise: more data leads to better business decisions– Periodic reporting as well as ad hoc queries– Analysts, not programmers (importance of tools and dashboards)
• Examples:– Slicing-and-dicing activity by different dimensions to better
understand the marketplace– Analyzing log data to improve OLTP experience– Analyzing log data to better optimize ad placement– Analyzing purchasing trends for better supply-chain management– Mining for correlations between otherwise unrelated activities
![Page 35: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/35.jpg)
OLTP/OLAP Architecture: Hadoop?
OLTP OLAP
ETL(Extract, Transform, and Load)
Hadoop here?
What about here?
![Page 36: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/36.jpg)
OLTP/OLAP/Hadoop Architecture
OLTP OLAP
ETL(Extract, Transform, and Load)
Hadoop
Why does this make sense?
![Page 37: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/37.jpg)
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 38: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/38.jpg)
MapReduce algorithms for processing relational data
![Page 39: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/39.jpg)
Design Pattern: Secondary Sorting
• MapReduce sorts input to reducers by key– Values are arbitrarily ordered
• What if want to sort value also?– E.g., k → (v1, r), (v3, r), (v4, r), (v8, r)…
![Page 40: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/40.jpg)
Secondary Sorting: Solutions
• Solution 1:– Buffer values in memory, then sort– Why is this a bad idea?
• Solution 2:– “Value-to-key conversion” design pattern: form
composite intermediate key, (k, v1)– Let execution framework do the sorting– Preserve state across multiple key-value pairs to handle
processing– Anything else we need to do?
![Page 41: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/41.jpg)
Value-to-Key Conversion
k → (v1, r), (v4, r), (v8, r), (v3, r)…
(k, v1) → (v1, r)
Before
After
(k, v3) → (v3, r)(k, v4) → (v4, r)(k, v8) → (v8, r)
Values arrive in arbitrary order…
…
Values arrive in sorted order…Process by preserving state across multiple keys
Remember to partition correctly!
![Page 42: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/42.jpg)
Working Scenario
• Two tables:– User demographics (gender, age, income, etc.)– User page visits (URL, time spent, etc.)
• Analyses we might want to perform:– Statistics on demographic characteristics– Statistics on page visits– Statistics on page visits by URL– Statistics on page visits by demographic characteristic– …
![Page 43: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/43.jpg)
Relational Algebra
• Primitives– Projection ()– Selection ()– Cartesian product ()– Set union ()– Set difference ()– Rename ()
• Other operations– Join ( )⋈– Group by… aggregation– …
![Page 44: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/44.jpg)
Projection
R1
R2
R3
R4
R5
R1
R2
R3
R4
R5
![Page 45: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/45.jpg)
Projection in MapReduce
• Easy!– Map over tuples, emit new tuples with appropriate
attributes– No reducers, unless for regrouping or resorting tuples– Alternatively: perform in reducer, after some other
processing• Basically limited by HDFS streaming speeds
– Speed of encoding/decoding tuples becomes important– Relational databases take advantage of compression– Semistructured data? No problem!
![Page 46: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/46.jpg)
Selection
R1
R2
R3
R4
R5
R1
R3
![Page 47: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/47.jpg)
Selection in MapReduce• Easy!
– Map over tuples, emit only tuples that meet criteria– No reducers, unless for regrouping or resorting tuples– Alternatively: perform in reducer, after some other
processing• Basically limited by HDFS streaming speeds
– Speed of encoding/decoding tuples becomes important
– Relational databases take advantage of compression– Semistructured data? No problem!
![Page 48: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/48.jpg)
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 49: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/49.jpg)
Relational Joins
Source: Microsoft Office Clip Art
![Page 50: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/50.jpg)
Relational Joins
R1
R2
R3
R4
S1
S2
S3
S4
R1 S2
R2 S4
R3 S1
R4 S3
![Page 51: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/51.jpg)
Natural Join Operation – Example
• Relations r, s:A B
12412
C D
aabab
B
13123
D
aaabb
E
r
A B
11112
C D
aaaab
E
s
r s
![Page 52: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/52.jpg)
Natural Join Example
R1 S1
R1 S1 =
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/9658 rusty 10 35.0 103 11/12/96
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
sid bid day
22 101 10/10/96 58 103 11/12/96
![Page 53: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/53.jpg)
Types of Relationships
One-to-OneOne-to-ManyMany-to-Many
![Page 54: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/54.jpg)
Join Algorithms in MapReduce
• Reduce-side join• Map-side join• In-memory join
– Striped variant– Memcached variant
![Page 55: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/55.jpg)
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
• Two variants– 1-to-1 joins– 1-to-many and many-to-many joins
![Page 56: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/56.jpg)
Reduce-side Join: 1-to-1
R1
R4
S2
S3
R1
R4
S2
S3
keys valuesMap
R1
R4
S2
S3
keys values
Reduce
Note: no guarantee if R is going to come first or S
![Page 57: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/57.jpg)
Reduce-side Join: 1-to-many
R1
S2
S3
R1
S2
S3
S9
keys valuesMap
R1 S2
keys values
Reduce
S9
S3 …
What’s the problem?
![Page 58: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/58.jpg)
Reduce-side Join: V-to-K Conversion
R1
keys values
In reducer…
S2
S3
S9
R4
S3
S7
New key encountered: hold in memory
Cross with records from other set
New key encountered: hold in memory
Cross with records from other set
![Page 59: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/59.jpg)
Reduce-side Join: many-to-many
R1
keys values
In reducer…
S2
S3
S9
Hold in memory
Cross with records from other set
R5
R8
What’s the problem?
![Page 60: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/60.jpg)
Map-side Join: Basic Idea
Assume two datasets are sorted by the join key:
R1
R2
R3
R4
S1
S2
S3
S4
A sequential scan through both datasets to join(called a “merge join” in database terminology)
![Page 61: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/61.jpg)
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 62: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/62.jpg)
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 63: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/63.jpg)
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 64: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/64.jpg)
Memcached
Database layer: 800 eight-core Linux servers running MySQL (40 TB user data)
Caching servers: 15 million requests per second, 95% handled by memcache (15 TB of RAM)
Source: Technology Review (July/August, 2008)
![Page 65: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/65.jpg)
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 66: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/66.jpg)
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 67: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/67.jpg)
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
![Page 68: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/68.jpg)
Evolving roles for relational database and MapReduce
![Page 69: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/69.jpg)
OLTP/OLAP/Hadoop Architecture
OLTP OLAP
ETL(Extract, Transform, and Load)
Hadoop
Why does this make sense?
![Page 70: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/70.jpg)
Need for High-Level Languages
• Hadoop is great for large-data processing!– But writing Java programs for everything is
verbose and slow– Analysts don’t want to (or can’t) write Java
• Solution: develop higher-level data processing languages– Hive: HQL is like SQL– Pig: Pig Latin is a bit like Perl
![Page 71: Database and MapReduce Based on slides from Jimmy Lins lecture slides (jimmylin/clou d-2010-Spring/index.html) (licensed under](https://reader036.vdocuments.site/reader036/viewer/2022062318/55163c3455034694308b64c0/html5/thumbnails/71.jpg)
Hive and Pig
• Hive: data warehousing application in Hadoop– Query language is HQL, variant of SQL– Tables stored on HDFS as flat files– Developed by Facebook, now open source
• Pig: large-scale data processing system– 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