Author: Prathamesh P. Kulkarni
What is NoSQL Database ?
A NoSQL database is a database that 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 API, 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 is used for the examples below.
How to install Cassandra on your system?
Hit the given link, you will be redirected to the Cassandra Download page http://cassandra.apache.org/download/
- Extract the zip file apache-cassandra-3.11.4 to the Local disk C
- Set the Environment variable : CASSANDRA_HOME as shown in the below screenshot.
- Similarly, set the path of cassandra.
- To run the 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 the python as shown in the screenshot below.
Step 2 :
- Run the Cassandra on your local system and make sure it is up and running without any problem.
- To run the 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 button
- Enter cassandra and hit enter 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, 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 keyspace dynamically.
While creating keyspace, You need to give the replication factor as well as replicationStrategyClass & pass it as java object. Below Screenshot will give you an idea 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 column and whether it is a primary key or not. If you want the column to be the primary key then set it as true.
In the postman set body like below mentioned screenshot.
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 also, 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 also you can try it on your own.
Request body of insert data
I have logged the response of select.
XML Code screenshot for reference