Get Started with Apache Cassandra

Interested in getting started with Cassandra? Follow these instructions.

STEP 1: GET CASSANDRA USING DOCKER

You'll need to have Docker Desktop for Mac, Docker Desktop for Windows, or similar software installed on your computer.

docker pull cassandra:latest

Apache Cassandra is also available as a tarball or package download.

STEP 2: START CASSANDRA

docker run --name cassandra cassandra

STEP 3: CREATE FILES

In the directory where you plan to run the next step, create these two files so that some data can be automatically inserted in the next step.

A cqlshrc file will log into the Cassandra database with the default superuser:

[authentication]
username = cassandra
password = cassandra

Create a scripts directory and change to that directory. The following data.cql file will create a keyspace, the layer at which Cassandra replicates its data, a table to hold the data, and insert some data:

# 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(toDate(now))));
INSERT INTO store.shopping_cart
(userid, item_count, last_update_timestamp)
VALUES (1234, 5, toTimeStamp(toDate(now))));

You should now have a cqlshrc file and <currentdir>/scripts/data.cql file.

STEP 4: RUN CQLSH TO INTERACT

Cassandra is a distributed database that can read and write data across multiple nodes with peer-to-peer replication. The Cassandra Query Language (CQL) is similar to SQL but suited for the JOINless structure of Cassandra. The CQL shell, or cqlsh, is one tool to use in interacting with the database.

docker run --rm -it -v /<currentdir>/scripts:/scripts \
-v /<currentdir/cqlshrc:/.cassandra/cqlshrc \
--env CQLSH_HOST=host.docker.internal --env CQLSH_PORT=9042 nuvo/docker-cqlsh

For this quickstart, this cqlsh docker image also loads some data automatically, so you can start running queries.

STEP 5: READ SOME DATA

SELECT * FROM store.shopping_cart;

STEP 6: WRITE SOME MORE DATA

INSERT (userid, item_count) VALUES (4567, 20) INTO store.shopping_cart;

STEP 7: TERMINATE CASSANDRA

docker rm cassandra

CONGRATULATIONS!

Hey, that wasn’t so hard, was it?

To learn more, we suggest the following next steps:

  • Read through the Cassandra Basics to learn main concepts and how Cassandra works at a high level.

  • To understand Cassandra in more detail, head over to the Docs.

  • Browse through the Case Studies to learn how other users in our worldwide community are getting value out of Cassandra.