CREATE KEYSPACE
Creates a top-level keyspace. Configure the replica placement strategy, replication factor, and durable writes setting.
Use only replication strategy implementations bundled with Cassandra or DataStax Enterprise. |
Synopsis
CREATE KEYSPACE [ IF NOT EXISTS ] <keyspace_name> WITH REPLICATION = { <replication_map> } [ AND DURABLE_WRITES = ( true | false ) ] [ AND graph_engine = 'Core' ] ;
Syntax legend
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. |
- keyspace_name
-
Maximum of 222 characters. Can contain alpha-numeric characters and underscores; only letters and numbers are supported as the first character. Unquoted names are forced to lowercase.
If a keyspace with the same name already exists, an error occurs and the operation fails; use IF NOT EXISTS to suppress the error message.
- replication_map
-
The replication map determines how many copies of the data are kept in a given datacenter. This setting impacts consistency, availability and request speed, for more details see replica placement strategy.
Replication strategy class and factor settings Class Replication factor Value Description 'SimpleStrategy'
'replication_factor' : <N>
Assign the same replication factor to the entire cluster. Use for evaluation and single datacenter test and development environments only.
'NetworkTopologyStrategy'
'<datacenter_name>' : <N>
Assign replication factors to each datacenter in a comma-separated list. Use in production environments and multi-DC test and development environments. Datacenter names must match the snitch DC name; refer to Snitches for more details.
Simple Topology syntax:
'class' : 'SimpleStrategy', 'replication_factor' : <N>
Network Topology syntax:
'class' : 'NetworkTopologyStrategy', '<dc1_name>' : <N> [ , ... ]
- DURABLE_WRITES = true | false
-
Optional. Although not recommended, can be changed to false, to bypass the commit log when writing to the keyspace. Default value is
true
.Never disable durable writes when using SimpleStrategy replication.
- graph_engine = 'Core'
-
Optional. Use to treat a Cassandra keyspace as a graph. Only the 'Core' engine can be specified; the 'Classic' engine cannot.
Examples
Create a keyspace for a single node evaluation cluster
Create cycling keyspace on a single node evaluation cluster:
CREATE KEYSPACE IF NOT EXISTS cycling
WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
};
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';
For more information on graphs, see About DataStax Graph.
Create a keyspace NetworkTopologyStrategy on an evaluation cluster
This example shows how to create a keyspace with network topology in a single node evaluation cluster.
CREATE KEYSPACE cycling
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'dc1' : 1
};
|
$dsetool status >> $results_table
Returns the data center name, rack name, host name and IP address. The following output shows example output from the previous command; the output will be different for your implementation.
DC: Cassandra Workload: Cassandra Graph: no
======================================================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Owns Token Rack Health [0,1]
0
UN 10.0.0.15 67.44 MiB ? -9223372036854775808 rack1 0.90
UN 10.0.0.110 766.4 KiB ? 0 rack2 0.90
Note: you must specify a keyspace to get ownership information.
Create the cycling keyspace in an environment with multiple data centers
Set the replication factor for the Boston, Seattle, and Tokyo datacenters. The data center name must match the name configured in the snitch.
CREATE KEYSPACE IF NOT EXISTS cycling
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'datacenter1': '3',
'datacenter2': '3'
};
For more about replication strategy options, see Changing keyspace replication strategy |
Disabling durable writes
Disable write commit log for the cycling keyspace. Disabling the commit log increases the risk of data loss. Do not disable in SimpleStrategy environments.
CREATE KEYSPACE cycling
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'datacenter1' : 3
}
AND DURABLE_WRITES = false;