2020-02-07

Spring Cassandra auto-configuration

Cassandra is an open source, distributed database management system designed to handle large
amounts of data across many commodity servers. Spring Boot offers auto-configuration for
Cassandra and the abstractions on top of it provided by Spring Data Cassandra.
There is a springboot-starter-data-cassandra “Starter” for collecting the dependencies in a convenient way.

Connecting to Cassandra

You can inject an auto-configured CassandraTemplate or a Cassandra CqlSession instance as you
would with any other Spring Bean. The spring.data.cassandra.* properties can be used to
customize the connection. Generally, you provide keyspace-name and contact-points as well the local
datacenter name, as shown in the following example:

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

You can also register an arbitrary number of beans that implement
DriverConfigLoaderBuilderCustomizer for more advanced driver customizations. The CqlSession can
be customized with a bean of type CqlSessionBuilderCustomizer.
The following code listing shows how to inject a Cassandra bean:

@Component
public class MyBean {
private final CassandraTemplate template;
@Autowired
public MyBean(CassandraTemplate template) {
this.template = template;
}
// ...
}
If you add your own @Bean of type CassandraTemplate, it replaces the default.

Spring Data Cassandra Repositories

Spring Data includes basic repository support for Cassandra. Currently, this is more limited than
the JPA repositories discussed earlier and needs to annotate finder methods with @Query.

No comments:

Post a Comment