Milan
Milan

Reputation: 1910

Apache Cassandra - Follow/Unfollow relationship on Twissandra example

I'm trying to learn Cassandra modeling by looking at the Twissandra project.

It seems that when user unfollows one of his followers only friendship/follower relationship is removed from the tables, but tweets of the unfollowed user remain in the timeline of the user that has just unfollowed him.

Also, with very basic knowledge of Cassandra modeling that I have currently, it seems to me that it is practically impossible to remove tweets from the timeline. Here is the model from Twissandra:

  CREATE TABLE timeline (
    username text,
    time timeuuid,
    tweet_id uuid,
    PRIMARY KEY (username, time)
  ) WITH CLUSTERING ORDER BY (time DESC)

Since tweet_id is neither a partition key nor a clustering column it is impossible to query by it and delete the record.

Further, can someone please suggest a model where it would be possible to remove tweets of the unfollowed users from the timeline.

I've been busting my head around this problem for a day and it seems as this is not very easy thing to do in Cassandra. I'm coming from relational world, so maybe my point of view is wrong also.

Upvotes: 0

Views: 531

Answers (1)

doanduyhai
doanduyhai

Reputation: 8812

Since tweet_id neither a partition key nor a clustering column it is impossible to query by it and delete the record.

CREATE TABLE timeline (
    username text,
    tweet_id timeuuid,
    tweet_content text
    PRIMARY KEY (username, tweet_id)
  ) WITH CLUSTERING ORDER BY (tweet_id DESC)

The above data model should do the trick

Further, can someone please suggest a model where it would be possible to remove tweets of the unfollowed users from the timeline.

You'll have to denormalize and create another table

CREATE TABLE followed_users_tweets(
    username text,
    followed_user text,
    tweet_id timeuuid,
    tweet_content text
    PRIMARY KEY ((username,followed_user) tweet_id)
) WITH CLUSTERING ORDER BY (tweet_id DESC);

When you unfollow "John DOE" and your username is "Helen SUE":

DELETE FROM followed_users_tweets WHERE username='Helen SUE' AND followed_user='John DOE'

Upvotes: 0

Related Questions