oracle to cassandra core concepts guide pt. 2

43
©2015 DataStax Confidential. Do not distribute without consent. @RachelPedreschi & @PatrickMcFadin Rachel Pedreschi & Patrick McFadin Evangelists for Apache Cassandra Oracle to Cassandra Core Concepts Guide Part 2: Data Model Strikes Back 1

Upload: planet-cassandra

Post on 18-Aug-2015

185 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Oracle to Cassandra Core Concepts Guide Pt. 2

©2015 DataStax Confidential. Do not distribute without consent.

@RachelPedreschi & @PatrickMcFadin

Rachel Pedreschi & Patrick McFadinEvangelists for Apache Cassandra

Oracle to Cassandra Core Concepts Guide Part 2: Data Model Strikes Back

1

Page 2: Oracle to Cassandra Core Concepts Guide Pt. 2

Where are we now?

Pt. 1 - Transition from Oracle to Cassandra How does it work? What are the differences?

Pt. 2 - Cassandra Data Model All the details on how to use Cassandra Query Language (CQL)

Pt. 3 - Building a Cassandra Application Wrap it all up with a top down application design. Combining everything we’ve learned plus a more.

Page 3: Oracle to Cassandra Core Concepts Guide Pt. 2

Application

hash(key) => token(43)

replication factor = 3

80

10

3050

70

60

40

20

Page 4: Oracle to Cassandra Core Concepts Guide Pt. 2

“Static” Table

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 NameColumn CQL Type

Primary Key Designation Partition Key

Page 5: Oracle to Cassandra Core Concepts Guide Pt. 2

Row

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Page 6: Oracle to Cassandra Core Concepts Guide Pt. 2

Partition

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Page 7: Oracle to Cassandra Core Concepts Guide Pt. 2

Table Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Column 1

Column 2

Column 3

Column 4

Column 1

Column 2

Column 3

Column 4

Column 1

Column 2

Column 3

Column 4

Partition Key 2

Partition Key 2

Partition Key 2

Page 8: Oracle to Cassandra Core Concepts Guide Pt. 2

Keyspace

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 1

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Column 1

Partition Key 2

Column 2

Column 3

Column 4

Table 1 Table 2Keyspace 1

Page 9: Oracle to Cassandra Core Concepts Guide Pt. 2

Insert

INSERT INTO videos (videoid, name, userid, description, location, location_type, preview_thumbnails, tags, added_date, metadata) VALUES (06049cbb-dfed-421f-b889-5f649a0de1ed,'The data model is dead. Long live the data model.',9761d3d7-7fbd-4269-9988-6cfd4e188678, 'First in a three part series for Cassandra Data Modeling','http://www.youtube.com/watch?v=px6U2n74q3g',1, {'YouTube':'http://www.youtube.com/watch?v=px6U2n74q3g'},{'cassandra','data model','relational','instruction'}, '2013-05-02 12:30:29');

Table Name Fields

Values

Partition Key: Required

Page 10: Oracle to Cassandra Core Concepts Guide Pt. 2

Partition keys

06049cbb-dfed-421f-b889-5f649a0de1ed Murmur3 Hash Token = 7224631062609997448

873ff430-9c23-4e60-be5f-278ea2bb21bd Murmur3 Hash Token = -6804302034103043898

Consistent hash. 128 bit number between 2-63 and 264

