✅ 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
- Create 2 keyspaces (
test_lab,prod_lab) - Set
test_labas active withUSE Create a table
studentwith:uuid,text,int,timestamp,decimal,boolean
- Run
DESCRIBEcommands to verify schema - Drop the
prod_labkeyspace
✅ Checklist
| Task | Done |
|---|---|
| Created keyspaces with different strategies | ✅ |
| Explored various data types | ✅ |
| Created a sample table using data types | ✅ |
Used DESCRIBE to inspect metadata |
✅ |
Tags:
Cassandra