How to Install Apache Cassandra: A Step-by-Step Guide

Learn how to quickly set up and run your own Apache Cassandra database using Docker with this easy-to-follow tutorial.

Prometheus

Welcome to the World of NoSQL Databases

Linux | macOS | Windows

What is Apache Cassandra?

Apache Cassandra is a highly popular, free, and open-source NoSQL database. Unlike traditional relational databases that use tables with strict rules, Cassandra is designed to handle massive amounts of data across many different servers. It is built to be extremely fast and reliable, making it a top choice for big companies that cannot afford for their websites or apps to go offline.

The secret to Cassandra's power is its "distributed" design. Instead of keeping all your data in one central location, Cassandra spreads the data out across multiple machines, known as a cluster. Because there is no single master computer controlling everything, there is no single point of failure. If one machine breaks down, the other machines step in and keep the database running smoothly without losing any information.

Finally, Cassandra is built for amazing scalability. If your app becomes popular and you need to store more data, you can simply add more computers to the Cassandra cluster, and it will handle the extra workload automatically. To interact with the database, you use a language called Cassandra Query Language (CQL), which looks and feels very similar to traditional SQL, making it easy for beginners to learn and use.

Prerequisites

  • Docker Installed: You must have Docker Desktop installed and running on your computer (available for Mac, Windows, or Linux).
  • Command Line Knowledge: You need a basic understanding of how to use your computer's terminal or command prompt.
  • Text Editor: A simple text editor (like Notepad, TextEdit, or VS Code) to create a database script file.
  • Internet Connection: Required to download the necessary Docker images from Docker Hub.

Step-by-Step Installation and Setup

1

Get Cassandra Using Docker

First, open your terminal or command prompt. We will download the latest official Apache Cassandra software image from Docker.
BASH
docker pull cassandra:latest
2

Start Cassandra

Next, we need to create a Docker network. This allows us to safely connect to the database. Run these two commands to create the network and start the Cassandra server.
BASH
docker network create cassandra

docker run --rm -d --name cassandra --hostname cassandra --network cassandra cassandra
(Note: It takes a few seconds for the database to fully start up. Give it a moment before moving to the next steps.)
3

Create the Database File

Now, create a new file on your computer and name it data.cql . Open it in your text editor, paste the following code into it, and save the file. This script creates a storage area (keyspace), creates a table for a shopping cart, and adds two items to it.
SQL
-- Create a keyspace
CREATE KEYSPACE IF NOT EXISTS store WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };

-- Create a table
CREATE TABLE IF NOT EXISTS store.shopping_cart (
userid text PRIMARY KEY,
item_count int,
last_update_timestamp timestamp
);

-- Insert some data
INSERT INTO store.shopping_cart
(userid, item_count, last_update_timestamp)
VALUES ('9876', 2, toTimeStamp(now()));

INSERT INTO store.shopping_cart
(userid, item_count, last_update_timestamp)
VALUES ('1234', 5, toTimeStamp(now()));
4

Load Data into Cassandra

Use the following command to load the data.cql file you just created into your running database. This uses a tool called cqlsh (Cassandra Query Language Shell).
BASH
docker run --rm --network cassandra -v "$(pwd)/data.cql:/scripts/data.cql" -e CQLSH_HOST=cassandra -e CQLSH_PORT=9042 -e CQLVERSION=3.4.7 nuvo/docker-cqlsh
5

Access the Interactive Command Shell

To talk directly to the database and write your own commands, open the interactive shell by running this command:
BASH
docker run --rm -it --network cassandra nuvo/docker-cqlsh cqlsh cassandra 9042 --cqlversion='3.4.7'
You will see a cqlsh> prompt appear on your screen.
6

Read Your Data

Inside the interactive shell, type the following command to view the data you inserted earlier. Press Enter to run it.
SQL
SELECT * FROM store.shopping_cart;
7

Write More Data

You can also add new data manually while inside the shell. Try running this command:
SQL
INSERT INTO store.shopping_cart (userid, item_count) VALUES ('4567', 20);
8

Clean Up

Once you are finished exploring, you can safely turn off the database and remove the network by typing exit to leave the shell, and then running these commands in your normal terminal:
Bash
docker kill cassandra

docker network rm cassandra

Discover CTCservers Dedicated Server Locations

CTCservers servers are available around the world, providing diverse options for hosting websites. Each region offers unique advantages, making it easier to choose a location that best suits your specific hosting needs.