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 Customer.java:

@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 application.properties, configure spring.data.cassandra:

– 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:
Use ALLOW FILTERING in Query:

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

Cassandra CQL Shell:

springboot-cassandra - select after saving

III. Sourcecode

SpringDataCassandra

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

    ????

    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

      Regards,
      JSA.

  2. hi,

    i tried the tutorial step by step.
    I feel like , i am alnmost there, but i get one error like this
    ——————————————-
    ***************************
    APPLICATION FAILED TO START
    ***************************

    Description:

    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.

    Action:

    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
    see:-
    COMPILATION ERROR :
    ————————————————————-
    com/javasampleapproach/cassandra/model/Customer.java:[3,50] package org.springframework.data.cassandra.mapping does not exist
    com/javasampleapproach/cassandra/model/Customer.java:[4,50] package org.springframework.data.cassandra.mapping does not exist
    com/javasampleapproach/cassandra/model/Customer.java:[6,2] cannot find symbol
    symbol: class Table
    com/javasampleapproach/cassandra/model/Customer.java:[9,10] cannot find symbol
    symbol: class PrimaryKey
    location: class com.javasampleapproach.cassandra.model.Customer
    4 errors
    ————————————————————-
    ————————————————————————
    BUILD FAILURE

  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: org.springframework.cassandra.support.exception.CassandraTruncateException: 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 org.springframework.cassandra.support.CassandraExceptionTranslator.translateExceptionIfPossible(CassandraExceptionTranslator.java:100) ~[spring-cql-1.5.3.RELEASE.jar:na]

    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504) ~[spring-data-commons-1.13.3.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489) ~[spring-data-commons-1.13.3.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[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(SpringDataCassandraApplication.java:31) [classes/:na]
    at com.javasampleapproach.cassandra.SpringDataCassandraApplication.run(SpringDataCassandraApplication.java:25) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [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 *

*