cassandra: can you query against a collection field?

耗尽温柔 提交于 2019-12-10 21:12:25

问题


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?


回答1:


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.

Docs for ALLOW FILTERING.



来源:https://stackoverflow.com/questions/20063352/cassandra-can-you-query-against-a-collection-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!