Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
1
5 Steps to an Awesome Apache
Cassandra™ Data Model
Patrick McFadin
VP Developer Relations, DataStax
@PatrickMcFadin
Relational Data Models
• 5 normal forms
• Foreign Keys
• Joins
2
deptId First Last
1 Edgar Codd
2 Raymond Boyce
id Dept
1 ...
3
Relational Modeling
4
CREATE TABLE users (
id number(12) NOT NULL ,
firstname nvarchar2(25) NOT NULL ,
lastname nvarchar2(...
Relational Modeling
5
Data
Models
Application
Cassandra Modeling
6
Data
Models
Application
• Think a YouTube competitor
– Users add videos, rate them, comment on
them, etc.
– Can search for videos by tag7
killrvid...
8
killrvideo.com
Confidential
1. Build Application
Workflow
9
© DataStax, All Rights Reserved.
Workflow?
10
User logs in
User selects video
Some Application Workflows in KillrVideo
11
User Logs into
site
Show basic
information
about user
Show videos
added by a
u...
2. Model Your Queries
12
Some of the Entities and Relationships in KillrVideo
13
User
id
firstname
lastname
email
password
Video
id
name
descriptio...
Some Queries in KillrVideo to Support Workflows
14
Users
User Logs into
site
Find user by email
address
Show basic
informa...
Some Queries in KillrVideo to Support Workflows
15
Videos
Search for a
video by tag Find video by tag
Show latest
videos a...
3. Make Your Tables
16
Moving From Workflows
17
Entities
Single Name
User
Comment
Video
Relationships or Look Up
Descriptive Name
Show
comments
p...
“Static” Table
18
CREATE TABLE videos (
videoid uuid,
userid uuid,
name varchar,
description varchar,
location text,
locat...
“Dynamic” Table
19
CREATE TABLE videos_by_tag (
tag text,
videoid uuid,
added_date timestamp,
name text,
preview_image_loc...
Users – The Cassandra Way
20
User Logs into
site
Find user by email
address
Show basic
information
about user
Find user by...
4. Get The Primary Key
Right
21
Partition Key
22
CREATE TABLE videos (
videoid uuid,
userid uuid,
name varchar,
description varchar,
location text,
locati...
Locality
23
1000 Node Cluster
videoid = 06049cbb-dfed-421f-b889-5f649a0de1ed
SELECT name, description, added_date
FROM vid...
Why Dynamic?
24
CREATE TABLE videos_by_tag (
tag text,
videoid uuid,
added_date timestamp,
name text,
preview_image_locati...
Primary key relationship
25
PRIMARY KEY (tag,videoid)
Primary key relationship
26
Partition Key
PRIMARY KEY (tag,videoid)
Primary key relationship
27
Partition Key Clustering Column
PRIMARY KEY (tag,videoid)
Primary key relationship
28
Partition Key
data model
PRIMARY KEY (tag,videoid)
Clustering Column
-5.6
06049cbb-dfed-421f-b889-5f649a0de1ed
Primary key relationship
29
Partition Key
2013-05-16 16:50:002013-05-02 12:30:29...
5. Use Data Types
Effectively
30
Data Types
31
1 - Data Marshalling
2 - Controlling Order
CREATE TABLE videos (
videoid uuid,
data blob,
PRIMARY KEY (video...
Controlling Order
32
• Controls row ordering when used as clustering column
• Default is ASC and can be overridden
INT
VAR...
Special Java Type Matches
33
CQL type Java type
decimal java.math.BigDecimal
float java.lang.Float
double java.lang.Double...
Collections
34
Set
List
Map
preview_thumbnails map<text,text>
Column Name
Column Name
CQL Key Type CQL Value Type
Column N...
What now?
35
Go do it!
36
Open Source Open Source with Support Full Enterprise Edition
DataStax Distribution of
Apache Cassandra™
DataS...
Confidential
Thank you
37
© DataStax, All Rights Reserved.
Nächste SlideShare
Wird geladen in …5
×

5 Steps to an Awesome Apache Cassandra™ Data Model

465 Aufrufe

Veröffentlicht am

Picking the right data model is the most critical step to ensuring you get the most out of your Apache Cassandra implementation. For years, Patrick McFadin, DataStax’s own VP of Developer Relations, has been a go-to resource for enterprises wanting to learn how to do this right. In this expert session, Patrick will walk you through how to apply schema design based on your use case and the associated query patterns—but that’s not all. He’ll also share his five-step framework for implementing an awesome Cassandra data model.

View recording: https://youtu.be/wyq_hGFz6Sc

Explore all DataStax webinars: https://www.datastax.com/resources/webinars

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

5 Steps to an Awesome Apache Cassandra™ Data Model

  1. 1. 1 5 Steps to an Awesome Apache Cassandra™ Data Model Patrick McFadin VP Developer Relations, DataStax @PatrickMcFadin
  2. 2. Relational Data Models • 5 normal forms • Foreign Keys • Joins 2 deptId First Last 1 Edgar Codd 2 Raymond Boyce id Dept 1 Engineering 2 Math Employees Department
  3. 3. 3
  4. 4. Relational Modeling 4 CREATE TABLE users ( id number(12) NOT NULL , firstname nvarchar2(25) NOT NULL , lastname nvarchar2(25) NOT NULL, email nvarchar2(50) NOT NULL, password nvarchar2(255) NOT NULL, created_date timestamp(6), PRIMARY KEY (id), CONSTRAINT email_uq UNIQUE (email) ); -- Users by email address index CREATE INDEX idx_users_email ON users (email); • Create entity table • Add constraints • Index fields • Foreign Key relationships CREATE TABLE videos ( id number(12), userid number(12) NOT NULL, name nvarchar2(255), description nvarchar2(500), location nvarchar2(255), location_type int, added_date timestamp, CONSTRAINT users_userid_fk FOREIGN KEY (userid) REFERENCES users (Id) ON DELETE CASCADE, PRIMARY KEY (id) );
  5. 5. Relational Modeling 5 Data Models Application
  6. 6. Cassandra Modeling 6 Data Models Application
  7. 7. • Think a YouTube competitor – Users add videos, rate them, comment on them, etc. – Can search for videos by tag7 killrvideo.com
  8. 8. 8 killrvideo.com
  9. 9. Confidential 1. Build Application Workflow 9 © DataStax, All Rights Reserved.
  10. 10. Workflow? 10 User logs in User selects video
  11. 11. Some Application Workflows in KillrVideo 11 User Logs into site Show basic information about user Show videos added by a user Show comments posted by a user Search for a video by tag Show latest videos added to the site Show comments for a video Show ratings for a video Show video and its details
  12. 12. 2. Model Your Queries 12
  13. 13. Some of the Entities and Relationships in KillrVideo 13 User id firstname lastname email password Video id name description location preview_image tags features Comment comment id adds timestamp posts timestamp 1 n n 1 1 n n m rates rating
  14. 14. Some Queries in KillrVideo to Support Workflows 14 Users User Logs into site Find user by email address Show basic information about user Find user by id Comments Show comments for a video Find comments by video (latest first) Show comments posted by a user Find comments by user (latest first) Ratings Show ratings for a video Find ratings by video
  15. 15. Some Queries in KillrVideo to Support Workflows 15 Videos Search for a video by tag Find video by tag Show latest videos added to the site Find videos by date (latest first) Show video and its details Find video by id Show videos added by a user Find videos by user (latest first)
  16. 16. 3. Make Your Tables 16
  17. 17. Moving From Workflows 17 Entities Single Name User Comment Video Relationships or Look Up Descriptive Name Show comments posted by a user Find comments by user (latest first) Search for a video by tag Find video by tag
  18. 18. “Static” Table 18 CREATE TABLE videos ( videoid uuid, userid uuid, name varchar, description varchar, location text, location_type int, preview_thumbnails map<text,text>, tags set<varchar>, added_date timestamp, PRIMARY KEY (videoid) ); Table Name Column Name Column CQL Type Primary Key Designation Partition Key Show video and its details Find video by id
  19. 19. “Dynamic” Table 19 CREATE TABLE videos_by_tag ( tag text, videoid uuid, added_date timestamp, name text, preview_image_location text, tagged_date timestamp, PRIMARY KEY (tag, videoid) ); Partition Key Clustering Column Search for a video by tag Find video by tag
  20. 20. Users – The Cassandra Way 20 User Logs into site Find user by email address Show basic information about user Find user by id CREATE TABLE user_credentials ( email text, password text, userid uuid, PRIMARY KEY (email) ); CREATE TABLE users ( userid uuid, firstname text, lastname text, email text, created_date timestamp, PRIMARY KEY (userid) );
  21. 21. 4. Get The Primary Key Right 21
  22. 22. Partition Key 22 CREATE TABLE videos ( videoid uuid, userid uuid, name varchar, description varchar, location text, location_type int, preview_thumbnails map<text,text>, tags set<varchar>, added_date timestamp, PRIMARY KEY (videoid) ); Primary Key Designation Partition Key
  23. 23. Locality 23 1000 Node Cluster videoid = 06049cbb-dfed-421f-b889-5f649a0de1ed SELECT name, description, added_date FROM videos WHERE videoid = 06049cbb-dfed-421f-b889-5f649a0de1ed; Partition Key: REQUIRED
  24. 24. Why Dynamic? 24 CREATE TABLE videos_by_tag ( tag text, videoid uuid, added_date timestamp, name text, preview_image_location text, tagged_date timestamp, PRIMARY KEY (tag, videoid) ); Partition Key Clustering Column
  25. 25. Primary key relationship 25 PRIMARY KEY (tag,videoid)
  26. 26. Primary key relationship 26 Partition Key PRIMARY KEY (tag,videoid)
  27. 27. Primary key relationship 27 Partition Key Clustering Column PRIMARY KEY (tag,videoid)
  28. 28. Primary key relationship 28 Partition Key data model PRIMARY KEY (tag,videoid) Clustering Column
  29. 29. -5.6 06049cbb-dfed-421f-b889-5f649a0de1ed Primary key relationship 29 Partition Key 2013-05-16 16:50:002013-05-02 12:30:29 873ff430-9c23-4e60-be5f-278ea2bb21bd PRIMARY KEY (tag,videoid) Clustering Column data model 49f64d40-7d89-4890-b910-dbf923563a33 2013-06-11 11:00:00
  30. 30. 5. Use Data Types Effectively 30
  31. 31. Data Types 31 1 - Data Marshalling 2 - Controlling Order CREATE TABLE videos ( videoid uuid, data blob, PRIMARY KEY (videoid) ); Full Schema! CREATE TABLE videos ( videoid uuid, userid uuid, name varchar, description varchar, location text, location_type int, preview_thumbnails map<text,text>, tags set<varchar>, added_date timestamp, PRIMARY KEY (videoid) );
  32. 32. Controlling Order 32 • Controls row ordering when used as clustering column • Default is ASC and can be overridden INT VARCHAR DATE TIMESTAMP TIMEUUID
  33. 33. Special Java Type Matches 33 CQL type Java type decimal java.math.BigDecimal float java.lang.Float double java.lang.Double varint java.math.BigInteger Most types are obvious to Java, but…
  34. 34. Collections 34 Set List Map preview_thumbnails map<text,text> Column Name Column Name CQL Key Type CQL Value Type Column Name CREATE TABLE videos ( videoid uuid, userid uuid, name varchar, description varchar, location text, location_type int, preview_thumbnails map<text,text>, tags set<varchar>, added_date timestamp, PRIMARY KEY (videoid) ); CQL Type tags list<varchar> tags set<varchar> CQL Type: For Ordering
  35. 35. What now? 35
  36. 36. Go do it! 36 Open Source Open Source with Support Full Enterprise Edition DataStax Distribution of Apache Cassandra™ DataStax EnterpriseApache Cassandra™
  37. 37. Confidential Thank you 37 © DataStax, All Rights Reserved.

×