Keyspaces: Where Tables Live
A keyspace is a named container for tables. Every Cassandra table lives inside a keyspace. If you know MySQL, a keyspace is like a database there: a group of tables that belong together.
+------------------------------+
| Keyspace: shop |
+------------------------------+
| (products) (orders) |
| (customers) (payments) |
+------------------------------+
Creating a keyspace
When you create a keyspace, you also tell Cassandra how many copies of each row to keep. These are the replication settings:
CREATE KEYSPACE shop
WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': 3
};
'replication_factor': 3means every row is stored on 3 different nodes. If one node stops, two other nodes still have the data.SimpleStrategyis fine for learning and for a cluster in one data center. Real production clusters useNetworkTopologyStrategy, which can keep copies in several data centers.
Using a keyspace
To work inside a keyspace, switch to it with USE:
USE shop;
SELECT * FROM products;
Or put the keyspace name in front of the table name, with a dot:
SELECT * FROM shop.products;
Other keyspace commands
| Command | What it does |
|---|---|
ALTER KEYSPACE | Changes the replication settings |
DROP KEYSPACE | Deletes the keyspace and all of its tables |
DESCRIBE TABLES | Lists the tables in the current keyspace |
Your keyspace in this course
Every time you run code here, a new keyspace is created for you, and you are already inside it. So you never need CREATE KEYSPACE or USE in the exercises. You can create and read tables right away.
Often a table is already waiting in that keyspace, like the employees table from the last lesson. DESCRIBE TABLES shows it:
DESCRIBE TABLES;
Output:
employees
Answer a short question about keyspaces below.