Connect a Spring Boot application to a Cassandra database - step by step

To connect a Spring Boot application to a Cassandra database, follow these steps:

1. Add Dependencies

In your pom.xml (for Maven) or build.gradle (for Gradle), add the necessary dependencies for Spring Boot and Cassandra.

For Maven:

<dependencies>
<!-- Spring Boot Starter Data Cassandra -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>

<!-- Other dependencies you may need -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>


For Gradle:

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-cassandra'
implementation 'org.springframework.boot:spring-boot-starter-web'
}


2. Configure application.properties or application.yml

Using application.properties:

spring.data.cassandra.keyspace-name=my_keyspace
spring.data.cassandra.contact-points=127.0.0.1
spring.data.cassandra.port=9042
spring.data.cassandra.local-datacenter=datacenter1
spring.data.cassandra.schema-action=create_if_not_exists


Or using application.yml:

spring:
data:
cassandra:
keyspace-name: my_keyspace
contact-points: 127.0.0.1
port: 9042
local-datacenter: datacenter1
schema-action: create_if_not_exists

  • keyspace-name: The Cassandra keyspace to connect to.
  • contact-points: The IP address of the Cassandra cluster nodes (you can add multiple nodes for high availability).
  • port: The port on which Cassandra is running (default: 9042).
  • local-datacenter: The data center name configured in your Cassandra setup.
  • schema-action: Defines how the schema is handled (create_if_not_exists, none, etc.).


3. Create a Cassandra Entity

Define an entity class that maps to a Cassandra table. Use annotations from org.springframework.data.cassandra.core.mapping.

import org.springframework.data.cassandra.core.mapping.Table;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Column;

@Table
public class User {

@PrimaryKey
private String id;

@Column
private String name;

@Column
private int age;

// Getters and Setters
}


4. Create a Repository Interface

Use Spring Data's CassandraRepository to interact with Cassandra.

import org.springframework.data.cassandra.repository.CassandraRepository;

public interface UserRepository extends CassandraRepository<User, String> {
// You can define custom queries here if needed
}


5. Service or Controller to Use the Repository

Inject the repository into a service or controller class to interact with Cassandra.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

@Autowired
private UserRepository userRepository;

public void saveUser(User user) {
userRepository.save(user);
}

public User findUserById(String id) {
return userRepository.findById(id).orElse(null);
}
}


6. Run the Application

Make sure Cassandra is running locally or remotely, and that the connection details in application.properties are correct. Then run your Spring Boot application.

mvn spring-boot:run

or

./gradlew bootRun

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete