How to Connect to Multiple Cassandra Keyspaces in Your Spring Boot Project
A keyspace in Cassandra is like a database in RDBMS.
I had to connect to different keyspaces in my project because one was for keeping encryption key parts and the tables were being watched with a security tool for access, the other keyspace had our project specific data.
My project was implemented with Spring Boot 2.3.5 RELEASE.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
These were my other dependencies used for database operations implementation:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
Firstly, I created a base configuration file class since I will need these common properties to connect to the database for each keyspace:
For encryption keyspace, I created another configuration file. As you can see, I declared the repository and model folders’ paths in “basePackages” and “getEntityBasePackages”:
This one is for the other keyspace (notice how “basePackages” and “getEntityBasePackages” values differ here):
I implemented my model, repository and service classes just like I would in a single keyspace one.
Now we are good to go!
Happy Coding!