Cassandra is a highly scalable, distributed NoSQL database designed to handle large amounts of data across many commodity servers. It provides a flexible and tunable consistency model, allowing for strong or eventual consistency, depending on the use case. In this tutorial, we will learn how to install Cassandra on Ubuntu 22.
Table of Contents
Prerequisites
Before proceeding with the installation, make sure your system meets the following requirements:
Ubuntu 22 installed on your system
A user account with sudo privileges
Step 1: Install Java
Cassandra is written in Java, so we need to have Java installed on our system. We will be using OpenJDK 8. To install it, run the following command:
sudo apt update
sudo apt install openjdk-8-jdk
Step 2: Add Cassandra Repository
The next step is to add the Cassandra repository to the system. This will allow us to install Cassandra easily. Run the following command to add the repository:
echo "deb https://debian.cassandra.apache.org 41x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
Step 3: Import Repository Signing Key
To ensure the authenticity of the packages we are going to install, we need to import the repository signing key. To do so, run the following command:
curl https://downloads.apache.org/cassandra/KEYS | sudo apt-key add -
Step 4: Install Cassandra
With the repository added and the signing key imported, we are ready to install Cassandra. To do so, run the following command:
sudo apt update
sudo apt install cassandra
Step 5: Start Cassandra Service
After the installation is complete, start the Cassandra service using the following command:
sudo systemctl start cassandra
Step 6: Verify the Installation
To verify the installation, run the following command:
nodetool status
This will show the status of the Cassandra cluster and the nodes that are part of it.
To insert data into a Cassandra database, you can use the CQL (Cassandra Query Language) which is a SQL-like language designed specifically for Cassandra. Here are the steps to insert data into Cassandra:
Connect to the Cassandra cluster:
cqlsh
Create a keyspace:
CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor' : 3};
Use the keyspace:
USE mykeyspace;
Create a table:
CREATE TABLE IF NOT EXISTS mytable (id int PRIMARY KEY, name text, age int);
Insert data into the table:
INSERT INTO mytable (id, name, age) VALUES (1, 'John', 25);
Verify the data:
SELECT * FROM mytable;
This will return the data in the mytable table. You can insert multiple rows of data into the table by repeating the INSERT statement with different values.
Note: In this example, the keyspace mykeyspace has a replication factor of 3, meaning that data will be replicated across 3 nodes in the cluster for redundancy.
Conclusion
That’s it! With these simple steps, you have successfully installed Cassandra on Ubuntu 22. You can now start using Cassandra for your application’s data storage needs. If you encounter any issues during the installation process, don’t hesitate to ask for help.