Cassandra for SQL Developers - Meetupfiles.meetup.com/4939632/CssandraForSQLDevelopers.pdf ·...

Preview:

Citation preview

Text

Cassandra for SQL DevelopersMaciej Miklas

Glossary

RDBMS vs NoSQL

Data Modelling

RDBMS vs NoSQL

RDBMS NoSQL (Cassandra)ACID:

DBASE:

E

full transaction support

supports “atomic batches”• no sequences / locking • AID inserts within single row • no isolation across rows

partitioning • eventual consistency: coordinator

node(s) could die before data is fully propagated (eventually consistent)

• exception during query execution indicates partly committed transaction (soft state)

supports efficient Inner Joins single query can select data only from

limited horizontal scalability due to relations between tables

horizontally scalable based on partitioning and denormalization

fixed schema schema free until Cassandra 2

CAP Triangle

C

A

PConsistency:

transactions are atomicPartition Tolerance:

operational even if there is no connection between nodes

Availability: read/write are possible

from each client

AP: Cassandra, Dynamo,

CouchDB

CA: RDBMS

CP: MongoDB, BigTable,

Hbase

Data Modelling

RDBMS NoSQL

1. domain model 2. normalised entity model (NF1,…,NF5) 3. queries

1. domain model 2. define queries, create ranking 3. de-normalised

Data modelling

USERSPK NAME EMAIL100 Frido frido@gmx.de

101 Olmo olmo@gmx.de

102 Wilbur wilbur@gmx.de

TWEETSPK USER_FK TWEET_DATE TEXT ICON

201 100 2013-02-01 11:10Another piece of gorgeous Hyrule Warriors art. We're wondering who the girl on the

right is though.happy.jpg

202 100 2013-02-01 11:15 Tetra may be older in this painting but she hasn't lost her sassy spirit. happy.jpg

203 100 2013-02-01 11:20 Amanda's back with the Zelda News roundup for the first day of E3 sad.jpg

204 102 2013-03-02 10:00Hey @Kotaku! For the record, I will

ALWAYS play Zelda in a Smash where Shiek is involved ;)

cat.jpg

205 102 2013-03-03 10:00 @zeldawilliams It's too bad Bowser photobombed it. xD horn.jpg

FOLLOWSPK USER_FK FOLOW_FK301 101 100

302 101 102

Example 1 Display latest tweets by frido@gmx.de

Display latest tweets for frido@gmx.de

USERSPK NAME EMAIL100 Frido frido@gmx.de

101 Olmo olmo@gmx.de

102 Wilbur wilbur@gmx.de

TWEETSPK USER_FK TWEET_DATE TEXT ICON

201 100 2013-02-01 11:10Another piece of gorgeous Hyrule Warriors art. We're wondering who the girl on the

right is though.happy.jpg

202 100 2013-02-01 11:15 Tetra may be older in this painting but she hasn't lost her sassy spirit. happy.jpg

203 100 2013-02-01 11:20 Amanda's back with the Zelda News roundup for the first day of E3 sad.jpg

204 102 2013-03-02 10:00Hey @Kotaku! For the record, I will

ALWAYS play Zelda in a Smash where Shiek is involved ;)

cat.jpg

205 102 2013-03-03 10:00 @zeldawilliams It's too bad Bowser photobombed it. xD horn.jpg

FOLLOWSPK USER_FK FOLOW_FK301 101 100

302 101 102

USERSPK NAME EMAIL100 Frido frido@gmx.de

TWEETSPK USER_FK TWEET_DATE TEXT ICON

201 100 2013-02-01 11:10Another piece of gorgeous Hyrule Warriors art. We're wondering who the girl on the

right is though.happy.jpg

202 100 2013-02-01 11:15 Tetra may be older in this painting but she hasn't lost her sassy spirit. happy.jpg

203 100 2013-02-01 11:20 Amanda's back with the Zelda News roundup for the first day of E3 sad.jpg

Display latest tweets for frido@gmx.de

select tweet_date, text, icon from tweets inner join users on users.pk = tweets.user_fk where

users.email=‘frido@gmx.de’ order by tweets.tweet_date desc

