Day 2: Cassandra Lab Practice – Keyspace & Data Types


Day 2: Keyspaces and Data Types

🎯 Objective:

Learn about keyspaces (Cassandra’s equivalent to databases) and explore the different data types available in Cassandra.


📘 1. What is a Keyspace?

  • A keyspace defines:

    • Replication strategy
    • Replication factor
    • Durable writes

🔑 Key terms:

  • Replication Factor: Number of nodes that will store a copy of the same data.
  • SimpleStrategy: Best for single-node setups.
  • NetworkTopologyStrategy: Best for production and multi-datacenter environments.

🧪 2. Create a Keyspace

➤ Using SimpleStrategy:

CREATE KEYSPACE test_lab
WITH REPLICATION = {
  'class' : 'SimpleStrategy',
  'replication_factor' : 1
};

➤ Using NetworkTopologyStrategy:

CREATE KEYSPACE prod_lab
WITH REPLICATION = {
  'class' : 'NetworkTopologyStrategy',
  'datacenter1' : 3
};

📌 3. Use and Drop a Keyspace

USE test_lab;

-- To delete a keyspace
DROP KEYSPACE test_lab;

📦 4. Data Types in Cassandra

Cassandra supports these main data types:

Category Types
Textual text, varchar, ascii
Numeric int, bigint, float, double, decimal, varint
Boolean boolean
Temporal timestamp, date, time, duration
UUIDs uuid, timeuuid
Binary blob
Collections list, set, map
IP Address inet

🧑‍💻 5. Practice: Create Tables with Different Data Types

CREATE TABLE test_lab.student (
  id uuid PRIMARY KEY,
  name text,
  age int,
  admission_date timestamp,
  fees decimal,
  is_active boolean
);

📂 6. View Keyspaces and Tables

DESCRIBE KEYSPACES;
DESCRIBE KEYSPACE test_lab;
DESCRIBE TABLE test_lab.student;

🧪 Day 2 Lab Tasks

  1. Create 2 keyspaces (test_lab, prod_lab)
  2. Set test_lab as active with USE
  3. Create a table student with:

    • uuid, text, int, timestamp, decimal, boolean
  4. Run DESCRIBE commands to verify schema
  5. Drop the prod_lab keyspace

Checklist

Task Done
Created keyspaces with different strategies
Explored various data types
Created a sample table using data types
Used DESCRIBE to inspect metadata