SlideShare a Scribd company logo
DATASTAX C*OLLEGE CREDIT:

DATA MODELLING FOR
 APACHE CASSANDRA
                      Aaron Morton
Apache Cassandra Committer, Data Stax MVP for Apache Cassandra
                      @aaronmorton
                   www.thelastpickle.com


            Licensed under a Creative Commons Attribution-NonCommercial 3.0 New Zealand License
General Guidelines
   API Choice
    Example
Cassandra is good at

reading data from a row in the
      order it is stored.
Typically an efficient data model will

 denormalize data and use the
    storage engine order.
To create a good data model

 understand the queries your
    application requires.
General Guidelines
   API Choice
    Example
Multiple API’s?

  initially only a Thrift / RPC
 API, used by language specific
              clients.
Multiple API’s...

   Cassandra Query Language
   (CQL) started as a higher
  level, declarative alternative.
Multiple API’s...

  CQL 3 brings many changes.
    Currently in Beta in
       Cassandra v1.1
CQL 3 uses

  a Table Orientated, Schema
      Driven, Data Model.
       (I said it had many changes.)
General Guidelines
   API Choice
    Example
Twitter Clone
  Previously done with Thrift at WDCNZ

   “Hello @World #Cassandra - Apache
           Cassandra in action”
        http://vimeo.com/49762233
Twitter clone...

   using CQL 3 via the cqlsh
            tool.
       bin/cqlsh -3
Queries?
        * Post Tweet to Followers
             * Get Tweet by ID
           * List Tweets by User
      * List Tweets in User Timeline
              * List Followers
Keyspace is

     a namespace container.
Our Keyspace
CREATE KEYSPACE
     cass_college
WITH
     strategy_class = 'NetworkTopologyStrategy'
AND
    strategy_options:datacenter1 = 1;
Table is

   a sparse collection of well
   known, ordered columns.
First Table
CREATE TABLE User
(
    user_name text,
    password text,
    real_name text,
    PRIMARY KEY (user_name)
);
Some users...
cqlsh:cass_college> INSERT INTO User
                ...     (user_name, password, real_name)
                ... VALUES
                ...     ('fred', 'sekr8t', 'Mr Foo');

cqlsh:cass_college> select * from User;
 user_name | password | real_name
-----------+----------+-----------
      fred |   sekr8t |    Mr Foo
Some users...
cqlsh:cass_college> INSERT INTO User
                ...     (user_name, password)
                ... VALUES
                ...     ('bob', 'pwd');


cqlsh:cass_college> select * from User where user_name =
'bob';
 user_name | password | real_name
-----------+----------+-----------
       bob |      pwd |      null
Data Model (so far)

                      User
Data Model (so far)
      CF /
                   User
      Value



    user_name   Primary Key
Tweet Table
CREATE TABLE Tweet
(
    tweet_id    bigint,
    body        text,
    user_name   text,
    timestamp   timestamp,
    PRIMARY KEY (tweet_id)
);
Tweet Table...
cqlsh:cass_college> INSERT INTO Tweet
                ...     (tweet_id, body, user_name, timestamp)
                ... VALUES
                ...     (1, 'The Tweet','fred',1352150816917);

cqlsh:cass_college> select * from Tweet where tweet_id = 1;
 tweet_id | body      | timestamp                | user_name
----------+-----------+--------------------------+-----------
        1 | The Tweet | 2012-11-06 10:26:56+1300 |      fred
Data Model (so far)
      CF /
                   User         Tweet
      Value



    user_name   Primary Key      Field




    tweet_id                  Primary Key
UserTweets Table
CREATE TABLE UserTweets
(
    tweet_id    bigint,
    user_name   text,
    body        text,
    timestamp   timestamp,
    PRIMARY KEY (user_name, tweet_id)
);
UserTweets Table...
cqlsh:cass_college> INSERT INTO UserTweets
                ...     (tweet_id, body, user_name, timestamp)
                ... VALUES
                ...     (1, 'The Tweet','fred',1352150816917);

cqlsh:cass_college> select * from UserTweets where
user_name='fred';

 user_name | tweet_id | body      | timestamp