SQL RESULTTWEET_DATE TEXT ICON

2013-02-01 11:10Another piece of gorgeous Hyrule Warriors art. We're wondering who the girl on the

right is though.happy.jpg

2013-02-01 11:15 Tetra may be older in this painting but she hasn't lost her sassy spirit. happy.jpg

2013-02-01 11:20 Amanda's back with the Zelda News roundup for the first day of E3 sad.jpg

Display latest tweets for frido@gmx.de

create table tweets_by_date ( email varchar, icon varchar, tweet_date timestamp, text varchar, primary key (email, tweet_date)) with clustering order by (tweet_date DESC)

Display latest tweets for frido@gmx.de

insert into tweets_by_date (email, icon, tweet_date, text) values ('wilbur@gmx.de', 'horn.jpg','2013-03-03 10:00',

'@zeldawilliams Its too bad Bowser photobombed it. xD'); !

insert into tweets_by_date (email, icon, tweet_date, text) values ('wilbur@gmx.de', 'cat.jpg','2013-03-02 10:00',

'Hey @Kotaku! For the record, I will ALWAYS play Zelda in a Smash where Shiek is involved');

Display latest tweets for frido@gmx.de

Display latest tweets for frido@gmx.de

Display latest tweets for frido@gmx.de

Display latest tweets for frido@gmx.de

TWEETS_BY_DATEEMAIL “2013-02-01 11:20”:”ICON” “2013-02-01 11:20”:”TEXT” “2013-02-01 11:15”:”ICON”

frido@gmx.de sad.jpg Amandas back with the Zelda News roundup for the first day of E3 happy.jpg

create table tweets_by_date ( email varchar, icon varchar, tweet_date timestamp, text varchar, primary key (email, tweet_date)) with clustering order by (tweet_date DESC)

“2013-02-01 11:15”:”TEXT” “2013-02-01 11:10”:”ICON” “2013-02-01 11:10”:”TEXT”Another piece of gorgeous Hyrule Warriors art. We're

wondering who the girl on the right is though. happy.jpg Another piece of gorgeous Hyrule Warriors art. Were wondering who the girl on the right is though.

Display latest tweets for frido@gmx.de

Example 2 Display tweets subscribed by olmo@gmx.de

Display tweets subscribed by olmo@gmx.de

USERSPK NAME EMAIL100 Frido frido@gmx.de

101 Olmo olmo@gmx.de

102 Wilbur wilbur@gmx.de

TWEETSPK USER_FK TWEET_DATE TEXT ICON

201 100 2013-02-01 11:10Another piece of gorgeous Hyrule Warriors art. We're wondering who the girl on the

right is though.happy.jpg

202 100 2013-02-01 11:15 Tetra may be older in this painting but she hasn't lost her sassy spirit. happy.jpg

203 100 2013-02-01 11:20 Amanda's back with the Zelda News roundup for the first day of E3 sad.jpg

204 102 2013-03-02 10:00Hey @Kotaku! For the record, I will

ALWAYS play Zelda in a Smash where Shiek is involved ;)

cat.jpg

205 102 2013-03-03 10:00 @zeldawilliams It's too bad Bowser photobombed it. xD horn.jpg

FOLLOWSPK USER_FK FOLOW_FK301 101 100

302 101 102

select tweet_date, text, icon from users inner join follows on users.pk = follows.user_fk inner join

tweets on follow_fk=tweets.user_fk where users.email=‘olmo@gmx.de’ order by

tweets.tweet_date desc

SQL RESULTTWEET_DATE TEXT ICON

2013-02-01 11:10 Another piece of gorgeous Hyrule Warriors art. We're wondering who the girl on the right is though. happy.jpg

2013-02-01 11:15 Tetra may be older in this painting but she hasn't lost her sassy spirit. happy.jpg

2013-02-01 11:20 Amanda's back with the Zelda News roundup for the first day of E3 sad.jpg

2013-03-02 10:00 Hey @Kotaku! For the record, I will ALWAYS play Zelda in a Smash where Shiek is involved ;) cat.jpg

2013-03-03 10:00 @zeldawilliams It's too bad Bowser photobombed it. xD horn.jpg

