Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database and MapReduce Based on slides from Jimmy Lins lecture slides (http://www.umiacs.umd.edu/~jimmylin/clou d-2010-Spring/index.html) (licensed under.

Similar presentations


Presentation on theme: "Database and MapReduce Based on slides from Jimmy Lins lecture slides (http://www.umiacs.umd.edu/~jimmylin/clou d-2010-Spring/index.html) (licensed under."— Presentation transcript:

1 Database and MapReduce Based on slides from Jimmy Lins lecture slides (http://www.umiacs.umd.edu/~jimmylin/clou d-2010-Spring/index.html) (licensed under Creation Commons Attribution 3.0 License)

2 RoadMap Role of relational databases in todays 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 – Whats in store for the future?

3 Relational Database Basics

4 Basic Structure Formally, given sets D 1, D 2, …. D n a relation r is a subset of D 1 x D 2 x … x D n Thus, a relation is a set of n-tuples (a 1, a 2, …, a n ) where each a i D i 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

5 Relation Schema A 1, A 2, …, A n are attributes R = (A 1, A 2, …, A n ) is a relation schema Example: Customer_schema = (customer_name, customer_street, customer_city) r(R) is a relation on the relation schema R Example: customer (Customer_schema)

6 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 Jones Smith Curry Lindsay customer_name Main North Park customer_street Harrison Rye Pittsfield customer_city customer attributes (or columns) tuples (or rows)

7 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)

8 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)

9 Relational Algebra Primitives – Projection ( ) – Selection ( ) – Cartesian product ( ) – Set union ( ) – Set difference ( ) – Rename ( ) Other operations – Join ( ) – Group by… aggregation – …

10 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)

11 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: OReilly Blog post by Joseph Hellerstein (11/19/2008)

12 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

13 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?

14 OLTP/OLAP Architecture OLTPOLAP ETL (Extract, Transform, and Load)

15 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

16 Warehouse Models & Operators Data Models – relations – stars & snowflakes – cubes Operators – slice & dice – roll-up, drill down – pivoting – other 16

17 Star 17

18 Star Schema 18

19 Terms Fact table Dimension tables Measures 19

20 Dimension Hierarchies 20 store sType cityregion snowflake schema constellations

21 Cube 21 Fact table view: Multi-dimensional cube: dimensions = 2

22 3-D Cube 22 day 2 day 1 dimensions = 3 Multi-dimensional cube:Fact table view:

23 ROLAP vs. MOLAP ROLAP: Relational On-Line Analytical Processing MOLAP: Multi-Dimensional On-Line Analytical Processing 23

24 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.

25 Aggregates 25 Add up amounts for day 1 In SQL: SELECT sum(amt) FROM SALE WHERE date = 1 81

26 Aggregates 26 Add up amounts by day In SQL: SELECT date, sum(amt) FROM SALE GROUP BY date

27 Another Example 27 Add up amounts by day, product In SQL: SELECT date, sum(amt) FROM SALE GROUP BY date, prodId drill-down rollup

28 Aggregates Operators: sum, count, max, min, median, ave Having clause Using dimension hierarchy – average by region (within store) – maximum by month (within date) 28

29 Cube Aggregation 29 day 2 day drill-down rollup Example: computing sums

30 Cube Operators 30 day 2 day sale(c1,*,*) sale(*,*,*) sale(c2,p2,*)

31 Extended Cube 31 day 2 day 1 * sale(*,p2,*)

32 Aggregation Using Hierarchies 32 day 2 day 1 customer region country (customer c1 in Region A; customers c2, c3 in Region B)

33 Pivoting 33 day 2 day 1 Multi-dimensional cube: Fact table view:

34 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

35 OLTP/OLAP Architecture: Hadoop? OLTPOLAP ETL (Extract, Transform, and Load) Hadoop here? What about here?

36 OLTP/OLAP/Hadoop Architecture OLTPOLAP ETL (Extract, Transform, and Load) Hadoop Why does this make sense?

37 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!

38 MapReduce algorithms for processing relational data

39 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 (v 1, r), (v 3, r), (v 4, r), (v 8, r)…

