Framework Spring - datastaxdevs/awesome-astra GitHub Wiki
🏠 Back to home | Written by Cedrick Lunven, Last Update 3/11/2022

Spring makes programming Java quicker, easier, and safer for everybody. Spring’s focus on speed, simplicity, and productivity has made it the world's most popular Java framework.. To get more information regarding the framework visit the reference Spring.io.
Spring applications are packaged as standalone using Spring Boot
. To add capabilities to applications multiple starters
are provided by Spring. In the current page we will details which are the starters needed to interact Astra interfaces. Datastax team also implemented a dedicated astra-spring-boot-starter
to help you with the boiler plate code.
The Astra Spring Boot Starter once imported in a Spring Boot application, will configure both Astra SDK and Spring Data Cassandra to work with AstraDB. Configuration keys are read in application.yaml
like any spring applications with a dedicated prefix astra
.
The starter will initialize any beans you would need (AstraClient
, CqlSession
, StargateClient
) to use all interfaces exposes by Astra. Not all are activated by default, you want to initialize only what you need.
- You should have an Astra account
- You should Create and Astra Database
- You should Have an Astra Token
- You should install Java Development Kit (JDK) 8: Use the reference documentation to install a Java Development Kit, Validate your installation with
java --version
- You should install Apache Maven: Use the reference documentation and validate your installation with
mvn -version
- Create your project with Spring Initializr. Dependencies needed are
web
anddata-cassandra
but we did the work for you if you click the template link
Property | Value | Property | Value |
---|---|---|---|
groupId | com.datastax.tutorial |
package | com.datastax.tutorial |
artifactId | sdk-quickstart-spring |
description | Sample Spring App |
name | sdk-quickstart-spring |
dependencies |
Spring Web and Spring Data for Cassandra
|
packaging | JAR |
Java Version |
8 or 11
|

-
Import the application in your favorite IDE but do not start the application immediately.
-
Add the latest version of starter as a dependency in
pom.xml
of
astra-spring-boot-starter
in the project.
<dependency>
<groupId>com.datastax.astra</groupId>
<artifactId>astra-spring-boot-starter</artifactId>
<version>0.3.0</version>
</dependency>
- Change the main class with the following code, we are leveraging on the unique
AstraClient
to interact with multiple interfaces.
@RestController
@SpringBootApplication
public class SdkQuickstartSpringApplication {
public static void main(String[] args) {
SpringApplication.run(SdkQuickstartSpringApplication.class, args);
}
// Provided by the Starter
@Autowired
private AstraClient astraClient;
// Spring Data using the CqlSession initialized by the starter
@Autowired
private CassandraTemplate cassandraTemplate;
@GetMapping("/api/devops/organizationid")
public String showOrganizationId() {
return astraClient.apiDevopsOrganizations().organizationId();
}
@GetMapping("/api/spring-data/datacenter")
public String showDatacenterNameWithSpringData() {
return cassandraTemplate.getCqlOperations()
.queryForObject("SELECT data_center FROM system.local", String.class);
}
@GetMapping("/api/cql/datacenter")
public String showDatacenterNameWithSpringData() {
return astraClient.cqlSession()
.execute("SELECT data_center FROM system.local")
.one().getString("data_center");
}
}
- Rename
src/main/resources/application.properties
tosrc/main/resources/application.yaml
. This step eases the configuration with hierarchical keys. Populateapplication.yaml
with the following content and replace the values with expected values (how to retrieve the values are explained in the Quickstart Astra
astra:
# Allow usage of devops and Stargate apis
api:
application-token: <your_token>
database-id: <your_database_id>
database-region: <your_database_region>
# Connectivity to Cassandra
cql:
enabled: true
download-scb:
enabled: true
driver-config:
basic:
session-keyspace: <your_keyspace>
- Start the application
mvn clean install spring-boot:run
- Access the resources we created
- Get your Organization ID: http://localhost:8080/api/devops/organizationid
- Get your Datacenter Name (Spring-data): http://localhost:8080/api/spring-data/datacenter
- Get your Datacenter Name (cql): http://localhost:8080/api/cql/datacenter
As describe in the Spring documentation, there are multiple ways to interact with Cassandra. In this tutorial we will show you how to use all of them
-
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.
-
CassandraTemplate
wraps aCqlTemplate
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.
ℹ️ Connecting Spring Data Cassandra to Astra
Spring Data Cassandra leverages on a CqlSession
bean, all the time. To build the CqlSession
there area again multiple ways described in the documentation from custom code to AbstractCassandraConfiguration
. FORGET ABOUT IT.
Astra Spring Boot Starter creates the CqlSession
bean for you using the keys previously listed as such you do not need to use configuration keys like spring.data.cassandra
(or very very few for special behaviour)
This one is straight forward. Remove the exclude
in @SpringBootApplication
@SpringBootApplication
You can restart the application, spring data is connected to Astra.
- Create a bean
Todos
in the same packagecom.datastax.tutorial
@Table
public class Todos {
@PrimaryKey
@CassandraType(type = Name.UUID)
private UUID uid = UUID.randomUUID();
private String title;
private boolean completed = false;
public Todos() {}
public Todos(String title) { this.title = title; }
//_getters and setters have been omitted here
- Create an interface
TodosRepository
in the same packagecom.datastax.tutorial
package com.datastax.tutorial;
import org.springframework.data.cassandra.repository.CassandraRepository;
public interface TodosRepository extends CassandraRepository<Todos, String> {}
- Edit the
QuickStartSpring
to add the following:
@Autowired
private TodosRepository todoRepository;
@PostConstruct
public void insertTodos() {
todoRepository.save(new Todos("Create Spring Project"));
todoRepository.save(new Todos("Setup Astra Starter"));
todoRepository.save(new Todos("Setup Spring Starter"));
}
@GetMapping("/todos")
public List<Todos> todos() {
return todoRepository.findAll(CassandraPageRequest.first(10)).toList();
}
- Finally tells Spring Data to create for us the tables with configuration. (not for CqlSession, only for this). In
application.yaml
add the following:
spring:
data:
cassandra:
schema-action: CREATE_IF_NOT_EXISTS
- You can restart your application and access http://localhost:8080/todos
[
{
"uid": "83d7a60d-1f24-42c5-aa16-9275f36dc312",
"title": "Setup Spring Starter",
"completed": false
},
{
"uid": "95e8a502-786d-4dd2-983a-b451a12877fe",
"title": "Setup Astra Starter",
"completed": false
},
{
"uid": "44da79c3-73a6-46d0-84cb-3afa2a96d99e",
"title": "Create Spring Project",
"completed": false
}
]
ℹ️ Note: Each time you restart the application you will get 3 new tasks as the primary is an generated UUID.
CassandraTemplate
is the bean initialized by Spring-Data. It embeds the CqlTemplate
= CqlOperations
.
- Add the following to your main class
@Autowired
private CassandraTemplate cassandraTemplate;
@GetMapping("/datacenter")
public String datacenter() {
return cassandraTemplate
.getCqlOperations()
.queryForObject("SELECT data_center FROM system.local", String.class);
}
- You can restart your application and access http://localhost:8080/datacenter
-
Spring data
-
Spring data cassandra
-
Leveraging java drivers
-
10 pitfalls of Spring Data Cassandra
TODO
- Working with 3.x driver
- 3.8 is required
- how to enforce version
- compatibility table
TODO
- Working with 4.x drivers
- CqlSession Customizer
- Create your own Cql Session
TODO
TODO
TODO
TODO
TODO