【问题标题】:Cassandra Delete query with If condition not workingCassandra删除查询,如果条件不起作用
【发布时间】:2019-08-06 23:59:43
【问题描述】:

我有一个 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';

【问题讨论】:

  • 这个集群有多少个节点?试试 CONSISTENCY QUORUM 吗?
  • 我已经执行了“CONSISTENCY QUORUM;”又试了一次,还是不行。
  • 也许我应该提到,如果我放弃 if 条件,删除是可行的。

标签: cassandra


【解决方案1】:

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 语句中的单元格的值设置为 null。通过删除或设置空值,您仍在创建墓碑。

【讨论】:

  • 所以删除有条件的查询并没有达到我的预期。但是他们在做什么呢?
  • 正如我所说,您可以将列设置为 null(并创建不推荐的墓碑)或只进行两次调用。
猜你喜欢
  • 2016-05-25
  • 1970-01-01
  • 2017-07-15
  • 2017-08-05
  • 2017-02-25
  • 2015-10-01
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多