Connect to Different Cassandra Keyspaces in Spring Boot

How to Connect to Multiple Cassandra Keyspaces in Your Spring Boot Project

Photo by Tom Rumble on Unsplash

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.

These were my other dependencies used for database operations implementation:

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!

I would love to change the world, but they won’t give me the source code | coding 👩🏼‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