How to start Spring Data Cassandra with SpringBoot

Apache Cassandra database is a NoSql solution for scalability & high availability. In the tutorial, JavaSampleApproach will show you how to start Spring Data Cassandra by sample code.

Related article:
How to start SpringBoot ElasticSearch using Spring Data
Couchbase – How to create a Spring Jpa Couchbase application with SpringBoot
SpringData Reactive Cassandra Repositories | SpringBoot
Angular 4 + Spring Boot + Cassandra CRUD example
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.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE
– DataStax Community Edition for Apache Cassandra: v3.9.0

II. Practice

Step to do:
– Set up Apache Cassandra
– Create SpringBoot project
– Create a mapping model
– Create a Cassandra repository
– Create a test client
– Run & check result

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:

cassandra start sever

Open Cassandra CQL Shell to create Cassandra table:

– Create Cassandra keyspace with name javasampleapproach:

create keyspace javasampleapproach with replication={'class':'SimpleStrategy', 'replication_factor':1};

– Create customer table for javasampleapproach keyspace:

use javasampleapproach;

CREATE TABLE customer(
   id int PRIMARY KEY,
   firstname text,
   lastname text,
   age int

– Create an index on firstname column:

CREATE INDEX ON javasampleapproach.customer (firstname);

springboot-cassandra create table

2. Create SpringBoot project

– Using SpringToolSuite, create a SpringBoot project. Then add data-cassandra dependency:


3. Create a mapping model

– Create

package com.javasampleapproach.cassandra.model;


public class Customer {
	private int id;
	private String firstname;
	private String lastname;
	private int age;
	public Customer(){}
	public Customer(int id, String firstname, String lastname, int age){ = id;
		this.firstname = firstname;
		this.lastname = lastname;
		this.age = age;
	public void setId(int id){ = id;
	public int getId(){
	public void setFirstname(String firstname){
		this.firstname = firstname;
	public String getFirstname(){
		return this.firstname;
	public void setLastname(String lastname){
		this.lastname = lastname;
	public String getLastname(){
		return this.lastname;
	public void setAge(int age){
		this.age = age;
	public int getAge(){
		return this.age;
	public String toString() {
		return String.format("Customer[id=%d, firstName='%s', lastName='%s', age=%d]",,
				this.firstname, this.lastname, this.age);

@Table: identifies a domain object to be persisted to Cassandra as a table.
@PrimaryKey: identifies the primary key field of the entity.

4. Create a Cassandra repository

– Open, configure

– Spring Data supports basic operations for Cassandra. But more limited than JPA repositories, so we need use @Query.

package com.javasampleapproach.cassandra.repository;

import java.util.List;


import com.javasampleapproach.cassandra.model.Customer;

public interface CustomerRepository extends CrudRepository {
	@Query(value="SELECT * FROM customer WHERE firstname=?0")
	public List findByFirstname(String firstname);

	@Query("SELECT * FROM customer WHERE age > ?0")
	public List findCustomerHasAgeGreaterThan(int age);

*Update for Spring Boot 2 (Spring Data Cassandra 2) with new @AllowFiltering annotation:

public interface CustomerRepository extends CassandraRepository {

	public List findByFirstname(String firstname);

	public List findByAgeGreaterThan(int age);
5. Create a test client

– Use repository: CustomerRepository

CustomerRepository customerRepository;

Implement 3 functions:
clearData() is used to empty customer table.
saveData() is used to persist Customer entities to Cassandra.
lookup() is used to load all records and show all on console.

package com.javasampleapproach.cassandra;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.javasampleapproach.cassandra.model.Customer;
import com.javasampleapproach.cassandra.repository.CustomerRepository;

public class SpringDataCassandraApplication implements CommandLineRunner{
    CustomerRepository customerRepository;

	public static void main(String[] args) {, args);

	public void run(String... args) throws Exception {
	public void clearData(){
	public void saveData(){
		System.out.println("===================Save Customers to Cassandra===================");
		Customer cust_1 = new Customer(1, "Peter", "Smith", 20);
        Customer cust_2 = new Customer(2, "Mary", "Taylor", 25);
        Customer cust_3 = new Customer(3, "Peter", "Brown", 30);
        Customer cust_4 = new Customer(4, "Lauren", "Taylor", 20);
        Customer cust_5 = new Customer(5, "Lauren", "Flores", 45);
        Customer cust_6 = new Customer(6, "Peter", "Williams", 20);
        // save customers to ElasticSearch;;;;;;
	public void lookup(){
		System.out.println("===================Lookup Customers from Cassandra by Firstname===================");
		List peters = customerRepository.findByFirstname("Peter");

		System.out.println("===================Lookup Customers from Cassandra by Age===================");
		List custsAgeGreaterThan25 = customerRepository.findCustomerHasAgeGreaterThan(25);

6. Run & check result

– Build & Run the project with SpringBoot App mode.
-> Meet an InvalidQueryException exception when executing lookup function – findCustomerHasAgeGreaterThan(int age):

Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING
	at com.datastax.driver.core.Responses$Error.asException( ~[cassandra-driver-core-3.1.4.jar:na]
	at com.datastax.driver.core.DefaultResultSetFuture.onSet( ~[cassandra-driver-core-3.1.4.jar:na]

-> Solution:

public interface CustomerRepository extends CrudRepository {
	@Query(value="SELECT * FROM customer WHERE firstname=?0")
	public List findByFirstname(String firstname);

	@Query("SELECT * FROM customer WHERE age > ?0 ALLOW FILTERING")
	public List findCustomerHasAgeGreaterThan(int age);

– Build & Run the project with SpringBoot App mode again.

===================Save Customers to Cassandra===================
===================Lookup Customers from Cassandra by Firstname===================
Customer[id=1, firstName='Peter', lastName='Smith', age=20]
Customer[id=6, firstName='Peter', lastName='Williams', age=20]
Customer[id=3, firstName='Peter', lastName='Brown', age=30]
===================Lookup Customers from Cassandra by Age===================
Customer[id=5, firstName='Lauren', lastName='Flores', age=45]
Customer[id=3, firstName='Peter', lastName='Brown', age=30]

Cassandra CQL Shell:

springboot-cassandra - select after saving

III. Sourcecode


By grokonez | May 1, 2017.

Last updated on March 19, 2018.

Related Posts

8 thoughts on “How to start Spring Data Cassandra with SpringBoot”

  1. Can we not use a @Query with update command in it like below

    @Query(value = "UPDATE AreacodeVO SET area ='hy' WHERE id = 1;")


    1. Hi saikiran,

      You can update data by finding the row by id, change data, then save new item:

      public class CustomerController {
      	public ResponseEntity updateCustomer(@PathVariable("id") UUID id, @RequestBody Customer customer) {
      		System.out.println("Update Customer with ID = " + id + "...");
      		Customer customerData = customerRepository.findOne("id", id));
      		if (customerData == null) {
      			return new ResponseEntity<>(HttpStatus.NOT_FOUND);
      		Customer updatedcustomer =;
      		return new ResponseEntity<>(updatedcustomer, HttpStatus.OK);

      For more details, please visit:
      Angular 4 + Spring Boot + Cassandra CRUD example


  2. hi,

    i tried the tutorial step by step.
    I feel like , i am alnmost there, but i get one error like this


    Field patient_info_Repo in com.datastax.cassandra.cassandradrivercore.SpringCassandraApplication required a bean of type ‘com.datastax.cassandra.cassandradrivercore.patient_info_repository’ that could not be found.


    Consider defining a bean of type ‘com.datastax.cassandra.cassandradrivercore.patient_info_repository’ in your configuration.

    My guess is the @Autowired is unable to find its corresponding bean.

    Please help me get rid of this problem.

    Thanks in advance

  3. there is an error in your code
    com/javasampleapproach/cassandra/model/[3,50] package does not exist
    com/javasampleapproach/cassandra/model/[4,50] package does not exist
    com/javasampleapproach/cassandra/model/[6,2] cannot find symbol
    symbol: class Table
    com/javasampleapproach/cassandra/model/[9,10] cannot find symbol
    symbol: class PrimaryKey
    location: class com.javasampleapproach.cassandra.model.Customer
    4 errors

  4. Hi , new to Cassandra , when I executed this source code aganist a three node cassandra cluster it is throwing below error , how to fix this , please help me , my keyspace replication factor is 2.

    Caused by: Error during truncate: Cannot achieve consistency level ALL; nested exception is com.datastax.driver.core.exceptions.TruncateException: Error during truncate: Cannot achieve consistency level ALL
    at ~[spring-cql-1.5.3.RELEASE.jar:na]

    at java.lang.reflect.Method.invoke( ~[na:1.8.0_181]
    at$QueryExecutorMethodInterceptor.executeMethodOn( ~[spring-data-commons-1.13.3.RELEASE.jar:na]
    at$QueryExecutorMethodInterceptor.doInvoke( ~[spring-data-commons-1.13.3.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed( ~[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke( ~[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at com.sun.proxy.$Proxy51.deleteAll(Unknown Source) ~[na:na]
    at com.javasampleapproach.cassandra.SpringDataCassandraApplication.clearData( [classes/:na]
    at [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner( [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    … 6 common frames omitted

  5. i need help…”error”: “Internal Server Error”,
    “message”: “Couldn’t find PersistentEntity for type class java.lang.Integer!”,

    how to solve this error…

Got Something To Say:

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