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 customer table for javasampleapproach keyspace:

– Create an index on firstname column:

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

@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.

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

5. Create a test client

– Use repository: 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.

6. Run & check result

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

-> Solution:

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

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. Hi saikiran,

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

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


  1. 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

  2. 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

  3. 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

  4. 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 *