Book Image

Learning Apache Cassandra

By : Matthew Brown
4 (1)
Book Image

Learning Apache Cassandra

4 (1)
By: Matthew Brown

Overview of this book

Table of Contents (19 chapters)
Learning Apache Cassandra
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Modeling follow relationships


A data model for follow relationships should be able to answer two questions on behalf of a user:

  • Who do I follow?

  • Who follows me?

In Chapter 3, Organizing Related Data, you learned to design our table structures so that all important data access can be accomplished by querying a single partition. For this reason, we're better off considering the above questions separately, and designing the right table schema for each.

Outbound follows

We'll start with the question, "Who do I follow?" We'll want a partition per user, with each partition containing all the other users they follow:

CREATE TABLE "user_outbound_follows" (
  "follower_username" text,
  "followed_username" text,
  PRIMARY KEY ("follower_username", "followed_username")
);

Simple enough, but there's something unusual here: there are only two columns in the table, and they're both part of the primary key. As it turns out, this is a perfectly valid way to construct a table schema; non-key columns are optional...