Putting it all together

Now that we know what partition and clustering keys are, we can put them to use. Assume that I have to design a table to track user logins to an application. I could solve that with a table like this:

CREATE TABLE packt.logins_by_user ( 
  user_id text, 
  login_datetime timestamp, 
  origin_ip text, 
  PRIMARY KEY ((user_id), login_datetime) 
) WITH CLUSTERING ORDER BY (login_datetime DESC); 
Keyspace and table creation will be covered in the Using Cassandra section that comes later in the chapter.

Next, I'll write some data to test with:

INSERT INTO logins_by_user (user_id, login_datetime, origin_ip) VALUES ('aploetz','2017-06-01 12:36:01','192.168.0.101'); INSERT INTO logins_by_user (user_id, login_datetime, origin_ip) VALUES ...

Get Seven NoSQL Databases in a Week now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.