SpringData Reactive Cassandra Repositories | SpringBoot

Reactive programming is about non-blocking applications. With the previous post, we had learned how to use SpringBoot WebFlux. In the tutorial, JavaSampleApproach will explore more about SpringData Reactive Cassandra Repositories with a set of high-level abstraction CRUD methods.

Related posts:
SpringBoot WebFlux Annotation-based RestAPIs
SpringBoot WebFlux Functional RestAPIs
How to start Spring Data Cassandra with SpringBoot
Angular + Spring WebFlux + Spring Data Reactive Cassandra example | Full-Reactive Angular Http Client – Spring Boot RestApi Server

I. Technologies

– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.9.0.RELEASE
– Spring Boot: 2.0.0.M4
– Spring Boot Starter Webflux
– Spring Boot Starter Data Cassandra Reactive
– Cassandra version: 3.9.0

II. SpringData Reactive Cassandra

Cassandra Reactive SpringData provides repositories with a set of high-level abstraction CRUD methods or templates for working. In the tutorial, we focus on Reactive Cassandra repositories implementation.

For working with ReactiveCassandra, we need dependency:

How to work?

We use AbstractReactiveCassandraConfiguration to configure Reactive Cassandra database:

We use ReactiveCrudRepository to define reactive-repositories:

We can pass parameters to query methods as plain (e.g. String), wrapped (Optional, Stream) or reactive wrapped arguments (Mono, Flux).
When using reactive wrappers (Mono, Flux) parameters, query creation and execution will be deferred until having an actual subscription.

We can combine Spring Web Reactive and SpringData Reactive in a reactive processing way:

III. Practice

We create a SpringBoot project as below:

springdata reactive cassandra repositories - project structure

Step to do:
– Create SpringBoot project
– Create data model
– Configure Cassandra Reactive
– Implement Reactive repository
– Implement WebFlux RestAPIs
– Implement initial client
– Run and check results

1. Create SpringBoot project

Using SpringToolSuite to create SpringBoot project, with needed dependencies:

springdata reactive cassandra repositories - add depedencies

Check pom.xml file after finishing:

2. Create data model

3. Configure Cassandra Reactive

4. Implement Reactive repository

5. Implement WebFlux RestAPIs

6. Implement initial client

7. Run and check results
7.1 Set up Apache Cassandra

Download Apache Cassandra:
– For Linux, go to the site Downloading Cassandra.
– For Windows, go to the site DataStax Distribution.

In the tutorial, environment: {Windows 10, Apache Cassandra – v3.9.0} to demo. After downloading, we have a file: datastax-ddc-64bit-3.9.0.msi. Now just double click on datastax-ddc-64bit-3.9.0.msi then do step by step to install.

Now start Cassandra server:

springdata reactive cassandra repositories - start cassandra server

Open Cassandra CQL Shell to create Cassandra table:

– Create Cassandra keyspace with name javasampleapproach:

– Create customer table for javasampleapproach keyspace:

– Create an index on age column:

springdata reactive cassandra repositories - create javasampleapproach keyspace

7.2 Run and check results

– Build & Run the project with SpringBoot App mode: {mvn clean install, mvn spring-boot:run}.

-> Check Cassandra customer table:

springdata reactive cassandra repositories - select all

– Make a request to get all customers http://localhost:8080/api/customer

springdata reactive cassandra repositories - request select all

– Make a request to get a customer by id http://localhost:8080/api/customer/1

springdata reactive cassandra repositories - request select a customer by id

– Make a request to get list full name of customers by age http://localhost:8080/api/customer/findbyage?age=24

springdata reactive cassandra repositories - request find by age

– Make a request to get a Customer by full name http://localhost:8080/api/customer/findbynames?firstname=Skyler&lastname=White

springdata reactive cassandra repositories - find by name

IV. Sourcecode


By grokonez | September 25, 2017.

Last updated on March 23, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *