cassandra:您可以查询集合字段吗?
假设您想在这样的字段中保留 friend 列表,您可以按照以下方式运行查询:where user_id = xxx andfriend = 'bob'?
如果集合不适合此目的,那么在 cassandra 中跟踪 friend 的正确方法是什么?
最佳答案
尚不支持二级索引,但开发正在进行中 ( CASSANDRA-4511 )
在您的模型中,如果您知道 user_id,您可以获取用户并检查“bob”是否在应用程序端的好友列表中。如果您需要查询 person_A 是否是 person_B 的好友,您可以将集合提取到其自己的表中,但此模型需要执行 2 次查询。
CREATE TABLE friends (
user_id text,
friend text,
PRIMARY KEY(user_id, friend)
);
CREATE INDEX idx_friends on friends (friend);
假设您从上表中得到的结果如下:
user_id | friend
---------+--------
daniel | bob
daniel | jack
jack | bob
如果你想找到所有关注 bob 的人,你可以使用SELECT * FROM Friends WHEREfriend='bob'
。实际上,您也可以在没有辅助索引和使用ALLOW FILTERING
的情况下执行此操作,但这可能会导致不可预测的性能:
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.
关于cassandra:您可以查询集合字段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20063352/