Cassandra Quickstart

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.

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

  1. docker pull cassandra:latest

STEP 2: START CASSANDRA

A Docker network allows us to access the container’s ports without exposing them on the host.

  1. docker network create cassandra
  2. docker run --rm -d --name cassandra --hostname cassandra --network cassandra cassandra

STEP 3: CREATE FILES

The Cassandra Query Language (CQL) is very similar to SQL but suited for the JOINless structure of Cassandra.

Create a file named data.cql and paste the following CQL script in it. This script will create a keyspace, the layer at which Cassandra replicates its data, a table to hold the data, and insert some data into that table:

  1. CREATE KEYSPACE IF NOT EXISTS store WITH REPLICATION =
  2. { 'class' : 'SimpleStrategy',
  3. 'replication_factor' : '1'
  4. };
  5. CREATE TABLE IF NOT EXISTS store.shopping_cart (
  6. userid text PRIMARY KEY,
  7. item_count int,
  8. last_update_timestamp timestamp
  9. );
  10. INSERT INTO store.shopping_cart
  11. (userid, item_count, last_update_timestamp)
  12. VALUES ('9876', 2, toTimeStamp(now()));
  13. INSERT INTO store.shopping_cart
  14. (userid, item_count, last_update_timestamp)
  15. VALUES ('1234', 5, toTimeStamp(now()));

STEP 4: LOAD DATA WITH CQLSH

The CQL shell, or cqlsh, is one tool to use in interacting with the database. We’ll use it to load some data into the database using the script you just saved.

  1. docker run --rm --network cassandra \
  2. -v "$(pwd)/data.cql:/scripts/data.cql" \
  3. -e CQLSH_HOST=cassandra -e CQLSH_PORT=9042 \
  4. -e CQLVERSION=3.4.6 nuvo/docker-cqlsh

The cassandra server itself (the first docker run command you ran) takes a few seconds to start up. The above command will throw an error if the server hasn’t finished its init sequence yet, so give it a few seconds to spin up.

STEP 5: INTERACTIVE CQLSH

Much like an SQL shell, you can also of course use cqlsh to run CQL commands interactively.

  1. docker run --rm -it --network \
  2. cassandra nuvo/docker-cqlsh cqlsh cassandra \
  3. 9042 --cqlversion='3.4.5'

This should get you a prompt like so:

  1. Connected to Test Cluster at cassandra:9042.
  2. [cqlsh 5.0.1 | Cassandra 4.0.4 | CQL spec 3.4.5 | Native protocol v5]
  3. Use HELP for help.
  4. cqlsh>

STEP 6: READ SOME DATA

  1. SELECT * FROM store.shopping_cart;

STEP 7: WRITE SOME MORE DATA

  1. INSERT INTO store.shopping_cart
  2. (userid, item_count)
  3. VALUES ('4567', 20);

STEP 8: CLEAN UP

  1. docker kill cassandra
  2. docker network 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.

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