首页 文章

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

提问于
浏览
1

假设我在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)

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

数据没有 TTL ,因为它应始终可访问 .

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

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

  • 编写一个程序,它将从表中选择所有数据并删除给定 store_id 的记录 . - 缺点是,随着我们在表格中插入更多数据,这将花费越来越多的时间 .

  • 将数据保留在表格中 . - 这样做的唯一问题是我们将拥有无用的数据 .

  • 将表名与可用分区键存储在不同的表中,可以通过_1565297查询,从中获取密钥并为每个或那些键创建删除语句 . - 我不喜欢这个概念,因为我必须保持记录 .

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

2 回答

  • 1

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

    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) ;
    
  • 3

    无法通过部分分区键删除 .

    这是一种方法:

    创建一个单独的表,其中包含给定商店的所有product_id .

    CREATE TABLE product_by_store(
    store_id uuid,
    product_id set<text>,
    PRIMARY KEY(store_id)
    );
    

    现在写到 customer_bought_product ,也更新到 product_by_store ,喜欢的东西

    UPDATE product_by_store SET product_id=product_id + 'someValue' WHERE store_id=GIVEN_STORE_ID

    您可以在编写时使用BATCH语句,这样您将获得原子性 .

    现在,在删除时,您可以获取给定store_id的所有product_id,然后使用

    DELETE FROM customer_bought_product WHERE store_id=GIVEN_STORE_ID and product_id in (PRODUCT_ID YOU GET from product_by_store table)

    同时从 customer_bought_product 删除相应的记录

相关问题