Create a keyspace
In Cassandra and DataStax Enterprise, the first schema to create is a keyspace using CQL.
Prerequisites
-
Determine the number of datacenters and replication strategy.
-
Determine the required replication factor.
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 ( |
Simple keyspace
-
Start
cqlsh
and create a keyspace.CREATE KEYSPACE IF NOT EXISTS cycling WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
The keyspace name, replication class, and replication factor must all be specified.
NTS keyspace
-
Start
cqlsh
and create a keyspace.CREATE KEYSPACE cycling WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'dc1' : 1 };
The keyspace name, replication class, and replication factor must all be specified.
Keyspace with datacenters
-
Verify the datacenter name or names using
nodetool status
.nodetool status
Datacenter: 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 rack1
+ Ensure that the datacenter name matches exactly in the next step, as the name is case-sensitive.
-
Start cqlsh and create a keyspace.
CREATE KEYSPACE IF NOT EXISTS cycling WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1': '3', 'datacenter2': '3' };
The keyspace name, replication class, and replication factor must all be specified. The replication factor is set for each datacenter.
Use created keyspace
Switch to the keyspace:
USE cycling;
See also: