cassandra:你可以查询集合字段吗? [英] cassandra: can you query against a collection field?

查看:539
本文介绍了cassandra:你可以查询集合字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cassandra:你可以查询集合字段吗?

cassandra: can you query against a collection field?

说如果您想在这样的字段中保留好友列表,您可以按以下行来运行查询:where user_id = xxx and friend ='bob'?

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'?

如果一个集合不适合这种情况,那么在cassandra中跟踪朋友的正确方法是什么?

If a collection is not right for this, What is the proper way to keep track of friends in cassandra?

推荐答案

二级索引仍然不受支持,但正在开发中( CASSANDRA-4511

Secondary indexes are still not yet supported but development is in progress (CASSANDRA-4511)

在您的模型中,如果您知道user_id,您可以提取用户,并检查bob朋友列表在应用程序端。如果您需要查询person_A是否是person_B的朋友,您可以将该集合提取到自己的表格中,但此模型将需要执行2个查询。

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

如果要查找bob后的所有人,可以使用 SELECT * FROM friends WHERE friend ='bob'。你实际上可以做到这一点,而没有二级索引和使用 ALLOW FILTERING ,但这可能导致不可预测的性能:

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:


ALLOW FILTERING选项允许显式地允许(一些)需要过滤的查询。请注意,使用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:你可以查询集合字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