Using the Cluster bean from CassandraAutoConfiguration makes it quite difficult to provide a custom keyspace creation (e.g. via CreateKeyspaceSpecification).
While CassandraClusterFactoryBean#setKeyspaceCreations provides support for custom keyspace creations I couldn't find anything similar when the cluster is created via the auto-configuration.
Trying to find a workaround that doesn't duplicate existing auto-configuration code led to this ugly custom configuration...
@Configuration
@Import(CassandraDataAutoConfiguration.class)
@EnableConfigurationProperties(CassandraProperties.class)
public class CassandraCustomConfiguration {
@Bean
Cluster cluster(CassandraProperties properties, ObjectProvider<ClusterBuilderCustomizer> builderCustomizers) {
CassandraAutoConfiguration cassandraAutoConfiguration = new CassandraAutoConfiguration(properties, builderCustomizers);
Cluster cluster = cassandraAutoConfiguration.cassandraCluster();
createKeyspace(cluster, getSimpleKeyspaceCreation());
return cluster;
}
private CreateKeyspaceSpecification getSimpleKeyspaceCreation() {
return CreateKeyspaceSpecification.createKeyspace("foo").ifNotExists(); // simplified sample
}
private void createKeyspace(Cluster cluster, CreateKeyspaceSpecification keyspaceCreation) {
try (Session session = cluster.connect()) {
CqlTemplate template = new CqlTemplate(session);
template.execute(CreateKeyspaceCqlGenerator.toCql(keyspaceCreation));
}
}
}
It would be nice if the auto-configuration would provide support for keyspace creations.
Using the
Clusterbean fromCassandraAutoConfigurationmakes it quite difficult to provide a custom keyspace creation (e.g. viaCreateKeyspaceSpecification).While
CassandraClusterFactoryBean#setKeyspaceCreationsprovides support for custom keyspace creations I couldn't find anything similar when the cluster is created via the auto-configuration.Trying to find a workaround that doesn't duplicate existing auto-configuration code led to this ugly custom configuration...
It would be nice if the auto-configuration would provide support for keyspace creations.