0

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.

Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212

1 Answers1

0

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}...

emgsilva
  • 3,047
  • 1
  • 17
  • 16
  • If both are primary key why its required to stored both ways like described in the last line of the comment. – Saurabh Kumar Aug 19 '13 at 12:07
  • That would be possible indeed... but you would need to make two queries to get all friends (I do not think you can use OR statements...) of a given "user", namely: `select * from friends where userid = 'user'`, and `select * from friends where friendid = 'user' allow filtering` (this second query can lead to "unpredictable performance" as reported by Cassandra)... so this is a design decision: "two writes" or "two queries". I my self would go for two writes (as I want performance on queries, the writes "I do not care so much" when I use Cassandra/NoSQL). Maybe I am wrong, still learning too :) – emgsilva Aug 19 '13 at 12:28
  • Could you also please guide me though how my Pojo class should look like to have a desired table like you described. I am directly creating tables from my pojo classes. – Saurabh Kumar Aug 19 '13 at 12:32
  • I do not have experience with Hector, so cannot help you on that... I use Kundera (https://github.com/impetus-opensource/Kundera), which is a very nice and easy to use client for Cassandra and other DB technologies... this is an example using "composite keys": https://github.com/impetus-opensource/Kundera/wiki/Using-Compound-keys-with-Kundera – emgsilva Aug 19 '13 at 12:39