Display tweets subscribed by olmo@gmx.de

create table tweets_by_subscription ( email varchar, author_email varchar, icon varchar, tweet_date timestamp, text varchar, primary key (email, tweet_date)) with clustering order by (tweet_date DESC)

Display tweets subscribed by olmo@gmx.de

!

insert into tweets_by_subscription (email, email_author, icon, tweet_date, text) values ('olmo@gmx.de', 'wilbur@gmx.de', 'horn.jpg','2013-03-03 10:00', '@zeldawilliams Its too bad

Bowser photobombed it. xD’); !

insert into tweets_by_subscription (email, email_author, icon, tweet_date, text) values ('olmo@gmx.de', 'wilbur@gmx.de', 'cat.jpg','2013-03-02 10:00', 'Hey @Kotaku! For the record,

I will ALWAYS play Zelda in a Smash where Shiek is involved');

Display tweets subscribed by olmo@gmx.de

Display tweets subscribed by olmo@gmx.de

In the column family with a billion users, looking up users by their email address (a value that is typically unique for each user) is likely to be very inefficient.

!http://www.datastax.com/docs/1.1/ddl/indexes

Thank YOU !

use cqldemo; !drop table if exists tweets_by_date; create table tweets_by_date (email varchar, icon varchar, tweet_date timestamp, text varchar, primary key (email, tweet_date)); !insert into tweets_by_date (email, icon, tweet_date, text) values ('wilbur@gmx.de', 'horn.jpg','2013-03-03 10:00', '@zeldawilliams Its too bad Bowser photobombed it. xD'); insert into tweets_by_date (email, icon, tweet_date, text) values ('wilbur@gmx.de', 'cat.jpg','2013-03-02 10:00', 'Hey @Kotaku! For the record, I will ALWAYS play Zelda in a Smash where Shiek is involved'); insert into tweets_by_date (email, icon, tweet_date, text) values ('frido@gmx.de', 'sad.jpg','2013-02-01 11:20', 'Amandas back with the Zelda News roundup for the first day of E3'); insert into tweets_by_date (email, icon, tweet_date, text) values ('frido@gmx.de', 'happy.jpg','2013-02-01 11:15', 'Tetra may be older in this painting but she hasnt lost her sassy spirit.'); insert into tweets_by_date (email, icon, tweet_date, text) values ('frido@gmx.de', 'happy.jpg', '2013-02-01 11:10', 'Another piece of gorgeous Hyrule Warriors art. Were wondering who the girl on the right is though.'); !drop table if exists tweets_by_subscription; create table tweets_by_subscription (email varchar, email_author varchar, icon varchar, tweet_date timestamp, text varchar, primary key (email, tweet_date)); !insert into tweets_by_subscription (email, email_author, icon, tweet_date, text) values ('olmo@gmx.de', 'wilbur@gmx.de', 'horn.jpg','2013-03-03 10:00', '@zeldawilliams Its too bad Bowser photobombed it. xD'); insert into tweets_by_subscription (email, email_author, icon, tweet_date, text) values ('olmo@gmx.de', 'wilbur@gmx.de', 'cat.jpg','2013-03-02 10:00', 'Hey @Kotaku! For the record, I will ALWAYS play Zelda in a Smash where Shiek is involved'); insert into tweets_by_subscription (email, email_author, icon, tweet_date, text) values ('olmo@gmx.de', 'frido@gmx.de', 'sad.jpg','2013-02-01 11:20', 'Amandas back with the Zelda News roundup for the first day of E3'); insert into tweets_by_subscription (email, email_author, icon, tweet_date, text) values ('olmo@gmx.de', 'frido@gmx.de', 'happy.jpg','2013-02-01 11:15', 'Tetra may be older in this painting but she hasnt lost her sassy spirit.'); insert into tweets_by_subscription (email, email_author, icon, tweet_date, text) values ('olmo@gmx.de', 'frido@gmx.de', 'happy.jpg', '2013-02-01 11:10', 'Another piece of gorgeous Hyrule Warriors art. Were wondering who the girl on the right is though.');

Recommended