Integrating Scylla DB with Quarkus
Tags: java, quarkus, scylladb, database
Published: 2021-05-26 18:18:09

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0ptpmkx367vihz2crwy.png)
 
On this page we put information like *groupId*, *artifactId*, build tools, and select which dependencies we will use.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avr8dypohiqj95jtogzp.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3p8bgri3rqgxqr78h24n.png)
 
> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuatj8eqk4hwec6yqn46.png)

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>
```  

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9o2fec5mpu49f12xk4h.png)

*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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kok3sscjwqrdgbnva36p.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x3ga94wvvy61312s2gf.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtp90fsna29s34j0tpif.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iody4zjp0tm1dov2f5v3.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9160elcr75s6p500selq.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/carcsm794k55j0j8g192.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jr0t5l3t1xm9kncebl8n.png)

Saving information

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cypr8u3xy47mki7n22fr.png)

New saved information

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0rr7lgu4m10vpflmnjg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61mxej9evkksd91s8bmi.png)

Now I will pause the docker that is running ScyllaDB

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6kiq51j317bxy541i1aa.png)

And test connection again

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y78r8r4oi3dcawq7x2xz.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7s48ns07ledu4m21fxb.png)

# 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