How to Save Data in a Cassandra Database Using Spring Boot

In modern software development, the need for scalable, high-performance databases is greater than ever. Apache Cassandra is a powerful NoSQL database designed for handling large amounts of data across many servers, with no single point of failure. In this article, you’ll learn how to save data in a Cassandra database using Spring Boot—a popular Java framework that simplifies building enterprise-grade applications.


Why Use Cassandra with Spring Boot?

Cassandra is favored for:

  • High availability and fault tolerance
  • Linear scalability
  • Schema flexibility

Spring Boot makes Java development easier with:

  • Auto-configuration
  • Dependency management
  • Easy integration with Spring Data Cassandra

When combined, Spring Boot and Cassandra offer a robust solution for real-time data applications.


Prerequisites

  • JDK 17+ installed
  • Apache Cassandra running locally or remotely
  • Maven or Gradle build tool
  • Spring Boot application (Java)

🔧 Step-by-Step Guide to Save Data in Cassandra with Spring Boot

1. Add Spring Data Cassandra Dependency

For Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>

For Gradle:

implementation 'org.springframework.boot:spring-boot-starter-data-cassandra'

2. Configure Cassandra in application.properties

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

Ensure that:

  • The keyspace exists (or use create_if_not_exists)
  • datacenter1 matches your Cassandra configuration

3. Create a Cassandra Entity

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

@Table("my_entity")
public class MyEntity {

    @PrimaryKey
    private String id;

    private String data;

    // Getters and Setters
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }

    public String getData() { return data; }
    public void setData(String data) { this.data = data; }
}

The @Table annotation maps the class to a Cassandra table.


4. Create a Repository Interface

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

public interface MyEntityRepository extends CassandraRepository<MyEntity, String> {
}

Spring Data provides out-of-the-box CRUD operations via this interface.


5. Save Data Using a Service

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

@Service
public class MyEntityService {

    @Autowired
    private MyEntityRepository repository;

    public void saveEntity(String id, String data) {
        MyEntity entity = new MyEntity();
        entity.setId(id);
        entity.setData(data);
        repository.save(entity);
    }
}

6. Example Controller (Optional)

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/entities")
public class MyEntityController {

    private final MyEntityService service;

    public MyEntityController(MyEntityService service) {
        this.service = service;
    }

    @PostMapping
    public String createEntity(@RequestParam String id, @RequestParam String data) {
        service.saveEntity(id, data);
        return "Saved successfully!";
    }
}

📦 Run the Application

  1. Start Cassandra.
  2. Run your Spring Boot application.
  3. Test with a tool like curl or Postman:
curl -X POST "http://localhost:8080/api/entities?id=1&data=HelloCassandra"

🔍 Conclusion

Integrating Cassandra with Spring Boot is simple and efficient using Spring Data Cassandra. With just a few lines of code, you can start storing and retrieving data in a scalable NoSQL environment. This setup is ideal for building microservices, analytics platforms, and IoT applications.


🏁 Next Steps

  • Add more fields and validation
  • Handle exceptions gracefully
  • Explore custom queries with @Query
  • Use ReactiveCassandraRepository for reactive applications
This article is inspired by real-world challenges we tackle in our projects. If you're looking for expert solutions or need a team to bring your idea to life,

Let's talk!

    Please fill your details, and we will contact you back

      Please fill your details, and we will contact you back