0
votes

I am working on a spring boot application that uses two different keyspaces in a cassandra database. In the process of trying to upgrade from spring boot 2.1.7-RELEASE to version 2.4.0, I have found that my java-based configuration no longer works. It seems to be much more difficult than before to extend AbstractCassandraConfiguration due to creation of multiple beans of the same type and ambiguity caused by having multiple cassandra/cql session factories.

All of the examples of a multi-keyspace configuration I have found online are for older versions of Spring Boot, so I am wondering if anyone has figured out a clean way to get this working in the more recent versions?

2

2 Answers

1
votes

I migrated my project to using spring-data-cassandra 3.x. I resolved issues with conflicting beans by overriding CqlSessionFactoryBean bean in configurations and adding @Primary annotation for primary keyspace cassandra session bean. My project is using following gradle dependencies:

compile group: 'org.springframework.data', name: 'spring-data-cassandra', version: '3.0.5.RELEASE'
compile group: 'com.datastax.oss', name: 'java-driver-core', version: '4.6.1'

Cassandra configurations classes:

public abstract class AbstractCassandraSourceConfiguration extends AbstractCassandraConfiguration {
    @Autowired
    protected Environment environment;

    @Override
    protected int getPort() {
        return Integer.parseInt(environment.getProperty("cassandra.port"));
    }

    @Override
    protected String getContactPoints() {
        return environment.getProperty("cassandra.contactpoints");
    }

    @Override
    protected String getLocalDataCenter() {
        return environment.getProperty("cassandra.data.center",
                                       "datacenter1");
    }

    @Override
    protected abstract String getKeyspaceName();
}

@Configuration
@EnableCassandraRepositories(basePackages = "com.data.cassandra.primary.repository",
                             repositoryBaseClass = CassandraRepositoryWithTtlWithTimestampImpl.class)
public class PrimaryCassandraSourceConfiguration extends AbstractCassandraSourceConfiguration {
    @Override
    protected String getKeyspaceName() {
        return "primary_keyspace";
    }

    @Override
    @Bean(name = "cassandraSession")
    @Primary
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean session = super.cassandraSession();
        session.setKeyspaceName(getKeyspaceName());
        return session;
    }

    @Bean(name = "cassandraTemplate")
    public CassandraAdminOperations cassandraTemplate(
            @Qualifier("cassandraSession") final CqlSessionFactoryBean session) {
        return new CassandraAdminTemplate(session.getObject(),
                                          cassandraConverter());
    }
}

@Configuration
@EnableCassandraRepositories(cassandraTemplateRef = "secondaryCassandraTemplate",
                             basePackages = "com.data.cassandra.secondary.repository",
                             repositoryBaseClass = CassandraRepositoryWithTtlWithTimestampImpl.class)
public class SecondaryCassandraSourceConfiguration extends AbstractCassandraSourceConfiguration {
    @Override
    protected String getKeyspaceName() {
        return "secondary_keyspace";
    }

    @Override
    @Bean(name = "secondaryCassandraSession")
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean session = super.cassandraSession();
        session.setKeyspaceName(getKeyspaceName());
        return session;
    }

    @Bean(name = "secondaryCassandraTemplate")
    public CassandraAdminOperations cassandraTemplate(
            @Qualifier("secondaryCassandraSession") final CqlSessionFactoryBean session) {
        return new CassandraAdminTemplate(session.getObject(),
                                          cassandraConverter());
    }
}
0
votes

I'm using spring-data-cassandra=3.0.4.RELEASE.

In my project I needed to connect to 2 keyspaces from 1 DC each. What I've configured in Cassandra @Configuration Bean is 1 Bean of Session, SessionFactoryFactoryBean and CassandraOperations per keyspace.