Integrating NoSQL Database With Mule 4 (OOTB Cassandra Connector)
Learn how to add a Cassandra database connector to the AnyPoint Studio in Mule 4 in this integration tutorial.
Join the DZone community and get the full member experience.Join For Free
What Is a NoSQL Database?
A NoSQL database provides a mechanism to store and retrieve data other than the tabular relations used in relational databases. These databases are schema-free, support easy replication, have simple APIs, are eventually consistent, and can handle huge amounts of data.
The primary objective of a NoSQL database is to have:
- Simple design
- Horizontal scaling
- Fine control over availability
What Is Cassandra?
Apache Cassandra is an open-source, distributed, and decentralized/distributed storage system (database) for managing very large amounts of structured data spread out across the world. It provides highly available service with no single point of failure.
The Apache Cassandra database is the right choice when you need scalability and high availability without compromising performance.
Mulesoft has a connector for Cassandra. The Cassandra connector provides connectivity between the Cassandra database system and REST API.
First of all, you will need to create a Mule project. Anypoint Studio and Mule Runtime 4.2.0 EE are used for the examples below.
How to Install Cassandra on Your System
Hit this link to go to the Cassandra Download page: http://cassandra.apache.org/download/.
- Extract the zip file apache-cassandra-3.11.4 to Local disk C
- Set the Environment variable CASSANDRA_HOME as shown in the below screenshot.
- Similarly, set the path of Cassandra.
- To run Cassandra, we also need to have JDK as well as Python.
Link to download Python is: https://www.python.org/download/releases/2.7/.
- Set the Environment variable for Python as shown in the screenshot below.
- Run the Cassandra on your local system and make sure it is up and running without any problem.
- To run Cassandra, copy the path of the Cassandra folder up to bin and change the working directory to the above copied path.
- cd C:\apache-cassandra-3.11.4\bin and hit Enter.
- Enter Cassandra and hit Enter. Now, your Cassandra is starting.
Open another CMD and enter the command as cqlsh and proceed.
Cassandra is up and running successfully.
How to Add Cassandra DB Connector to the AnyPoint Studio
Go to Add Modules and search for Cassandra. Add it and finish the task.
Setup & Configuration
Select the Cassandra connector and proceed. You will be able to see the Cassandra connector in Mule Palette.
There are two ways to configure the Cassandra:
- Using your localhost
- Using Cluster Configuration
I have used the localhost. The default port for cassandra is 9042.
Drag a Cassandra component for Create Keyspace onto the canvas.
I have kept the keyspace as empty as I want to create the keyspace dynamically.
While creating the keyspace, you need to give the replication factor as well as replicationStrategyClass and pass it as a Java object. The below screenshot will give you an idea of how to pass the payload.
You can run the application and hit the created API with the proper request body.
You can see the keyspace has been created. In order to cross verify you can goto cmd and get the list of all keyspaces, you should see your keyspace.
How to Create Table
We have the component in the Cassandra called Create Table. Drag it from the Mule palette and create a new flow.
Set the payload in the transform message as shown in the below screenshot.
Here, you need to specify columns as well as the datatype of the column and whether it is a primary key. If you want the column to be the primary key, then set it as true.
In the postman, set the body like the below screenshot.
As you can see, right now, there is no table in the keyspace.
After hitting the API, Your table will get created with the name cassandra_demo.
The table is created with the given table name. Now, it’s time to insert data into the table.
How to Insert Data
For this, Mulesoft has provided us with an Insert Data component under the Cassandra Connector. So, you just have to drag that component and create a new flow with the different endpoint.
The Cassandra Module uses Java maps as the mechanism to define how data is inserted in a Cassandra key space.
Here, I am using batch step. The reason I am using batch step is we won’t be able to insert multiple records in the single hit and similarly to get the response back I am using VM publish and consume as batch job works Asynchronously.
I have used a select component in the same flow. You can go through the XML screenshot to get a better idea. Also, there are other components like delete, update, etc. and you can try it on your own.
Request Body of Insert Data
I have logged the response of select.
XML code screenshot for reference:
Opinions expressed by DZone contributors are their own.