|
This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Data Cassandra 4.5.2! |
Cassandra Support
This part of the reference documentation explains the core functionality offered by Spring Data for Apache Cassandra. Spring Data support for Apache Cassandra contains a wide range of features:
-
Spring configuration support with Java-based
@Configurationclasses or the XML namespace. -
The
CqlTemplate,AsyncCqlTemplate, andReactiveCqlTemplatehelper classes that increases productivity by properly handling common Cassandra data access operations. -
The
CassandraTemplate,AsyncCassandraTemplate, andReactiveCassandraTemplatehelper classes that provide object mapping between CQL Tables and POJOs. -
Exception translation into Spring’s portable Data Access Exception Hierarchy.
-
Feature rich object mapping integrated with Spring’s Conversion Service.
-
Annotation-based mapping metadata that is extensible to support other metadata formats.
-
Java-based query, criteria, and update DSLs.
-
Automatic implementation of imperative and reactive
Repositoryinterfaces including support for custom query methods.
Abstractions
Spring Data for Apache Cassandra allows interaction on both the CQL and the entity level.
The value provided by the Spring Data for Apache Cassandra abstraction is perhaps best shown by the sequence of actions outlined in the table below. The table shows which actions Spring take care of and which actions are the responsibility of you, the application developer.
| Action | Spring | You |
|---|---|---|
Define connection parameters. |
X |
|
Open the connection. |
X |
|
Specify the CQL statement. |
X |
|
Declare parameters and provide parameter values |
X |
|
Prepare and run the statement. |
X |
|
Set up the loop to iterate through the results (if any). |
X |
|
Do the work for each iteration. |
X |
|
Process any exception. |
X |
|
Close the Session. |
X |
The core CQL support takes care of all the low-level details that can make Cassandra and CQL such a tedious API with which to develop. Using mapped entity objects allows schema generation, object mapping, and repository support.
Choosing an Approach for Cassandra Database Access
You can choose among several approaches to use as a basis for your Cassandra database access. Spring’s support for Apache Cassandra comes in different flavors. Once you start using one of these approaches, you can still mix and match to include a feature from a different approach. The following approaches work well:
-
CqlTemplateandReactiveCqlTemplateare the classic Spring CQL approach and the most popular. This is the “lowest-level” approach. Note that components likeCassandraTemplateuseCqlTemplateunder-the-hood. -
CassandraTemplatewraps aCqlTemplateto provide query result-to-object mapping and the use ofSELECT,INSERT,UPDATE, andDELETEmethods instead of writing CQL statements. This approach provides better documentation and ease of use. -
ReactiveCassandraTemplatewraps aReactiveCqlTemplateto provide query result-to-object mapping and the use ofSELECT,INSERT,UPDATE, andDELETEmethods instead of writing CQL statements. This approach provides better documentation and ease of use. -
Repository Abstraction lets you create repository declarations in your data access layer. The goal of Spring Data’s repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.
For most data-oriented tasks, you can use the [Reactive|Async]CassandraTemplate or the Repository support, both of which use the rich object-mapping functionality. [Reactive|Async]CqlTemplate is commonly used to increment counters or perform ad-hoc CRUD operations. [Reactive|Async]CqlTemplate also provides callback methods that make it easy to get low-level API objects, such as com.datastax.oss.driver.api.core.CqlSession, which lets you communicate directly with Cassandra.
Spring Data for Apache Cassandra uses consistent naming conventions on objects in various APIs to those found in the DataStax Java Driver so that they are familiar and so that you can map your existing knowledge onto the Spring APIs.