ThrowableException
ThrowableException

Reputation: 1426

Spring-Data-Cassandra SchemaAction not working

I'm trying to configure Cassandra in my application by extending CassandraAutoConfiguration

I'm using spring CassandraRepository for DB access and classes with o.s.d.cassandra.core.mapping.Table annotation for defining my tables.

I've also configured following property, along with other required properties for cluster

spring: 
  data:
   cassandra:
     schema-action: CREATE_IF_NOT_EXISTS

But No table get created in Cassandra upon application startup. schemaAction in CassandraProperties is not working.

If I programmatically create tables upon startup in my ApplicationRunner by using cassandraTemplate.getCqlOperations().execute(...) then everything works fine.

In this case I am able to use my repository. find() and save() methods. Just to prove that my @table classes are correctly written

Upvotes: 5

Views: 2432

Answers (1)

clunven
clunven

Reputation: 1685

Here is the behaviour I noticed. This is not only true for this particular key in application.yaml

  • When you don't create any bean extending AbstractCassandraConfiguration spring-data will read every key matching spring.data.* in application.yaml including the schema-action you provided. (by CONVENTION). I don't seen any issue with the file you provided, as a matter of fact I have a working sample here

  • When you create a bean extending AbstractCassandraConfiguration, now this is your job to implements explicitly the values you want as such please add in your class. Also you will need ro provide explicitly annotation @EnableCassandraRepositories

@Value("${spring.data.cassandra.schema-action}")
private String schemaAction;
    
@Override
public SchemaAction getSchemaAction() {
  return SchemaAction.valueOf(schemaAction);
}

On top of this I would like to advise NOT USING IT AT ALL. Spring Data works like a charm but here are my concerns:

  • Creating a table is not only a matter of matching the data model. Indeed what about Compaction Strategy based on your use case or TTL or any metadata.

  • We assume you know how to build a primary key properly with Partitions key and Clustering column but what if you need to store the exact same object in 2 tables because you have 2 different queries on it. (remember: I you need ALLOW FILTERING anywhere in your application=> your data model is probably wrong.

Upvotes: 4

Related Questions