Create a keyspace
Keyspaces are the first schema objects you create in Cassandra-based databases.
Keyspaces are containers for all lower schema object, including tables, user-defined types, materialized views, and indexes.
Use the CREATE KEYSPACE command to create a keyspace.
Prerequisites
-
Determine the number of datacenters and replication strategy.
-
Determine the required replication factor.
-
Start
cqlsh.
|
Keyspace and table name conventions
The name of a keyspace or a table is a string of alphanumeric characters and underscores, but it must begin with a letter.
If case must be maintained, the name must be encased in double quotes, such as Since tables are defined within a keyspace, you can either use the keyspace as part of the table creation command, or create a table in the current keyspace.
To specify the keyspace as part of a table name, use the keyspace name, a period ( |
Create a simple keyspace
Start cqlsh, and then create a keyspace.
You must specify the keyspace name, replication class, and replication factor.
CREATE KEYSPACE IF NOT EXISTS cycling
WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
};
Create an NTS keyspace
Start cqlsh, and then create a keyspace.
You must specify the keyspace name, replication class, and replication factor.
CREATE KEYSPACE cycling
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'dc1' : 1
};
Create a keyspace with datacenters
-
Use
nodetool statusto get the names of the datacenters for the keyspace:nodetool statusResultDatacenter: datacenter1 ================ Status=Up/Down |/ State=Normal/Leaving/Joining/Moving -- Address Load Tokens Owns Host ID Rack UN 10.200.181.134 4.97 MiB 1 ? c9027497-011c-4390-ae59-e4ed1ac794cc rack1 UN 10.200.181.135 5.16 MiB 1 ? af2af8ec-5fa2-4a04-ac9c-c75669d8d3a0 rack1In the next step, the datacenter name must match exactly because datacenter names are case-sensitive.
-
Start
cqlsh, and then create a keyspace. You must specify the keyspace name, the keyspace replication class, and a replication factor for each datacenter.CREATE KEYSPACE IF NOT EXISTS cycling WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1': '3', 'datacenter2': '3' };
Use a keyspace
After you create a keyspace, set it as your active keyspace with the USE KEYSPACE command.
Once set, you can run CQL commands against it without specifying the keyspace name in every command.
USE cycling;