How to structure friend relation in cassandra

809 views Asked by At

I have a user table in Cassandra currently.

I want To create a friend relation table but i am not sure what is the best way to do it. First i thought put like key as current user and column as friend but than i wanted to achieve the functionality of sending friend request so i think there should be another object called accepted which takes care of that two persons are real friends or not or eather the other person accepted the friend request or not. I think it will be Many to Many table. For example in a friendship relation.

Friendship: {
John Dow: {
10: Mark Seldon,
8: Julian Hendrix,
...
},
Mark Seldon: {
9: John Dow,
...
},
...
}

Any directions is appreciated.

1

There are 1 answers

4
emgsilva On BEST ANSWER

That seems to be possible by creating a new table "friends", where you could (for example) use a "composite key" {userid, friendid}, in CQL:

CREATE TABLE friends(    
userid varchar,
friendid varchar,
PRIMARY KEY (userid, friendid)
);

which would define a "friendship" relation between two users (both with an entry in the Users table)... This would allow you to query several things:

  • get all friend of "userid"
  • check if userid and "user x" (with a given friendid) exist

This would also mean that whenever you store a relationship, you need to create two "entries" in the table: {user1, user2} and {user2, user1}...