Cassandra is an open source distributed database management system designed for processing large volumes of data across multiple typical servers. In addition, Spring Boot provides auto-configuration for Cassandra and abstractions provided through Spring Data Cassandra. For convenient collection of dependencies, there is a “starter” spring-boot-starter-data-cassandra.

Connecting to Cassandra

You can implement an autoconfigured CassandraTemplate or a CqlSession instance from Cassandra just like any other Spring bean. The spring.data.cassandra.* properties can be used to configure the connection. Typically, keyspace-name and contact-points are specified, as well as the name of the local data center, as shown in the following example:

Properties
spring.data.cassandra.keyspace-name=mykeyspace
spring.data.cassandra.contact-points=cassandrahost1:9042,cassandrahost2:9042
spring.data.cassandra.local-datacenter=datacenter1
Yaml
spring:
  data:
    cassandra:
      keyspace-name: "mykeyspace"
      contact-points: "cassandrahost1:9042,cassandrahost2:9042"
      local-datacenter: "datacenter1"

If the port is the same for all contact points, you can use a shortcut and specify only the host names, as shown in the following example:

Properties
spring.data.cassandra.keyspace-name=mykeyspace
spring.data.cassandra.contact-points=cassandrahost1,cassandrahost2
spring.data.cassandra.local-datacenter=datacenter1
Yaml
spring:
  data:
    cassandra:
      keyspace-name: "mykeyspace"
      contact-points: "cassandrahost1,cassandrahost2"
      local-datacenter: "datacenter1"
These two examples are identical because the default port is 9042. If you need to configure the port, use spring.data.cassandra.port.

The Cassandra driver has its own configuration framework that loads application.conf into the root classpath.

Spring Boot does not look for such a file by default, but can load it using spring.data.cassandra.config. If a property is present in both spring.data.cassandra.* and the configuration file, the value in spring.data.cassandra.* has a higher precedence level.

For deeper driver customization, you can register an arbitrary number of beans that implement DriverConfigLoaderBuilderCustomizer. CqlSession can be customized using a bean like CqlSessionBuilderCustomizer.

If you are using CqlSessionBuilder to create multiple CqlSession beans, be aware that the builder is mutable. Therefore, you should ensure that a new copy is deployed for each session.

The following code listing shows how to implement a Cassandra bean:

Java
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
    private final CassandraTemplate template;
    public MyBean(CassandraTemplate template) {
        this.template = template;
    }
 // ...
 public long someMethod() {
        return this.template.count(User.class);
    }
}
Kotlin
import org.springframework.data.cassandra.core.CassandraTemplate
import org.springframework.stereotype.Component
@Component
class MyBean(private val template: CassandraTemplate) {
 // ...
 fun someMethod(): Long {
        return template.count(User::class.java)
    }
}

If you add your own CassandraTemplate bean marked with the @Bean annotation, it will replace the default template.

Spring Data Cassandra Repositories

Spring Data contains repository support for Cassandra. They are currently more limited than the similar repository support for JPA discussed earlier and require lookup methods to be annotated with the @Query annotation.