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
Legend
Syntax conventions Description

UPPERCASE

Literal keyword.

Lowercase

Not literal.

< >

Variable value. Replace with a user-defined value.

[]

Optional. Square brackets ([]) surround optional command arguments. Do not type the square brackets.

( )

Group. Parentheses ( ( ) ) identify a group to choose from. Do not type the parentheses.

|

Or. A vertical bar (|) separates alternative elements. Type any one of the elements. Do not type the vertical bar.

...

Repeatable. An ellipsis ( ... ) indicates that you can repeat the syntax element as often as required.

'<Literal string>'

Single quotation (') marks must surround literal strings in CQL statements. Use single quotation marks to preserve upper case.

{ <key> : <value> }

Map collection. Braces ({ }) enclose map collections or key value pairs. A colon separates the key and the value.

<datatype2

Set, list, map, or tuple. Angle brackets ( < > ) enclose data types in a set, list, map, or tuple. Separate the data types with a comma.

<cql_statement>;

End CQL statement. A semicolon (;) terminates all CQL statements.

[--]

Separate the command line options from the command arguments with two hyphens ( -- ). This syntax is useful when arguments might be mistaken for command line options.

' <<schema\> ... </schema\>> '

Search CQL only: Single quotation marks (') surround an entire XML schema declaration.

@<xml_entity>='<xml_entity_type>'

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
};

datacenter1 is the default datacenter name. To display the datacenter name, use dsetool status.

    $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;

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com