Build a Goodreads Clone with Spring Boot and Astra DB — Part 4
Author: Pieter Humphrey
This is the fourth post in a series that walks you through building a simple, highly available Spring Boot application that can handle millions of data records. In this post, we will set up an Astra DB instance and connect it to our Spring Boot application using Spring Data for Apache Cassandra®. To understand the data model for this app, have a look at part three. Also, check out part one and part two to get the full story.
It is now time to set up the necessary technologies to build our application. We are going to work backwards — from the database, to the midtier, and then we are going to set up our code.
Setting up your Astra DB database
For this project, we are going to be using a hosted Cassandra instance: DataStax Astra DB. You can get started by signing up for a free account. Then, log into Astra DB and follow DataStax’s official documentation to create an Astra DB database.
Use the following if you would like to follow the tutorial closely:
- A database name — we are using
- A keyspace name — we are using
A keyspace stores our group of tables, like a schema in a relational database. This helps keep related tables in a single place within your database. Once the status on your database has changed to “Active”, click on the database name.
In the dashboard for our
betterreads database (or whatever you may have named it), click on the CQL console tab. Cassandra uses CQL (Cassandra Query Language), which looks very similar to SQL. In the CQL Console on Astra DB, you can run CQL queries to interact with your database.
For example, using the
DESCRIBE command. This displays all the keyspaces in your database:
Many of these are system keyspaces. Use the
DESCRIBE command on the keyspace that we created when we spun up our instance:
Obviously there aren’t any tables in our keyspace yet; we just created it. We could create tables by using the
CREATE TABLE command in the CQL console, but we are going to do something even better using Spring Boot. Up next, we will create our Spring Boot project and add the necessary dependencies.
Create the Data Loader Spring Boot application
Now we are going to be working on the first part of our application development to load data into our database. We have an empty Cassandra instance that we created earlier. What we want to do is populate our database with the data from the OpenReads data dump.
Your Astra DB instance offers load data functionality, which allows you to load your own data set with CSV files. But the data dump provides us with a
works.txt file and
authors.txt, both tab delimited files with JSON data, not CSV. We are going to build an app that parses these files, gets the information in each line and then posts it to our Cassandra instance.
There are multiple ways we could do this, but since we already connected Spring Data Cassandra to our main web app, we can simply re-use some of that work to load our data as well. Normally, this is not a recommended practice for a production app. Using DS Bulk in most cases would sidestep the performance problems with this approach. Since data loading is not the focus of the tutorial, we are going to create a Spring Boot Data Loader app with Spring Data Cassandra, which we will use to post these data elements or records to our Cassandra instance on Astra DB.
Initialize a new Spring Boot project
How do we do this? We can go to start.spring.io and create a Spring Boot instance. Alternatively, you can use an IDE like Visual Studio Code, like we did. You can use whatever IDE you like, just make sure you have these equivalent extension packs installed:
- Spring Boot Extension Pack: This pack contains a bunch of extensions that allows you to create Spring Boot applications from Visual Studio Code.
- Java Extension Pack: A collection of popular extensions that can help write, test and debug Java applications.
You may need to restart your IDE to get your extension packs to work. Follow these steps to create a new project:
- Start a new Spring Boot Project. Ctrl+Shift+P brings up the command palette in VS code, start typing “Spring”. This should look something like:
Spring Initializer: Create a Maven Project…
- Choose a version of Spring Boot Version. You will be prompted to do this when you start a new Spring Boot project. It is a good idea to choose the latest stable version.
- Choose Java as the project language.
- Choose your coordinates. They are
io.javabrainsin our case.
- The artifact id is going to be
betterreads-data-loader. Remember that we are not building the main application at this point.
- Select a packing type: Jar.
- Choose Java Version 11.
- Select dependencies for our project.
- Spring Boot DevTools
- Spring Data for Apache Cassandra
- Click enter, and we will be asked where we want to save the project. We saved it to a code/betterreads folder we created. The project structure will be generated to this folder.
- Open the project in your IDE once prompted.
Our project contains
BetterReadsDataLoaderApplication.java, which is our main application file. Since we have Spring Data Cassandra dependencies, when we run this it is going to try to connect to a local Cassandra instance by default. We need to tell Spring Data Cassandra to connect to the Astra DB instance that we just created, which is the next step.
Connecting the Spring Boot application to Astra DB
Let’s start by getting the secure connect bundle from Astra DB.
Get the Secure Connect Bundle
Go to the database you created for the project. In the Astra UI, click on Connect Tab, and then the Java Section. This will tell you how to connect to your Astra application using Java.
Download the secure connect bundle. DataStax provides this to help us connect our Java applications to our Astra DB instances. Choose the region you selected for your database, then Click OK.
Copy the ZIP file to your application folder. Place it in the
src/main/resources directory. Now we can use a relative path to our secure connect bundle. (Anything in the
src/main/resources directory gets added to the classpath of a Java application.)
Configure Spring Data Cassandra
We need to provide three things to our application for it to connect to Cassandra:
- The address of where the database exists
- The credentials of how to connect (username and password for the database)
- The secure connect bundle (certificate/trust store)
The typical way to configure anything in a Spring Boot app is using
application.properties in the resources file. Spring Data Cassandra expects you to configure your connection to Cassandra by providing the coordinates of your instance in this file. Rename the file
application.yml, as it is less verbose than properties. Provide the
spring.data.cassandra configuration. This is the space where you can provide all of your Cassandra configurations. Hit Ctrl+Space to see options.
Create a username and password for your database
- Go to the Organization settings section in your Astra dashboard
- Select Role: Admin user
- Click “Generate Token”
- Copy Client Id, Client Secret, and Token
Go to the Astra DB documentation for more in-depth instructions on how to do this.
Add your configurations
Next, add your Cassandra configurations to the application.yml file.
- Make the
keyspace-name: main (or whatever name you used).
- Make client id the
username, and client secret the
- Keep the Token on hand for later.
- Use the schema-action property to specify what the action should be. Choose
- Set request
timeoutto 10s (recommended if using a development environment and your Cassandra instance is on the cloud).
- Also set
This is our Spring Data Cassandra configuration. We have now configured the credentials, the keyspace name, and some of the request and timeout parameters.
At this point we can specify the connection bundle. Let’s create a DataStax-specific property to expose to our application, so that it uses this ZIP file to connect to our Astra DB instance.
We also need to provide some location-specific information for our Astra DB instance.
Grab these values from your Astra DB database dashboard. Use the token value you saved from earlier. See an example of an application.yml file here.
Connect to Cassandra
These are the properties that you need to have for your application to connect to Astra DB. One more thing, we want to expose our secure-connect-bundle property as a Java configuration. To do this, create a new Java class in the
Then expose the
datastax-astra property in your new class. When you expose this as a property, the application will know to pick up the
secure-connect-bundle.zip file and use it. Create the getters and setters and you are good to go.
Also add the
@EnableConfigurationProperties annotation to your main file, and give it the class where we have this configuration property (
Next, paste code in the main class that exposes the
CqlSessionBuilderCustomizer Bean. This can build a new
CqlSession using the
secure-connect-bundle. See the full code you should have for the main class here.
We are giving the Spring Data Cassandra infrastructure the ability to connect to our Astra DB instance by using the secure bundle.
Now when you run your application, it will no longer try to connect to a local instance, which is the default behavior, but to your Astra DB instance.
Congratulations! You now have a Spring Boot application that connects Spring Data Cassandra to your Cassandra instance in the cloud.
Now what we have left to do is get this application to the point where it takes our data one line after another and posts them as rows in a table in our Cassandra instance. This all starts with creating a new database entity class — a model class.
Spring Data cassandra will create the tables and schema in the keyspace for whatever we have created in the Java class.
That’s what we will be covering in our next post. In the meantime, you can check out the Github repository with the full code for this project.