-----------+----------+-----------+--------------------------
      fred |        1 | The Tweet | 2012-11-06 10:26:56+1300
UserTweets Table...
cqlsh:cass_college> select * from UserTweets where
user_name='fred' and tweet_id=1;

 user_name | tweet_id | body      | timestamp
-----------+----------+-----------+--------------------------
      fred |        1 | The Tweet | 2012-11-06 10:26:56+1300
UserTweets Table...
cqlsh:cass_college> INSERT INTO UserTweets
                ...     (tweet_id, body, user_name, timestamp)
                ... VALUES
                ...     (2, 'Second Tweet', 'fred', 1352150816918);

cqlsh:cass_college> select * from UserTweets where user_name = 'fred';
 user_name | tweet_id | body         | timestamp
-----------+----------+--------------+--------------------------
      fred |        1 |    The Tweet | 2012-11-06 10:26:56+1300
      fred |        2 | Second Tweet | 2012-11-06 10:26:56+1300
UserTweets Table...
cqlsh:cass_college> select * from UserTweets where user_name = 'fred' order by
tweet_id desc;

 user_name | tweet_id | body         | timestamp
-----------+----------+--------------+--------------------------
      fred |        2 | Second Tweet | 2012-11-06 10:26:56+1300
      fred |        1 |    The Tweet | 2012-11-06 10:26:56+1300
UserTimeline
CREATE TABLE UserTimeline
(
    tweet_id    bigint,
    user_name   text,
    body        text,
    timestamp   timestamp,
    PRIMARY KEY (user_name, tweet_id)
);
Data Model (so far)
   CF /                                     User          User
                User         Tweet
   Value                                   Tweets       Timeline



 user_name   Primary Key      Field      Primary Key   Primary Key



                                         Primary Key   Primary Key
  tweet_id                 Primary Key
                                         Component     Component
UserMetrics Table
CREATE TABLE UserMetrics
(
    user_name   text,
    tweets      counter,
    followers   counter,
    following   counter,
    PRIMARY KEY (user_name)
);
UserMetrics Table...
cqlsh:cass_college> UPDATE
                ...      UserMetrics
                ... SET
                ...      tweets = tweets + 1
                ... WHERE
                ...      user_name = 'fred';
cqlsh:cass_college> select * from UserMetrics where user_name
= 'fred';
 user_name | followers | following | tweets
-----------+-----------+-----------+--------
      fred |      null |       null |      1
Data Model (so far)
   CF /                             User         User
              User     Tweet                               User Metrics
   Value                           Tweets      Timeline


             Primary              Primary      Primary       Primary
 user_name              Field
               Key                  Key          Key           Key


                       Primary   Primary Key Primary Key
 tweet_id
                         Key     Component Component
Relationships
CREATE TABLE Followers
(
    user_name       text,
    follower        text,
    timestamp       timestamp,
    PRIMARY KEY (user_name, follower)
);

CREATE TABLE Following
(
    user_name       text,
    following       text,
    timestamp       timestamp,
    PRIMARY KEY (user_name, following)
);
Relationships
INSERT INTO
    Following
    (user_name, following, timestamp)
VALUES
    ('bob', 'fred', 1352247749161);
INSERT INTO
    Followers
    (user_name, follower, timestamp)
VALUES
    ('fred', 'bob', 1352247749161);
Relationships
cqlsh:cass_college> select * from Following;
 user_name | following | timestamp
-----------+-----------+--------------------------
       bob |      fred | 2012-11-07 13:22:29+1300

cqlsh:cass_college> select * from Followers;
 user_name | follower | timestamp
-----------+----------+--------------------------
      fred |      bob | 2012-11-07 13:22:29+1300
Data Model
  CF /                             User        User        User      Follows
             User     Tweet
  Value                           Tweets     Timeline     Metrics   Followers


                                                                    Primary
            Primary              Primary     Primary      Primary
user_name              Field                                          Key
              Key                  Key         Key          Key
                                                                     Field


                      Primary   Primary Key Primary Key
 tweet_id
                        Key     Component Component
