I devised a project with the usage of Spring boot and Cassandra which runs on Docker Container.
After I implemented the configuration of Cassandra, I ran the project and it threw an error shown below.
Caused by: com.datastax.oss.driver.api.core.InvalidKeyspaceException: Invalid keyspace mykeyspace
How can I fix the issue?
Here is my application.properties file.
spring.cassandra.contactpoints=127.0.0.1
spring.cassandra.port=9042
spring.data.cassandra.keyspace-name=mykeyspace
spring.cassandra.basepackages=com.springboot.cassandra
Here is a configuration file of Cassandra
@Configuration
@EnableCassandraRepositories
public class CassandraConfiguration extends AbstractCassandraConfiguration {
@Value("${spring.cassandra.contactpoints}")
private String contactPoint;
@Value("${spring.cassandra.port}")
private int port;
@Value("${spring.data.cassandra.keyspace-name}")
private String keyspaceName;
@Value("${spring.cassandra.basepackages}")
private String basePackages;
@Override
protected String getKeyspaceName() {
return keyspaceName;
}
@Override
protected int getPort() {
return port;
}
@Override
protected String getContactPoints() {
return contactPoint;
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
@Override
public String[] getEntityBasePackages() {
return new String[] {basePackages};
}
}
By default Spring Data will not create or alter the schema for you. This is a good thing for most use cases as normally you do not want your schema to be created based on a java class. Altering would be even worse in general and also difficult for Cassandra in particular.
If you want for spring to create it you need:
spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS
I would still recommend not using this in production.
When talking about keyspaces however, from my knowledge and from the wording of the documentation spring will not create the keyspace even if you use the code above. This makes a lot of sense for Cassandra as a keyspace needs info such as replication strategy and replication factor, the latter being changed for things such as new Data centers being added or removed. This things are administrative tasks that should not be left up to Spring.