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:

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.

Table 1. Spring Data for Apache Cassandra - who does what?
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:

  • 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.

  • 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.

  • 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.

  • 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.