Thanks.
Aaron Morton
                     @aaronmorton
                   www.thelastpickle.com




Licensed under a Creative Commons Attribution-NonCommercial 3.0 New Zealand License

More Related Content

What's hot (20)

PPT
Scaling web applications with cassandra presentation
Murat Çakal
 
PPTX
C*ollege Credit: Creating Your First App in Java with Cassandra
DataStax
 
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
PDF
Programming with Python and PostgreSQL
Peter Eisentraut
 
PDF
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Henning Jacobs
 
PDF
Replication MongoDB Days 2013
Randall Hunt
 
PPTX
MongoDB London 2013: Basic Replication in MongoDB presented by Marc Schwering...
MongoDB
 
PDF
Linux system admin
Mohammed Zainul Abiddin
 
PPTX
Introduction to PostgreSQL
Joel Brewer
 
PDF
Non-Relational Postgres / Bruce Momjian (EnterpriseDB)
Ontico
 
PDF
MongoDB Database Replication
Mehdi Valikhani
 
PDF
Postgres can do THAT?
alexbrasetvik
 
PDF
glance replicator
irix_jp
 
PPTX
Replication and Replica Sets
MongoDB
 
PPTX
Webinar: Replication and Replica Sets
MongoDB
 
PDF
2013 london advanced-replication
Marc Schwering
 
PPTX
Replication and Replica Sets
MongoDB
 
TXT
Oracle ORA Errors
Manish Mudhliyar
 
PDF
Redis SoCraTes 2014
steffenbauer
 
PPTX
Replication and replica sets
Randall Hunt
 
Scaling web applications with cassandra presentation
Murat Çakal
 
C*ollege Credit: Creating Your First App in Java with Cassandra
DataStax
 
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
Programming with Python and PostgreSQL
Peter Eisentraut
 
"PostgreSQL and Python" Lightning Talk @EuroPython2014
Henning Jacobs
 
Replication MongoDB Days 2013
Randall Hunt
 
MongoDB London 2013: Basic Replication in MongoDB presented by Marc Schwering...
MongoDB
 
Linux system admin
Mohammed Zainul Abiddin
 
Introduction to PostgreSQL
Joel Brewer
 
Non-Relational Postgres / Bruce Momjian (EnterpriseDB)
Ontico
 
MongoDB Database Replication
Mehdi Valikhani
 
Postgres can do THAT?
alexbrasetvik
 
glance replicator
irix_jp
 
Replication and Replica Sets
MongoDB
 
Webinar: Replication and Replica Sets
MongoDB
 
2013 london advanced-replication
Marc Schwering
 
Replication and Replica Sets
MongoDB
 
Oracle ORA Errors
Manish Mudhliyar
 
Redis SoCraTes 2014
steffenbauer
 
Replication and replica sets
Randall Hunt
 

Viewers also liked (16)

PPTX
Webinar - Security and Manageability: Key Criteria in Selecting Enterprise-Gr...
DataStax
 
PPTX
Introducing DataStax Enterprise 4.7
DataStax
 
PDF
What is DataStax Enterprise?
DataStax
 
KEY
C*ollege Credit: An Introduction to Apache Cassandra
DataStax
 
PPT
Community Webinar: 15 Commandments of Cassandra DBAs
DataStax
 
PDF
Cassandra Community Webinar | The World's Next Top Data Model
DataStax
 
PDF
Cassandra Community Webinar | Data Model on Fire
DataStax
 
PDF
Understanding Data Consistency in Apache Cassandra
DataStax
 
PPTX
Using Spark to Load Oracle Data into Cassandra (Jim Hatcher, IHS Markit) | C*...
DataStax
 
PDF
Data Modeling a Scheduling App (Adam Hutson, DataScale) | Cassandra Summit 2016
DataStax
 
PDF
How Do I Cassandra?
Rick Branson
 
