本文介绍了Cassandra:基于集合中的一个特定值进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Cassandra中有一个数据表,其中一列是:

I have a data table in Cassandra and one of the columns is:

customer_favourites ,每个值均已设置类型,并且包含每个客户喜欢的食物的详细信息.例如,一个客户可能有 {'Mexican','Italian','Indian'} ,而另一位客户可能有 {'Mexican','French'} {'Mexican'} .

customer_favourites, with each value being of type set and it has the details of each customer's favourite foods. For example one customer could have {'Mexican', 'Italian', 'Indian'} and another customer could have {'Mexican', 'French'} and another could have {'Mexican'}.

我有以下代码:

从客户中选择customer_id,customer_fname,customer_lname,其中customer_favourites包含墨西哥"允许过滤;

我希望它过滤那些只喜欢墨西哥食物的顾客,但是现在它返回每个将墨西哥作为他们最喜欢的食物之一的顾客的详细信息.如何过滤查询以返回仅喜欢墨西哥食物的客户?

I want it to filter on those customers whose favourite food is ONLY Mexican, but right now it's returning the details of every customer who has Mexican as one of their favourite foods. How do I filter my query to return customer who like ONLY Mexican food?

推荐答案

幼稚的方法:您需要使用 customer_favourites = {'Mexican'} ...

Naive approach: You need to use customer_favourites = {'Mexican'}...

更好的方法-在相应的字段上创建二级索引,使用 Full 关键字,然后使用 customer_favourites = {'Mexican'} .

Better approach - create secondary index on the corresponding field, using the FULL keyword, and then use customer_favourites = {'Mexican'}.

最佳方法-使用 customer_favourites 作为分区键创建一个单独的表,并在其中搜索用户(列应为 frozen ).这种方法的问题之一是数据偏斜,因为喜欢的食物数量相对较少并且非常不平衡.

Best approach - create a separate table with customer_favourites as partition key, and search users in it (column should be frozen). One of the problems with this approach would be the data skew, as number of favorite foods is relatively small, and quite imbalanced.

替代方法-如果您需要非常频繁地使用非分区键进行搜索,请重新考虑使用Cassandra.

Alternative approach - reconsider the use of the Cassandra, if you need to search by non-partition key very often.

这篇关于Cassandra:基于集合中的一个特定值进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:10