Reputation: 1053
cassandra: can you query against a collection field?
say if you wanted to keep a friends list in such a field, can you run a query along the lines of: where user_id = xxx and friend = 'bob'?
If a collection is not right for this, What is the proper way to keep track of friends in cassandra?
Upvotes: 3
Views: 781
Reputation: 14163
Secondary indexes are still not yet supported but development is in progress (CASSANDRA-4511)
In your model, if you know the user_id you could fetch the user and check if 'bob' is in their friends list at the application side. If you need to query on whether person_A is friends with person_B you can extract the collection to its own table but this model will require doing 2 queries.
CREATE TABLE friends (
user_id text,
friend text,
PRIMARY KEY(user_id, friend)
);
CREATE INDEX idx_friends on friends (friend);
Say you have a result from the above table like:
user_id | friend
---------+--------
daniel | bob
daniel | jack
jack | bob
If you want to find all the people following bob, you can use SELECT * FROM friends WHERE friend='bob'
. You could actually do it too without having the secondary index and using ALLOW FILTERING
but this can lead to unpredictable performance:
The ALLOW FILTERING option allows to explicitly allow (some) queries that require filtering. Please note that a query using ALLOW FILTERING may thus have unpredictable performance (for the definition above), i.e. even a query that selects a handful of records may exhibit performance that depends on the total amount of data stored in the cluster.
Upvotes: 2