INSERT INTO videos (videoid, name, userid, description) VALUES (06049cbb-dfed-421f-b889-5f649a0de1ed,'The data model is dead. Long live the data model.’, 9761d3d7-7fbd-4269-9988-6cfd4e188678, 'First in a three part series for Cassandra Data Modeling');

INSERT INTO videos (videoid, name, userid, description) VALUES (873ff430-9c23-4e60-be5f-278ea2bb21bd,'Become a Super Modeler’, 9761d3d7-7fbd-4269-9988-6cfd4e188678, 'Second in a three part series for Cassandra Data Modeling');

Page 11: Oracle to Cassandra Core Concepts Guide Pt. 2

“Dynamic” Table

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

Page 12: Oracle to Cassandra Core Concepts Guide Pt. 2

Primary key relationship

PRIMARY KEY (tag,videoid)

Page 13: Oracle to Cassandra Core Concepts Guide Pt. 2

Primary key relationship

Partition Key

PRIMARY KEY (tag,videoid)

Page 14: Oracle to Cassandra Core Concepts Guide Pt. 2

Primary key relationship

Partition Key Clustering Column

PRIMARY KEY (tag,videoid)

Page 15: Oracle to Cassandra Core Concepts Guide Pt. 2

Primary key relationship

Partition Key

data model

PRIMARY KEY (tag,videoid)

Clustering Column

Page 16: Oracle to Cassandra Core Concepts Guide Pt. 2

-5.6

06049cbb-dfed-421f-b889-5f649a0de1ed

Primary key relationship

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 model49f64d40-7d89-4890-b910-dbf923563a33

2013-06-11 11:00:00

Page 17: Oracle to Cassandra Core Concepts Guide Pt. 2

Select

name | description | added_date---------------------------------------------------+----------------------------------------------------------+--------------------------The data model is dead. Long live the data model. | First in a three part series for Cassandra Data Modeling | 2013-05-02 12:30:29-0700

SELECT name, description, added_dateFROM videosWHERE videoid = 06049cbb-dfed-421f-b889-5f649a0de1ed;

FieldsTable Name

Primary Key: Partition Key Required

Page 18: Oracle to Cassandra Core Concepts Guide Pt. 2

Controlling OrderCREATE TABLE raw_weather_data ( wsid text, year int, month int, day int, hour int, temperature double, PRIMARY KEY ((wsid), year, month, day, hour) ) WITH CLUSTERING ORDER BY (year DESC, month DESC, day DESC, hour DESC);

INSERT INTO raw_weather_data(wsid,year,month,day,hour,temperature) VALUES (‘10010:99999’,2005,12,1,10,-5.6);

INSERT INTO raw_weather_data(wsid,year,month,day,hour,temperature) VALUES (‘10010:99999’,2005,12,1,9,-5.1);

INSERT INTO raw_weather_data(wsid,year,month,day,hour,temperature) VALUES (‘10010:99999’,2005,12,1,8,-4.9);

INSERT INTO raw_weather_data(wsid,year,month,day,hour,temperature) VALUES (‘10010:99999’,2005,12,1,7,-5.3);

Page 19: Oracle to Cassandra Core Concepts Guide Pt. 2

Clustering

200510010:99999 12 1 10

200510010:99999 12 1 9

raw_weather_data

-5.6

-5.1

200510010:99999 12 1 8

200510010:99999 12 1 7

-4.9

-5.3

Order By

DESC

Page 20: Oracle to Cassandra Core Concepts Guide Pt. 2

Write PathClient INSERT INTO raw_weather_data(wsid,year,month,day,hour,temperature)

VALUES (‘10010:99999’,2005,12,1,7,-5.3);

year 1wsid 1 month 1 day 1 hour 1

year 2wsid 2 month 2 day 2 hour 2

Memtable

SSTable

SSTable

SSTable

SSTable

Node

Commit Log Data * Compaction *

Temp

Temp

Page 21: Oracle to Cassandra Core Concepts Guide Pt. 2

Storage Model - Logical View

2005:12:1:10

-5.6

2005:12:1:9

-5.1

2005:12:1:8

-4.9

10010:99999

10010:99999

10010:99999

wsid hour temperature

2005:12:1:7

-5.310010:99999

SELECT wsid, hour, temperatureFROM raw_weather_dataWHERE wsid=‘10010:99999’ AND year = 2005 AND month = 12 AND day = 1;

Page 22: Oracle to Cassandra Core Concepts Guide Pt. 2

2005:12:1:10

-5.6 -5.3-4.9-5.1

Storage Model - Disk Layout

2005:12:1:9 2005:12:1:810010:99999

2005:12:1:7

Merged, Sorted and Stored Sequentially

SELECT wsid, hour, temperatureFROM raw_weather_dataWHERE wsid=‘10010:99999’ AND year = 2005 AND month = 12 AND day = 1;

Page 23: Oracle to Cassandra Core Concepts Guide Pt. 2

2005:12:1:10

-5.6

2005:12:1:11

-4.9 -5.3-4.9-5.1

Storage Model - Disk Layout

2005:12:1:9 2005:12:1:810010:99999

2005:12:1:7

Merged, Sorted and Stored Sequentially

SELECT wsid, hour, temperatureFROM raw_weather_dataWHERE wsid=‘10010:99999’ AND year = 2005 AND month = 12 AND day = 1;

Page 24: Oracle to Cassandra Core Concepts Guide Pt. 2

2005:12:1:10

-5.6

2005:12:1:11

-4.9 -5.3-4.9-5.1

Storage Model - Disk Layout

2005:12:1:9 2005:12:1:810010:99999

2005:12:1:7

Merged, Sorted and Stored Sequentially

SELECT wsid, hour, temperatureFROM raw_weather_dataWHERE wsid=‘10010:99999’ AND year = 2005 AND month = 12 AND day = 1;

2005:12:1:12

-5.4

Page 25: Oracle to Cassandra Core Concepts Guide Pt. 2

Read PathClient

SSTableSSTable

SSTable

Node

Data

SELECT wsid,hour,temperatureFROM raw_weather_dataWHERE wsid='10010:99999'AND year = 2005 AND month = 12 AND day = 1 AND hour >= 7 AND hour <= 10;

year 1wsid 1 month 1 day 1 hour 1

year 2wsid 2 month 2 day 2 hour 2

Memtable

Temp

Temp

Page 26: Oracle to Cassandra Core Concepts Guide Pt. 2

Query patterns• Range queries • “Slice” operation on disk

Single seek on disk

10010:99999

Partition key for locality

SELECT wsid,hour,temperatureFROM raw_weather_dataWHERE wsid='10010:99999'AND year = 2005 AND month = 12 AND day = 1 AND hour >= 7 AND hour <= 10;

2005:12:1:10

-5.6 -5.3-4.9-5.1

2005:12:1:9 2005:12:1:8 2005:12:1:7

Page 27: Oracle to Cassandra Core Concepts Guide Pt. 2

Query patterns• Range queries • “Slice” operation on disk

Programmers like this

Sorted by event_time2005:12:1:10

-5.6

2005:12:1:9

-5.1

2005:12:1:8

-4.9

10010:99999

10010:99999

10010:99999

weather_station hour temperature

2005:12:1:7

-5.310010:99999

SELECT weatherstation,hour,temperature FROM temperature WHERE weatherstation_id=‘10010:99999' AND year = 2005 AND month = 12 AND day = 1 AND hour >= 7 AND hour <= 10;

Page 28: Oracle to Cassandra Core Concepts Guide Pt. 2

Other New and Not-so-New-but-different things

Page 29: Oracle to Cassandra Core Concepts Guide Pt. 2

CollectionsSet

tags set<varchar>

CQL Type: For Ordering

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

Page 30: Oracle to Cassandra Core Concepts Guide Pt. 2

CollectionsSet

List

Column Name

Column Name

CQL Type

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

tags set<varchar>

CQL Type: For Ordering

tags set<varchar>

Page 31: Oracle to Cassandra Core Concepts Guide Pt. 2

CollectionsSet

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 set<varchar>

tags set<varchar>

CQL Type: For Ordering

Page 32: Oracle to Cassandra Core Concepts Guide Pt. 2

Aggregates (Sort of)

*As of Cassandra 2.2

•Built-in: avg, min, max, count(<column name>) •Runs on server •Always use with partition key

Page 33: Oracle to Cassandra Core Concepts Guide Pt. 2

User Defined Functions

CREATE FUNCTION maxI(current int, candidate int) CALLED ON NULL INPUTRETURNS int LANGUAGE java AS'if (current == null) return candidate; else return Math.max(current, candidate);' ; CREATE AGGREGATE maxAgg(int) SFUNC maxISTYPE intINITCOND null;

CQL Type

Pure Function

SELECT maxAgg(temperature) FROM raw_weather_dataWHERE wsid='10010:99999' AND year = 2005 AND month = 12 AND day = 1

Aggregate usingfunction overpartition

Page 34: Oracle to Cassandra Core Concepts Guide Pt. 2

Lightweight Transactions

Don’t overwrite!

INSERT INTO videos (videoid, name, userid, description, location, location_type, preview_thumbnails, tags, added_date, metadata) VALUES (06049cbb-dfed-421f-b889-5f649a0de1ed,'The data model is dead. Long live the data model.',9761d3d7-7fbd-4269-9988-6cfd4e188678, 'First in a three part series for Cassandra Data Modeling','http://www.youtube.com/watch?v=px6U2n74q3g',1, {'YouTube':'http://www.youtube.com/watch?v=px6U2n74q3g'},{'cassandra','data model','relational','instruction'}, '2013-05-02 12:30:29’) IF NOT EXISTS;

Page 35: Oracle to Cassandra Core Concepts Guide Pt. 2

Lightweight Transactions

No-op. Don’t throw error

CREATE TABLE IF NOT EXISTS videos_by_tag ( tag text, videoid uuid, added_date timestamp, name text, preview_image_location text, tagged_date timestamp, PRIMARY KEY (tag, videoid) );

Page 36: Oracle to Cassandra Core Concepts Guide Pt. 2

Regular Update

UPDATE videosSET name = 'The data model is dead. Long live the data model.'WHERE id = 06049cbb-dfed-421f-b889-5f649a0de1ed;

Table Name Fields to Update: Not in Primary Key

Primary Key

Page 37: Oracle to Cassandra Core Concepts Guide Pt. 2

Lightweight Transactions

Don’t overwrite!

UPDATE videosSET name = 'The data model is dead. Long live the data model.'WHERE id = 06049cbb-dfed-421f-b889-5f649a0de1ed IF userid = 9761d3d7-7fbd-4269-9988-6cfd4e188678;

Page 38: Oracle to Cassandra Core Concepts Guide Pt. 2

Deleting Data

Page 39: Oracle to Cassandra Core Concepts Guide Pt. 2

Delete

DELETE FROM videosWHERE id = 06049cbb-dfed-421f-b889-5f649a0de1ed;

Table Name

Primary Key: Required

Page 40: Oracle to Cassandra Core Concepts Guide Pt. 2

Expiring Data

Time To Live = TTL

INSERT INTO videos (videoid, name, userid, description, location, location_type, preview_thumbnails, tags, added_date, metadata) VALUES (06049cbb-dfed-421f-b889-5f649a0de1ed,'The data model is dead. Long live the data model.',9761d3d7-7fbd-4269-9988-6cfd4e188678, 'First in a three part series for Cassandra Data Modeling','http://www.youtube.com/watch?v=px6U2n74q3g',1, {'YouTube':'http://www.youtube.com/watch?v=px6U2n74q3g'},{'cassandra','data model','relational','instruction'}, '2013-05-02 12:30:29’) USING TTL = 2592000

Expire Data: 30 Days

Page 41: Oracle to Cassandra Core Concepts Guide Pt. 2

What about joins??

Page 42: Oracle to Cassandra Core Concepts Guide Pt. 2

Oracle to Cassandra Core Concepts Guide Pt. 3 Tired of timeouts? Cursing your cursors? Join the distributed revolution and bring your dev team into application nirvana. You won’t believe how easy it is to be code complete on your next big project. We will show you how to lead your devs away from the clutches of the DBA and be in control of their own data destiny. Discover the methodology that will make your Cassandra project epic.

Stay tuned!

Page 43: Oracle to Cassandra Core Concepts Guide Pt. 2

RachelP50 or PatrickM50- 50% off Priority PassRachelPCert or PatrickMCert- 25% Certification