For the latest stable version, please use Spring Data Cassandra 4.4.0!spring-doc.cn

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-doc.cn

Abstractions

Spring Data for Apache Cassandra allows interaction on both the CQL and the entity level.spring-doc.cn

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.spring-doc.cn

Table 1. Spring Data for Apache Cassandra - who does what?
Action Spring You

Define connection parameters.spring-doc.cn

Xspring-doc.cn

Open the connection.spring-doc.cn

Xspring-doc.cn

Specify the CQL statement.spring-doc.cn

Xspring-doc.cn

Declare parameters and provide parameter valuesspring-doc.cn

Xspring-doc.cn

Prepare and run the statement.spring-doc.cn

Xspring-doc.cn

Set up the loop to iterate through the results (if any).spring-doc.cn

Xspring-doc.cn

Do the work for each iteration.spring-doc.cn

Xspring-doc.cn

Process any exception.spring-doc.cn

Xspring-doc.cn

Close the Session.spring-doc.cn

Xspring-doc.cn

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.spring-doc.cn

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:spring-doc.cn

  • CqlTemplate and ReactiveCqlTemplate are the classic Spring CQL approach and the most popular. This is the “lowest-level” approach. Note that components like CassandraTemplate use CqlTemplate under-the-hood.spring-doc.cn

  • CassandraTemplate wraps a CqlTemplate to provide query result-to-object mapping and the use of SELECT, INSERT, UPDATE, and DELETE methods instead of writing CQL statements. This approach provides better documentation and ease of use.spring-doc.cn

  • ReactiveCassandraTemplate wraps a ReactiveCqlTemplate to provide query result-to-object mapping and the use of SELECT, INSERT, UPDATE, and DELETE methods instead of writing CQL statements. This approach provides better documentation and ease of use.spring-doc.cn

  • 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.spring-doc.cn

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.spring-doc.cn