我有一个要求,在cassandra中,我必须在使用spring和springboot的批处理操作下,仅使用partiton键(使用分区键删除所有记录)来执行delete操作,但是cassandrabatchoperations的delete方法只接受输入完整实体对象,如
CassandraBatchOperations delete(Object... entities);
我有一个表,比如table1,它有键:key1-partiton键,key2-clustering键1,key3-clustering键2
so my requirement is that in batch operation below query should run
DELETE from table1 where key1='input key';
so when i create an object like
tableEntity recordToDelete=new Table1Entity();
recordToDelete.setKey1('input key');
and run batchOperations like
CassandraBatchOperations batchOps=cassandraTemplate.batchOps();
batchOps.delete(recordToDelete);
then the effective query getting generated is
DELETE from table1 where key1='input key' and key2=null and key3=null
那我就到下面去了
> rg.springframework.data.cassandra.CassandraInvalidQueryException:
> Query; CQL [BEGIN BATCH DELETE FROM table1 WHERE key2=null AND
> key3=null AND key1='0002';APPLY BATCH;]; Invalid null value in
> condition for column key2; nested exception is
> com.datastax.driver.core.exceptions.InvalidQueryException: Invalid
> null value in condition for column key2
问题是获取create的查询还考虑了集群键key2和key3,它们没有值,因为我只想按分区键删除。
我想知道如何只通过partiton键删除,从db中获取记录列表不是一个选项,因为我还在cassandra中插入同一批操作下的记录,并且可能发生的情况是,在同一批操作中也有一个记录被插入,它具有我要删除的分区键。因此,在这种情况下,如果我获取并删除记录,那么在批处理操作中插入的新记录将不会被删除。
1条答案
按热度按时间aiqt4smr1#
这个问题的答案是我的实体类包含了所有的pk(正如它应该包含的那样),所以当cassandra模板创建查询时,它会获取所有的键
原始实体类
所以作为修复:我创建了一个新的实体类,它的表名相同,只有一个键(分区键)
现有实体类之外的新类
当我只需要按分区键删除时,我将新的实体类传递给cassandra批处理操作的delete方法
执行的查询仅通过新实体类中的分区键删除记录