PPTX
What We Learned About Cassandra While Building go90 (Christopher Webster & Th...
DataStax
 
PDF
Understanding Data Partitioning and Replication in Apache Cassandra
DataStax
 
PDF
What is in All of Those SSTable Files Not Just the Data One but All the Rest ...
DataStax
 
PPTX
An Overview of Apache Cassandra
DataStax
 
PPTX
Webinar - DataStax Enterprise 5.1: 3X the operational analytics speed, help f...
DataStax
 
Webinar - Security and Manageability: Key Criteria in Selecting Enterprise-Gr...
DataStax
 
Introducing DataStax Enterprise 4.7
DataStax
 
What is DataStax Enterprise?
DataStax
 
C*ollege Credit: An Introduction to Apache Cassandra
DataStax
 
Community Webinar: 15 Commandments of Cassandra DBAs
DataStax
 
Cassandra Community Webinar | The World's Next Top Data Model
DataStax
 
Cassandra Community Webinar | Data Model on Fire
DataStax
 
Understanding Data Consistency in Apache Cassandra
DataStax
 
Using Spark to Load Oracle Data into Cassandra (Jim Hatcher, IHS Markit) | C*...
DataStax
 
Data Modeling a Scheduling App (Adam Hutson, DataScale) | Cassandra Summit 2016
DataStax
 
How Do I Cassandra?
Rick Branson
 
What We Learned About Cassandra While Building go90 (Christopher Webster & Th...
DataStax
 
Understanding Data Partitioning and Replication in Apache Cassandra
DataStax
 
What is in All of Those SSTable Files Not Just the Data One but All the Rest ...
DataStax
 
An Overview of Apache Cassandra
DataStax
 
Webinar - DataStax Enterprise 5.1: 3X the operational analytics speed, help f...
DataStax
 
Ad

Similar to C*ollege Credit: Data Modeling for Apache Cassandra (20)

PDF
Cassandra By Example: Data Modelling with CQL3
Eric Evans
 
PDF
Cassandra by Example: Data Modelling with CQL3
Eric Evans
 
PDF
Apache Cassandra Lesson: Data Modelling and CQL3
Markus Klems
 
PDF
NoSQL Overview
adesso AG
 
PDF
CQL3 in depth
Yuki Morishita
 
PPTX
Apache Cassandra Developer Training Slide Deck
DataStax Academy
 
PDF
CQL3 and Data Modeling 101 with Apache Cassandra
Chris McEniry
 
PDF
Cassandra Community Webinar - Introduction To Apache Cassandra 1.2
aaronmorton
 
PPTX
Understanding How CQL3 Maps to Cassandra's Internal Data Structure
DataStax
 
KEY
Building a Highly Scalable, Open Source Twitter Clone
Paul Brown
 
PDF
Cassandra 2012
beobal
 
PDF
Slide presentation pycassa_upload
Rajini Ramesh
 
PPTX
Just in time (series) - KairosDB
Victor Anjos
 
KEY
Cassandra presentation - Geek Nights Braga
Pedro Gomes
 
PDF
Cassandra Summit 2014: Understanding CQL3 Inside and Out
DataStax Academy
 
PDF
CQL In Cassandra 1.0 (and beyond)
Eric Evans
 
PPTX
Cassandra
Bang Tsui Liou
 
PDF
Introduction to Data Modeling with Apache Cassandra
Luke Tillman
 
PDF
Introduction to data modeling with apache cassandra
Patrick McFadin
 
PDF
Cassandra Day Atlanta 2015: Data Modeling 101
DataStax Academy
 
Cassandra By Example: Data Modelling with CQL3
Eric Evans
 
Cassandra by Example: Data Modelling with CQL3
Eric Evans
 
Apache Cassandra Lesson: Data Modelling and CQL3
Markus Klems
 
NoSQL Overview
adesso AG
 
CQL3 in depth
Yuki Morishita
 
Apache Cassandra Developer Training Slide Deck
DataStax Academy
 
CQL3 and Data Modeling 101 with Apache Cassandra
Chris McEniry
 
Cassandra Community Webinar - Introduction To Apache Cassandra 1.2
aaronmorton
 
Understanding How CQL3 Maps to Cassandra's Internal Data Structure
DataStax
 
Building a Highly Scalable, Open Source Twitter Clone
Paul Brown
 
Cassandra 2012
beobal
 
Slide presentation pycassa_upload
Rajini Ramesh
 
Just in time (series) - KairosDB
Victor Anjos
 
Cassandra presentation - Geek Nights Braga
Pedro Gomes
 
Cassandra Summit 2014: Understanding CQL3 Inside and Out
DataStax Academy
 
CQL In Cassandra 1.0 (and beyond)
Eric Evans
 
Cassandra
Bang Tsui Liou
 
Introduction to Data Modeling with Apache Cassandra
Luke Tillman
 
Introduction to data modeling with apache cassandra
Patrick McFadin
 
Cassandra Day Atlanta 2015: Data Modeling 101
DataStax Academy
 
Ad

More from DataStax (20)

PPTX
Is Your Enterprise Ready to Shine This Holiday Season?
DataStax
 
PPTX
Designing Fault-Tolerant Applications with DataStax Enterprise and Apache Cas...
DataStax
 
PPTX
Running DataStax Enterprise in VMware Cloud and Hybrid Environments
DataStax
 
PPTX
Best Practices for Getting to Production with DataStax Enterprise Graph
DataStax
 
PPTX
Webinar | Data Management for Hybrid and Multi-Cloud: A Four-Step Journey
DataStax
 
PPTX
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
DataStax
 
PDF
Webinar | Better Together: Apache Cassandra and Apache Kafka
DataStax
 
PDF
Top 10 Best Practices for Apache Cassandra and DataStax Enterprise
DataStax
 
PDF
Introduction to Apache Cassandra™ + What’s New in 4.0
DataStax
 
PPTX
Webinar: How Active Everywhere Database Architecture Accelerates Hybrid Cloud...
DataStax
 
PPTX
Webinar | Aligning GDPR Requirements with Today's Hybrid Cloud Realities
DataStax
 
PDF
Designing a Distributed Cloud Database for Dummies
DataStax
 
PDF
How to Power Innovation with Geo-Distributed Data Management in Hybrid Cloud
DataStax
 
PDF
How to Evaluate Cloud Databases for eCommerce
DataStax
 
PPTX
Webinar: DataStax Enterprise 6: 10 Ways to Multiply the Power of Apache Cassa...
DataStax
 
PPTX
Webinar: DataStax and Microsoft Azure: Empowering the Right-Now Enterprise wi...
DataStax
 
PPTX
Webinar - Real-Time Customer Experience for the Right-Now Enterprise featurin...
DataStax
 
PPTX
Datastax - The Architect's guide to customer experience (CX)
DataStax
 
PPTX
An Operational Data Layer is Critical for Transformative Banking Applications
DataStax
 
PPTX
Becoming a Customer-Centric Enterprise Via Real-Time Data and Design Thinking
DataStax
 
Is Your Enterprise Ready to Shine This Holiday Season?
DataStax
 
Designing Fault-Tolerant Applications with DataStax Enterprise and Apache Cas...
DataStax
 
Running DataStax Enterprise in VMware Cloud and Hybrid Environments
DataStax
 
Best Practices for Getting to Production with DataStax Enterprise Graph
DataStax
 
Webinar | Data Management for Hybrid and Multi-Cloud: A Four-Step Journey
DataStax
 
Webinar | How to Understand Apache Cassandra™ Performance Through Read/Writ...
DataStax
 
Webinar | Better Together: Apache Cassandra and Apache Kafka
DataStax
 
Top 10 Best Practices for Apache Cassandra and DataStax Enterprise
DataStax
 
Introduction to Apache Cassandra™ + What’s New in 4.0
DataStax
 
Webinar: How Active Everywhere Database Architecture Accelerates Hybrid Cloud...
DataStax
 
Webinar | Aligning GDPR Requirements with Today's Hybrid Cloud Realities
DataStax
 
Designing a Distributed Cloud Database for Dummies
DataStax
 
How to Power Innovation with Geo-Distributed Data Management in Hybrid Cloud
DataStax
 
How to Evaluate Cloud Databases for eCommerce
DataStax
 
Webinar: DataStax Enterprise 6: 10 Ways to Multiply the Power of Apache Cassa...
DataStax
 
Webinar: DataStax and Microsoft Azure: Empowering the Right-Now Enterprise wi...
DataStax
 
Webinar - Real-Time Customer Experience for the Right-Now Enterprise featurin...
DataStax
 
Datastax - The Architect's guide to customer experience (CX)
DataStax
 
An Operational Data Layer is Critical for Transformative Banking Applications
DataStax
 
Becoming a Customer-Centric Enterprise Via Real-Time Data and Design Thinking
DataStax
 

C*ollege Credit: Data Modeling for Apache Cassandra

  • 1. DATASTAX C*OLLEGE CREDIT: DATA MODELLING FOR APACHE CASSANDRA Aaron Morton Apache Cassandra Committer, Data Stax MVP for Apache Cassandra @aaronmorton www.thelastpickle.com Licensed under a Creative Commons Attribution-NonCommercial 3.0 New Zealand License
  • 2. General Guidelines API Choice Example
  • 3. Cassandra is good at reading data from a row in the order it is stored.
  • 4. Typically an efficient data model will denormalize data and use the storage engine order.
  • 5. To create a good data model understand the queries your application requires.
  • 6. General Guidelines API Choice Example
  • 7. Multiple API’s? initially only a Thrift / RPC API, used by language specific clients.
  • 8. Multiple API’s... Cassandra Query Language (CQL) started as a higher level, declarative alternative.
  • 9. Multiple API’s... CQL 3 brings many changes. Currently in Beta in Cassandra v1.1
  • 10. CQL 3 uses a Table Orientated, Schema Driven, Data Model. (I said it had many changes.)
  • 11. General Guidelines API Choice Example
  • 12. Twitter Clone Previously done with Thrift at WDCNZ “Hello @World #Cassandra - Apache Cassandra in action” http://vimeo.com/49762233
  • 13. Twitter clone... using CQL 3 via the cqlsh tool. bin/cqlsh -3
  • 14. Queries? * Post Tweet to Followers * Get Tweet by ID * List Tweets by User * List Tweets in User Timeline * List Followers
  • 15. Keyspace is a namespace container.
  • 16. Our Keyspace CREATE KEYSPACE cass_college WITH strategy_class = 'NetworkTopologyStrategy' AND strategy_options:datacenter1 = 1;
  • 17. Table is a sparse collection of well known, ordered columns.
  • 18. First Table CREATE TABLE User ( user_name text, password text, real_name text, PRIMARY KEY (user_name) );
  • 19. Some users... cqlsh:cass_college> INSERT INTO User ... (user_name, password, real_name) ... VALUES ... ('fred', 'sekr8t', 'Mr Foo'); cqlsh:cass_college> select * from User; user_name | password | real_name -----------+----------+----------- fred | sekr8t | Mr Foo
  • 20. Some users... cqlsh:cass_college> INSERT INTO User ... (user_name, password) ... VALUES ... ('bob', 'pwd'); cqlsh:cass_college> select * from User where user_name = 'bob'; user_name | password | real_name -----------+----------+----------- bob | pwd | null
  • 21. Data Model (so far) User
  • 22. Data Model (so far) CF / User Value user_name Primary Key
  • 23. Tweet Table CREATE TABLE Tweet ( tweet_id bigint, body text, user_name text, timestamp timestamp, PRIMARY KEY (tweet_id) );
  • 24. Tweet Table... cqlsh:cass_college> INSERT INTO Tweet ... (tweet_id, body, user_name, timestamp) ... VALUES ... (1, 'The Tweet','fred',1352150816917); cqlsh:cass_college> select * from Tweet where tweet_id = 1; tweet_id | body | timestamp | user_name ----------+-----------+--------------------------+----------- 1 | The Tweet | 2012-11-06 10:26:56+1300 | fred
  • 25. Data Model (so far) CF / User Tweet Value user_name Primary Key Field tweet_id Primary Key
  • 26. UserTweets Table CREATE TABLE UserTweets ( tweet_id bigint, user_name text, body text, timestamp timestamp, PRIMARY KEY (user_name, tweet_id) );
  • 27. UserTweets Table... cqlsh:cass_college> INSERT INTO UserTweets ... (tweet_id, body, user_name, timestamp) ... VALUES ... (1, 'The Tweet','fred',1352150816917); cqlsh:cass_college> select * from UserTweets where user_name='fred'; user_name | tweet_id | body | timestamp -----------+----------+-----------+-------------------------- fred | 1 | The Tweet | 2012-11-06 10:26:56+1300
  • 28. UserTweets Table... cqlsh:cass_college> select * from UserTweets where user_name='fred' and tweet_id=1; user_name | tweet_id | body | timestamp -----------+----------+-----------+-------------------------- fred | 1 | The Tweet | 2012-11-06 10:26:56+1300
  • 29. UserTweets Table... cqlsh:cass_college> INSERT INTO UserTweets ... (tweet_id, body, user_name, timestamp) ... VALUES ... (2, 'Second Tweet', 'fred', 1352150816918); cqlsh:cass_college> select * from UserTweets where user_name = 'fred'; user_name | tweet_id | body | timestamp -----------+----------+--------------+-------------------------- fred | 1 | The Tweet | 2012-11-06 10:26:56+1300 fred | 2 | Second Tweet | 2012-11-06 10:26:56+1300
  • 30. UserTweets Table... cqlsh:cass_college> select * from UserTweets where user_name = 'fred' order by tweet_id desc; user_name | tweet_id | body | timestamp -----------+----------+--------------+-------------------------- fred | 2 | Second Tweet | 2012-11-06 10:26:56+1300 fred | 1 | The Tweet | 2012-11-06 10:26:56+1300
  • 31. UserTimeline CREATE TABLE UserTimeline ( tweet_id bigint, user_name text, body text, timestamp timestamp, PRIMARY KEY (user_name, tweet_id) );
  • 32. Data Model (so far) CF / User User User Tweet Value Tweets Timeline user_name Primary Key Field Primary Key Primary Key Primary Key Primary Key tweet_id Primary Key Component Component
  • 33. UserMetrics Table CREATE TABLE UserMetrics ( user_name text, tweets counter, followers counter, following counter, PRIMARY KEY (user_name) );
  • 34. UserMetrics Table... cqlsh:cass_college> UPDATE ... UserMetrics ... SET ... tweets = tweets + 1 ... WHERE ... user_name = 'fred'; cqlsh:cass_college> select * from UserMetrics where user_name = 'fred'; user_name | followers | following | tweets -----------+-----------+-----------+-------- fred | null | null | 1
  • 35. Data Model (so far) CF / User User User Tweet User Metrics Value Tweets Timeline Primary Primary Primary Primary user_name Field Key Key Key Key Primary Primary Key Primary Key tweet_id Key Component Component
  • 36. Relationships CREATE TABLE Followers ( user_name text, follower text, timestamp timestamp, PRIMARY KEY (user_name, follower) ); CREATE TABLE Following ( user_name text, following text, timestamp timestamp, PRIMARY KEY (user_name, following) );
  • 37. Relationships INSERT INTO Following (user_name, following, timestamp) VALUES ('bob', 'fred', 1352247749161); INSERT INTO Followers (user_name, follower, timestamp) VALUES ('fred', 'bob', 1352247749161);
  • 38. Relationships cqlsh:cass_college> select * from Following; user_name | following | timestamp -----------+-----------+-------------------------- bob | fred | 2012-11-07 13:22:29+1300 cqlsh:cass_college> select * from Followers; user_name | follower | timestamp -----------+----------+-------------------------- fred | bob | 2012-11-07 13:22:29+1300
  • 39. Data Model CF / User User User Follows User Tweet Value Tweets Timeline Metrics Followers Primary Primary Primary Primary Primary user_name Field Key Key Key Key Key Field Primary Primary Key Primary Key tweet_id Key Component Component
  • 41. Aaron Morton @aaronmorton www.thelastpickle.com Licensed under a Creative Commons Attribution-NonCommercial 3.0 New Zealand License