40 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, v 1 ) – Let execution framework do the sorting – Preserve state across multiple key-value pairs to handle processing – Anything else we need to do?

41 Value-to-Key Conversion k (v 1, r), (v 4, r), (v 8, r), (v 3, r)… (k, v 1 ) (v 1, r) Before After (k, v 3 ) (v 3, r) (k, v 4 ) (v 4, r) (k, v 8 ) (v 8, r) Values arrive in arbitrary order… … Values arrive in sorted order… Process by preserving state across multiple keys Remember to partition correctly!

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

43 Relational Algebra Primitives – Projection ( ) – Selection ( ) – Cartesian product ( ) – Set union ( ) – Set difference ( ) – Rename ( ) Other operations – Join ( ) – Group by… aggregation – …

44 Projection R1R1 R2R2 R3R3 R4R4 R5R5 R1R1 R2R2 R3R3 R4R4 R5R5

45 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!

46 Selection R1R1 R2R2 R3R3 R4R4 R5R5 R1R1 R3R3

47 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!

48 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

49 Relational Joins Source: Microsoft Office Clip Art

50 Relational Joins R1R1 R2R2 R3R3 R4R4 S1S1 S2S2 S3S3 S4S4 R1R1 S2S2 R2R2 S4S4 R3R3 S1S1 R4R4 S3S3

51 Natural Join Operation – Example Relations r, s: AB CD aababaabab B D aaabbaaabb E r AB CD aaaabaaaab E s r s

52 Natural Join Example R1 S1 R1 S1 =

53 Types of Relationships One-to-OneOne-to-Many Many-to-Many

54 Join Algorithms in MapReduce Reduce-side join Map-side join In-memory join – Striped variant – Memcached variant

55 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

56 Reduce-side Join: 1-to-1 R1R1 R4R4 S2S2 S3S3 R1R1 R4R4 S2S2 S3S3 keysvalues Map R1R1 R4R4 S2S2 S3S3 keysvalues Reduce Note: no guarantee if R is going to come first or S

57 Reduce-side Join: 1-to-many R1R1 S2S2 S3S3 R1R1 S2S2 S3S3 S9S9 keysvalues Map R1R1 S2S2 keysvalues Reduce S9S9 S3S3 … Whats the problem?

58 Reduce-side Join: V-to-K Conversion R1R1 keysvalues In reducer… S2S2 S3S3 S9S9 R4R4 S3S3 S7S7 New key encountered: hold in memory Cross with records from other set New key encountered: hold in memory Cross with records from other set

59 Reduce-side Join: many-to-many R1R1 keysvalues In reducer… S2S2 S3S3 S9S9 Hold in memory Cross with records from other set R5R5 R8R8 Whats the problem?

60 Map-side Join: Basic Idea Assume two datasets are sorted by the join key: R1R1 R2R2 R3R3 R4R4 S1S1 S2S2 S3S3 S4S4 A sequential scan through both datasets to join (called a merge join in database terminology)

61 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?

62 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

63 In-Memory Join: Variants Striped variant: – R too big to fit into memory? – Divide R into R 1, R 2, R 3, … s.t. each R n fits into memory – Perform in-memory join: n, R n S – Take the union of all join results Memcached join: – Load R into memcached – Replace in-memory hash lookup with memcached lookup

64 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)

65 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)

66 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

67 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

68 Evolving roles for relational database and MapReduce

69 OLTP/OLAP/Hadoop Architecture OLTPOLAP ETL (Extract, Transform, and Load) Hadoop Why does this make sense?

70 Need for High-Level Languages Hadoop is great for large-data processing! – But writing Java programs for everything is verbose and slow – Analysts dont want to (or cant) write Java Solution: develop higher-level data processing languages – Hive: HQL is like SQL – Pig: Pig Latin is a bit like Perl

71 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


Download ppt "Database and MapReduce Based on slides from Jimmy Lins lecture slides (http://www.umiacs.umd.edu/~jimmylin/clou d-2010-Spring/index.html) (licensed under."

Similar presentations


Ads by Google