Spring Cassandra 批量操作仅通过分区键删除

huangapple go评论69阅读模式
英文:

Spring cassandra Batch Operation delete by partition key only

问题

我需要在使用Spring和Spring Boot的批处理操作下,在Cassandra中仅使用分区键执行删除操作(删除所有具有分区键的记录),但是CassandraBatchOperations的delete方法只接受完整的实体对象作为输入,例如:

CassandraBatchOperations delete(Object... entities);

我有一张表,假设为table1,它有以下键:

  • key1 - 分区键
  • key2 - 聚簇键1
  • key3 - 聚簇键2

所以我的需求是在批处理操作中运行以下查询:

DELETE from table1 where key1='input key';

当我创建一个对象如下:

TableEntity recordToDelete = new Table1Entity();
recordToDelete.setKey1('input key');

并且像这样运行batchOperations:

CassandraBatchOperations batchOps = cassandraTemplate.batchOps();
batchOps.delete(recordToDelete);

然后生成的有效查询是:

DELETE from table1 where key1='input key' and key2=null and key3=null

然后我收到以下异常:

org.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

问题是生成的查询还考虑了聚簇键key2和key3,而它们没有值,而我只想根据分区键删除。

我想知道如何只根据分区键删除,从数据库中获取记录的列表不是一个选项,因为我还在批处理操作中将记录插入Cassandra,可能会发生在同一批处理操作中插入具有我想要删除的分区键的记录的情况。因此,在这种情况下,如果我获取并删除记录,将不会删除批处理操作中正在插入的新记录。

英文:

I have a requirement in which i have to perform a delete operation in cassandra by using only partiton key (delete all records with the partition key ) under batch operation using spring and springboot ,but the CassandraBatchOperations 's delete method only takes in input full entity object like

CassandraBatchOperations delete(Object... entities);

i have a table say table1
and it has keys :
key1- partiton key ,
key2 -clustering key1 ,
key 3-clustering key2

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

then i am getting below exception

>  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

The problem is the query getting create is also considering the clustering key key2 and key3 which does not have values as i want to delete only by partition key .

I want to know how can i delete only by partiton Key ,
Getting the list of record from DB is not an option as i am also inserting the records in cassandra under same batch operation and it can happen that there is also a record getting instered in the same batch operation which has the partition key that i want to delete . so in that case if i get and delete the record the new record that is getting inserted in batch operation will not get deleted .

答案1

得分: 1

这个问题的答案是我的实体类包含了所有的主键(正如它应该的那样),所以当Cassandra模板创建查询时,它会选择所有的键。

原始实体类

@Table(CassandraDBSchemaConstants.TABLE1)
public class Table {
    @PrimaryKeyColumn (name = CassandraDBSchemaConstants.KEY1 , type = PrimaryKeyType.PARTITIONED)
    private String key1;
    
    @PrimaryKeyColumn(name = CassandraDBSchemaConstants.KEY2 , type = PrimaryKeyType.CLUSTERED)
    private String key2;
    
    @PrimaryKeyColumn(name = CassandraDBSchemaConstants.KEY3 , type = PrimaryKeyType.CLUSTERED)
    private String key3;
    
    // 其他列
}

所以作为解决方法:我创建了一个新的实体类,与现有的实体类具有相同的表名,但只有一个主键(分区键)。

除现有实体类外的新类

@Table(CassandraDBSchemaConstants.TABLE1)
public class TableOnlyByPartitonKey {
    @PrimaryKeyColumn (name = CassandraDBSchemaConstants.KEY1 , type = PrimaryKeyType.PARTITIONED)
    private String key1;
}

当我需要仅按分区键删除时,我将新的实体类传递给Cassandra批处理操作的删除方法,执行的查询仅通过新实体类中存在的分区键删除记录。

英文:

The Answer to this question was my Entity class was containing all the pk ( as it should)
so when the Cassandra Template created the query it was picking up all the keys

Original Entity class

@Table(CassandraDBSchemaConstants.TABLE1)
public class Table {
	@PrimaryKeyColumn (name = CassandraDBSchemaConstants.KEY1 , type = PrimaryKeyType.PARTITIONED)
	private String key1;
	
	@PrimaryKeyColumn(name = CassandraDBSchemaConstants.KEY2 , type = PrimaryKeyType.CLUSTERED)
	private String key2;
	
	@PrimaryKeyColumn(name = CassandraDBSchemaConstants.KEY3 , type = PrimaryKeyType.CLUSTERED)
	private String key3;
	
	
	..//other columns
	
	}

so as fix : I created a new Entity class with same table name with only 1 key (partition key ) in it

a new class apart from the existing entity class

@Table(CassandraDBSchemaConstants.TABLE1)
public class TableOnlyByPartitonKey {
	@PrimaryKeyColumn (name = CassandraDBSchemaConstants.KEY1 , type = PrimaryKeyType.PARTITIONED)
	private String key1;
	
	}

and when I had to delete by partition key only I passed the new entity class to the cassandra batch operation's delete method

and the query that got executed deleted the record only by the partition key which was present in new entity class

huangapple
  • 本文由 发表于 2020年8月7日 12:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63295459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定