Building a Cassandra based application from scratch        Patrick McFadin    Cassandra Summit 2012         #cassandra12
This is me• Chief Architect at Hobsons  – Hobsons is an education services company. More    here: www.hobsons.com• Cassand...
Goals•   Take a new concept•   What’s the data model?!?!•   Some sample code•   You get homework! (If you want)
Here’s the plan•   Conceptualize a new application•   Identify the entity tables•   Identify query tables•   Code. Rinse. ...
www.killrvideos.com                                                    Video Tit le                        User name      ...
Break down the features•   Post a video*•   View a video•   Add a comment•   Rate a video•   Tag a video     * Not talking...
Create Entity Tables  Basic storage unit
Users                       password        FirstName        LastName      Username•   Similar to a RDBMS table. Fairly fi...
Users: The set codestatic void setUser(User user, Keyspace keyspace) {    // Create a mutator that allows you to talk to c...
Videos                                   UserName      Description   Tags    VideoId       VideoName    <UUID>•   Use a UU...
Videos: The get codestatic Video getVideoByUUID(UUID videoId, Keyspace keyspace){    Video video = new Video();    //Creat...
Comments     VideoId      Username:<timestamp>           ..        Username:<timestamp>     <UUID>                    Time...
Rating a video                             rating_count         rating_total             VideoId             <UUID>       ...
Video Event                       start_<timestamp>   stop_<timestamp>    start_<timestamp>    VideoId:Username           ...
Create Query TablesIndexes to support fast lookups
Lookup Video By Username                      VideoId:<timestamp>       ..           VideoId:<timestamp>       Username•  ...
Videos by Tag                         VideoId                ..              VideoId         tag•   Tag is unique regardle...
Deployment strategies• Measure your risk  – Replication factor?  – Multi-datacenter?  – Cost?• Performance  – Today != tom...
Wrap up• Similar data model process to RDBMS… to  start• Query -> Index table• Don’t be afraid to write in multiple tables...
Go play!•   Go to: http://github.com/pmcfadin•   Look for projects with cassandra12•   Clone or fork my examples•   Implem...
Thank You!Connect with me at @PatrickMcFadin            Or linkedIn   Conference tag #cassandra12
Upcoming SlideShare
Loading in...5
×

Cassandra Summit 2012 - Building a Cassandra Based App From Scratch

6,806

Published on

Talk I did at Cassandra Summit 2012 on building an application based on the Cassandra data source. Meant to be an introduction into how to start the development process.

0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,806
On Slideshare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
233
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide
  • I’ll be using the term Tables instead of Column Family during this presentation.
  • Comp Columns. Two different types
  • Example shows use of a counter
  • Cassandra Summit 2012 - Building a Cassandra Based App From Scratch

    1. 1. Building a Cassandra based application from scratch Patrick McFadin Cassandra Summit 2012 #cassandra12
    2. 2. This is me• Chief Architect at Hobsons – Hobsons is an education services company. More here: www.hobsons.com• Cassandra user since .7• Follow me here: @PatrickMcFadin
    3. 3. Goals• Take a new concept• What’s the data model?!?!• Some sample code• You get homework! (If you want)
    4. 4. Here’s the plan• Conceptualize a new application• Identify the entity tables• Identify query tables• Code. Rinse. Repeat.• Deploy• …• Profit! * I’ll be using the term Tables which is equivalent to Column Families
    5. 5. www.killrvideos.com Video Tit le User name Recommended D ipt ion escrStart with a Meow Ads concept by Google Video Sharing Website Rat ing: Tags: Foo Bar Upload New! Comment s*Cat drawing by goodrob13 on Flickr
    6. 6. Break down the features• Post a video*• View a video• Add a comment• Rate a video• Tag a video * Not talking about transcoding! Check out zencoder.com, it’s pretty sweet.
    7. 7. Create Entity Tables Basic storage unit
    8. 8. Users password FirstName LastName Username• Similar to a RDBMS table. Fairly fixed columns• Username is unique• Use secondary indexes on firstname and lastname for lookup• Adding columns with Cassandra is super easy CREATE TABLE users ( username varchar PRIMARY KEY, firstname varchar, lastname varchar, password varchar );
    9. 9. Users: The set codestatic void setUser(User user, Keyspace keyspace) { // Create a mutator that allows you to talk to casssandra Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); try { // Use the mutator to insert data into our table mutator.addInsertion(user.getUsername(), "users", HFactory.createStringColumn("firstname", user.getFirstname())); mutator.addInsertion(user.getUsername(), "users”, HFactory.createStringColumn("lastname", user.getLastname())); mutator.addInsertion(user.getUsername(), "users", HFactory.createStringColumn("password", user.getPassword())); // Once the mutator is ready, execute on cassandra mutator.execute(); } catch (HectorException he) { he.printStackTrace(); }} You can implement the get…
    10. 10. Videos UserName Description Tags VideoId VideoName <UUID>• Use a UUID as a row key for uniqueness• Allows for same video names• Tags should be stored in some sort of delimited format• Index on username may not be the best plan CREATE TABLE videos ( videoid uuid PRIMARY KEY, videoname varchar, username varchar, description varchar, tags varchar );
    11. 11. Videos: The get codestatic Video getVideoByUUID(UUID videoId, Keyspace keyspace){ Video video = new Video(); //Create a slice query. Well be getting specific column names SliceQuery<UUID, String, String> sliceQuery = HFactory.createSliceQuery(keyspace, uuidSerializer, stringSerializer, stringSerializer); sliceQuery.setColumnFamily("videos"); sliceQuery.setKey(videoId); sliceQuery.setColumnNames("videoname","username","description","tags"); // Execute the query and get the list of columns ColumnSlice<String,String> result = sliceQuery.execute().get(); // Get each column by name and add them to our video object video.setVideoName(result.getColumnByName("videoname").getValue()); video.setUsername(result.getColumnByName("username").getValue()); video.setDescription(result.getColumnByName("description").getValue()); video.setTags(result.getColumnByName("tags").getValue().split(",")); return video;} You can implement the set…
    12. 12. Comments VideoId Username:<timestamp> .. Username:<timestamp> <UUID> Time Order• Videos have many comments• Use Composite Columns to store user and time• Value of each column is the text of the comment• Order is as inserted• Use getSlice() to pull some or all of the comments CREATE TABLE comments ( videoid uuid PRIMARY KEY comment varchar );
    13. 13. Rating a video rating_count rating_total VideoId <UUID> <counter> <counter>• Use counter for single call update• rating_count is how many ratings were given• rating_total is the sum of rating• Ex: rating_count = 5, rating_total = 23, avg rating = 23/5 = 4.6 CREATE TABLE video_rating ( videoid uuid PRIMARY KEY, rating_counter counter, rating_total counter);* * Only valid in CQL 3+
    14. 14. Video Event start_<timestamp> stop_<timestamp> start_<timestamp> VideoId:Username video_<timestamp> Time Order• Track viewing events• Combine Video ID and Username for a unique row• Stop time can be used to pick up where they left off• Great for usage analytics later CREATE TABLE video_event ( videoid_username varchar PRIMARY KEY, event varchar );
    15. 15. Create Query TablesIndexes to support fast lookups
    16. 16. Lookup Video By Username VideoId:<timestamp> .. VideoId:<timestamp> Username• Username is unique• One column for each new video uploaded• Column slice for time span. From x to y• VideoId is added the same time a Video record is added CREATE TABLE username_video_index ( username varchar PRIMARY KEY, videoid_timestamp varchar );
    17. 17. Videos by Tag VideoId .. VideoId tag• Tag is unique regardless of video• Great for “List videos with X tag”• Tags have to be updated in Video and Tag at the same time• Index integrity is maintained in app logic CREATE TABLE tag_index ( tag varchar PRIMARY KEY, videoid varchar );
    18. 18. Deployment strategies• Measure your risk – Replication factor? – Multi-datacenter? – Cost?• Performance – Today != tomorrow. Scale when needed – Have a expansion plan ready
    19. 19. Wrap up• Similar data model process to RDBMS… to start• Query -> Index table• Don’t be afraid to write in multiple tables at once• Bonus points: Hadoop and Solr!
    20. 20. Go play!• Go to: http://github.com/pmcfadin• Look for projects with cassandra12• Clone or fork my examples• Implement stubbed methods• Send me your solutions: pmcfadin@gmail.com• Follow me for updates: @PatrickMcFadin
    21. 21. Thank You!Connect with me at @PatrickMcFadin Or linkedIn Conference tag #cassandra12
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×