代码之家  ›  专栏  ›  技术社区  ›  Aki

cassandra删除if条件不起作用的查询

  •  0
  • Aki  · 技术社区  · 7 年前

    我有一个cassandra表,想要删除一行,但是只有一列有一个特定的值。

    即使Cassandra声称删除成功(它返回“applied:true”),消息仍然存在。


    让我们创建表并插入一些数据:

    CREATE TABLE IF NOT EXISTS test
    (
         id uuid PRIMARY KEY, 
         recipient text, 
         message text
    );
    
    INSERT INTO test (id, recipient, message) 
    VALUES (7ee055ee-b5dd-4bfd-b184-614d51e268d5, 'felix', 'foo');
    
    INSERT INTO test (id, recipient, message) 
    VALUES (86c9d632-dc24-4635-8277-c987c78bd242, 'andrew', 'bar');
    

    现在,我想删除一条消息,但前提是请求删除的用户(在本例中是felix)是收件人,因此具有执行此操作的权限:

    cqlsh:service_message> DELETE FROM test WHERE id=7ee055ee-b5dd-4bfd-b184-614d51e268d5 IF recipient='felix';
    
     [applied]
    -----------
          True
    

    所以我现在认为查询确实成功了,但是如果我们查看表,就会发现消息仍然存在。

    cqlsh:service_message> SELECT * FROM test;
    
     id                                   | message | recipient
    --------------------------------------+---------+-----------
     86c9d632-dc24-4635-8277-c987c78bd242 |     bar |    andrew
     7ee055ee-b5dd-4bfd-b184-614d51e268d5 |     foo |     felix
    
    (2 rows)
    

    一些附加信息:

    cqlsh 5.0.1 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4
    
    cqlsh> DESCRIBE KEYSPACE service_message 
    
    CREATE KEYSPACE service_message WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;
    
    CREATE TABLE service_message.test (
        id uuid PRIMARY KEY,
        message text,
        recipient text
    ) WITH bloom_filter_fp_chance = 0.01
        AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
        AND comment = ''
        AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
        AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
        AND crc_check_chance = 1.0
        AND dclocal_read_repair_chance = 0.1
        AND default_time_to_live = 0
        AND gc_grace_seconds = 864000
        AND max_index_interval = 2048
        AND memtable_flush_period_in_ms = 0
        AND min_index_interval = 128
        AND read_repair_chance = 0.0
        AND speculative_retry = '99PERCENTILE';
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Rahul Singh    7 年前

    INSERT and UPDATE statements using the IF clause support lightweight transactions .

    来自cql上的datastax文档: https://docs.datastax.com/en/cql/3.3/cql/cql_using/useInsertLWT.html

    我确信不支持删除。如果要有效地删除信息,可以考虑将UPDATE语句中单元格的值设置为空。通过删除或设置空值,您仍在创建逻辑删除。