【问题标题】:In Cassandra CQL, is there a way to query the size of a collection column type?在 Cassandra CQL 中,有没有办法查询集合列类型的大小?
【发布时间】:2019-03-21 00:23:02
【问题描述】:

我们希望能够做类似的事情..

select * from User where rewards.size > 0

当架构是

create table user (
    id uuid primary key,
    network_id uuid,
    rewards set<uuid>,
    name text   
);

【问题讨论】:

  • 试试这个。 select * from User where count(rewards) > '0' ;
  • 这似乎是行数,仅在 select 中有效,在 where 中无效。

标签: cassandra cql


【解决方案1】:

可悲的是,Cassandra 渴望执行非常快速的读取,而另一方面,它并没有完美地支持集合: Get count of elements in Set type column in Cassandra

不幸的是,即使在 CQL 驱动程序 v2 中也不支持集合 完美:您可以在 upsert 语句中添加或删除项目。但更多关于 他们,比如做一个项目选择,要求收集项目的 TTL 或 不支持询问集合的大小。所以你必须

resultset: SELECT collection_column FROM ... 然后拿项目 resultset.one()resultset.all() 并自己获取 item.size()

您想要做的是添加一个带有计数器的索引列来计数和快速读取元素计数。如果您只想知道集合是否不为空,则可能需要一个带有布尔值的索引列。索引确保您可以有效地扫描列。

【讨论】:

    【解决方案2】:

    有一个 size() 方法可以做到这一点.. 举例说明它是如何使用的:

     select order_id,items.item_id,last_modified from person.ecommerce_order   where size(items.item_id)> 1
    

    解释查询:

    items.item_id 是一个列表字段 .检查列表中是否有多个项目。

    【讨论】:

    • @Alex Ott,你能再解释一下吗?
    • 因为你的WHERE条件没有分区键,所以会发送到集群中的所有节点,需要对数据进行全扫描...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2015-03-18
    • 2019-05-18
    • 2020-10-14
    相关资源
    最近更新 更多