DEV Community

João Victor Martins
João Victor Martins

Posted on

Integrating Scylla DB with Quarkus

Hello, everyone!! In the previous post, we talk about Scylla DB and some characteristics of the database. For those who have not seen it yet, this is the link https://dev.to/j_a_o_v_c_t_r/working-with-scylla-database-3al9. Today I intend to integrate the database with an application Java and to help us I decided to use Java + Quarkus. Quarkus is a Kubernetes Native Java stack tailored for OpenJDK HotSpot and GraalVM, crafted from the best-of-breed Java libraries and standards. It is known as Supersonic Subatomic java because it is fast and small. To those interested, the link will be at the final of the post. Ok, now that we know our goal, let's start!!

Creating the application

The first step is to create the application and for that, Quarkus gives us a simple interface that we can access at this link https://code.quarkus.io/.

Alt Text

On this page we put information like groupId, artifactId, build tools, and select which dependencies we will use.

Alt Text

Let's know about the selected dependencies

  • Datastack Apache Cassandra client: Driver to connect to Apache Cassandra databases. How we saw, Scylla DB is based on Cassandra, so we can use the same client.
  • RESTEasy JSON-B: JSON-B serialization support for RESTEasy. We will use endpoints to interact with our application
  • RESTEasy Mutiny: Mutiny support for RESTEasy server. If we want to call async resources (We don't use them in this example).
  • SmallRye Health: Monitor service health.
  • Micrometer Registry Prometheus: Enable Prometheus support for micrometer, so with this dependency, we can collect metrics for our application.

Ok, now we can generate our application in the Generate your application button.

Let's import the application in our IDE.

Quarkus gives us a GET resource for testing, but we are not going to use it so we can delete it.

Alt Text

If you want test, is enough execute mvn package quarkus:dev command, wait application is up and call http://localhost:8080/hello-reasteasy

Now we can create our entity. Entity class is a model for objects that we will work on. This object will be Car. In the name of the class will use the annotation @Entity. The name of the class should be the same as the table of database. The object will have the fields id, brand, color, and model. How we saw in the previous post, the id field is a primary key, so we will use the annotation @PartitionKey.

In the next post, I will talk about Scylla Primary Key and the differences in relation the relational databases

Alt Text

How we can see, the class does not compile, because we need to add some dependencies in our pom.xml. This dependencies are:

  • com.datastax.oss:java-driver-core: A modern, feature-rich, and highly tunable Java client library for Apache Cassandra® (2.1+) and DataStax Enterprise (4.7+), and DataStax Astra, using exclusively Cassandra's binary protocol and Cassandra Query Language (CQL) v3.

  • com.datastax.oss:java-driver-mapper-runtime: The mapper generates the boilerplate to execute queries and convert the results into application-level objects.

The dependencies can be downloaded in https://mvnrepository.com/

And we need to change the original maven-compiler-plugin in pom.xml for the below.

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>com.datastax.oss.quarkus</groupId>
                            <artifactId>cassandra-quarkus-mapper-processor</artifactId>
                            <version>1.0.1</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
Enter fullscreen mode Exit fullscreen mode

This is necessary because Quarkus need know the annotations in compile time for injecting the resources like DAO and others. We'll see that soon.

After including the dependencies in pom.xml, we can import annotations and include constructors and getters/setters.

Alt Text

rest of the methods omitted!

The next step is to create the Data Access Object or more commonly DAO. Is enough for this post two methods, one select method and one insert method.

Alt Text

The annotation @Dao comes from the DataStax Object Mapper, which will also automatically generate an implementation of this interface for us. @Insert and @Select generate an implementation of methods. Note also the special return type of the findAll method, PagingIterable: it’s the base type of result sets returned by the driver.

Now let's create a mapper that is responsible for constructing instances of DAOs. In this case, will create an instance of CarDAO. The name will be CarMapper.

Alt Text

To interact with the saved information or save new information in the database, we will create a REST endpoint and for that will create a CarResource class. For our example, we will need a POST and a GET method.

Alt Text

In relation to the image above, let's talk about some details. The resource for access the application will be cars. We should send JSON for the application and consume JSON of the application. To save or retrieve information, we need access to the database, so for that, we inject the CarDAO in CarResource.

The last step will be to configure the information of the database in the application. For that, we will use application.properties.

Alt Text

And we can see the information above when we up our node Scylla.

Alt Text

Let's start the application using mvn package quarkus:dev command

Alt Text

And to use our browser to do a request for the application

Alt Text

Saving information

Alt Text

New saved information

Alt Text

Tested requests, our application is complete and functional. We can now explore more features.

The reason we put in our pom.xml the quarkus-smallrye-health dependency is automatically add a readiness health check to validate the connection to the Scylla cluster. We can access http://localhost:8080/q/health/ready to know if our connection is healthy or not.

Alt Text

Now I will pause the docker that is running ScyllaDB

Alt Text

And test connection again

Alt Text

We can see the metrics of Scylla session and about individual Scylla nodes and for this is enough add a property quarkus.cassandra.metrics.enabled=true in our application.properties what we already did. To see metrics we can access http://localhost:8080/metrics

Alt Text

Conclusion

My intention with this post is to show how we can integrate an application Java with Scylla DB and how easy that is using Quarkus. Some details can explorer yet, like primary key, but we can see this in the next post. I hope that you liked it and any doubt, critics, or suggestions tell me. Will be a pleasure to help you.

References
https://quarkus.io/
https://quarkus.io/guides/cassandra

Top comments (4)

Collapse
 
marcuspaulo profile image
Marcus Paulo

Excellent post, congratulations João.

Collapse
 
j_a_o_v_c_t_r profile image
João Victor Martins

Thank's ma friend!!

Collapse
 
paulo_iggor profile image
Paulo Igor

Incredible!!!

Collapse
 
j_a_o_v_c_t_r profile image
João Victor Martins

Thank's!!