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

28
Text Cassandra for SQL Developers Maciej Miklas

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

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 [email protected]

101 Olmo [email protected]

102 Wilbur [email protected]

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 [email protected]

Display latest tweets for [email protected]

USERSPK NAME EMAIL100 Frido [email protected]

101 Olmo [email protected]

102 Wilbur [email protected]

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 [email protected]

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 [email protected]

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

users.email=‘[email protected]’ 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 [email protected]

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 [email protected]

insert into tweets_by_date (email, icon, tweet_date, text) values ('[email protected]', '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 ('[email protected]', '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 [email protected]

Display latest tweets for [email protected]

Display latest tweets for [email protected]

Display latest tweets for [email protected]

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

[email protected] 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 [email protected]

Example 2 Display tweets subscribed by [email protected]

Display tweets subscribed by [email protected]

USERSPK NAME EMAIL100 Frido [email protected]

101 Olmo [email protected]

102 Wilbur [email protected]

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=‘[email protected]’ 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 [email protected]

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 [email protected]

!

insert into tweets_by_subscription (email, email_author, icon, tweet_date, text) values ('[email protected]', '[email protected]', '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 ('[email protected]', '[email protected]', '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 [email protected]

Display tweets subscribed by [email protected]

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 ('[email protected]', '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 ('[email protected]', '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 ('[email protected]', '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 ('[email protected]', '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 ('[email protected]', '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 ('[email protected]', '[email protected]', '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 ('[email protected]', '[email protected]', '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 ('[email protected]', '[email protected]', '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 ('[email protected]', '[email protected]', '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 ('[email protected]', '[email protected]', 'happy.jpg', '2013-02-01 11:10', 'Another piece of gorgeous Hyrule Warriors art. Were wondering who the girl on the right is though.');