CREATE KEYSPACE
Creates a keyspace with keyspace options.
Keyspaces are the first schema objects you create in Cassandra-based databases.
Keyspaces are containers for all lower schema objects, including tables, user-defined types, materialized views, and indexes.
Synopsis
CREATE KEYSPACE [ IF NOT EXISTS ] <keyspace_name>
WITH REPLICATION = { <replication_map> }
[ AND DURABLE_WRITES = ( true | false ) ]
[ AND graph_engine = 'Core' ] ;
| Syntax conventions | Description |
|---|---|
UPPERCASE |
Literal keyword. |
Lowercase |
Not literal. |
|
Variable value. Replace with a user-defined value. |
|
Optional.
Square brackets ( |
|
Group.
Parentheses ( |
|
Or.
A vertical bar ( |
|
Repeatable.
An ellipsis ( |
|
Single quotation ( |
|
Map collection.
Braces ( |
|
Set, list, map, or tuple.
Angle brackets ( |
|
End CQL statement.
A semicolon ( |
|
Separate the command line options from the command arguments with two hyphens ( |
|
Search CQL only: Single quotation marks ( |
|
Search CQL only: Identify the entity and literal value to overwrite the XML element in the schema and solrConfig files. |
Options
| Option | Description | Default | ||
|---|---|---|---|---|
|
Name of the keyspace to create. The following rules apply to keyspace names:
|
|||
|
Allows silent skip by checking for existing keyspaces with the same name. If a match is found, the operation silently does nothing (no error is thrown). If no match is found, the operation creates the keyspace. |
Not set. If a keyspace with the same name already exists, an error occurs and the operation fails. |
||
|
A map of replication strategy and factor settings. Determines how many copies of the data are stored in a given datacenter. This setting impacts consistency, availability, and latency. For more information, see Data replication. The The The For examples, see Examples. [IMPORTANT] ==== Use only replication strategy implementations included with your database. ==== |
|||
|
Non-default configuration not recommended. If
|
|
||
|
Optional.
Use to treat a Cassandra keyspace as a graph.
Only the |
Examples
- Use the SimpleStrategy class
-
Create a keyspace on a single-node cluster with the
SimpleStrategyclass:CREATE KEYSPACE IF NOT EXISTS cycling WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }; - Use the NetworkTopologyStrategy class
-
With the
NetworkTopologyStrategy, you provide a list of datacenters with individual replication factors, instead of a single replication factor for the entire cluster.You cannot insert data into a table in a keyspace that uses
NetworkTopologyStrategyunless you define the datacenter names in the snitch properties file.Verify the spelling of the datacenter name before using this command because datacenter names are case-sensitive and must match the names configured in the snitch properties file.
-
Use
nodetool statusto get the names of the datacenters:nodetool statusThe datacenter name is on the line before the node status table:
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 -
Create a keyspace, specifying the keyspace name, replication class, and a comma-separated list of datacenters and replication factors:
CREATE KEYSPACE IF NOT EXISTS cycling WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1': '3', 'datacenter2': '3' };
-
- Create a keyspace for a graph
-
Create a keyspace to use for graph data:
CREATE KEYSPACE food_cql WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor': 1 } AND graph_engine = 'Core';
Use a keyspace
After you create a keyspace, set it as your active keyspace with the USE command.
Once set, you can run CQL commands against it without specifying the keyspace name in every command.