使用部分分区键从 Cassandra 中删除数据

Iva*_*nov 6 cassandra cassandra-3.0

假设我在 Cassandra 中有下表:

customer_bought_product (
    store_id uuid,
    product_id text,
    order_time timestamp,
    email text,
    first_name text,
    last_name text,
    PRIMARY KEY ((store_id, product_id), order_time, email)
Run Code Online (Sandbox Code Playgroud)

分区键是store_idorder_id,用于存储时间序列数据。

数据没有TTL,因为它应该可以随时访问。

在某些情况下,我们可能需要删除给定store_id. 这样做的最佳做法是什么?

到目前为止,我想到了以下解决方案:

  1. 编写一个程序,该程序将从表中选择所有数据并删除具有给定store_id. - 缺点是随着我们在表中插入更多数据,这将花费越来越多的时间。
  2. 将数据留在表中。- 这样做的唯一问题是我们将拥有无用的数据。
  3. 将带有可用分区键的表名存储在不同的表中,可以通过 查询store_id,从中获取键并为每个或这些键创建删除语句。- 我不喜欢这个概念,因为我必须维护记录。

有没有人遇到过这个问题?从 Cassandra(不包括TTL)清除未使用记录的最佳做法是什么?

dil*_*ngi 5

创建一个物化视图来存储属于相应store_ids的product_ids。这样您就可以在 MV 中查询给定的 store_id,然后从主表中删除相应的行。这样就可以避免使用额外的应用程序代码来维护两个不同的表。

create materialized view mv_customer_bought_product 
as select product_id, store_id, order_time, email 
from customer_bought_product 
where order_time is not null 
and email is not null 
and product_id is not null 
and store_id is not null 
primary key (store_id, product_id, order_time, email) ;
Run Code Online (Sandbox Code Playgroud)

  • @ArunJoyThekkiniyath 您还需要主表主键中的所有列都出现在物化视图中。节省存储空间也不例外:) (2认同)