Apache Cassandra 1.2 Documentation

CREATE KEYSPACE

The Cassandra 1.2 documentation is transitioning to a new format!
Please use the new Cassandra 1.2 documentation instead.
Back to Table of Contents
CQL Commands Table of Contents

Define a new keyspace and its replica placement strategy.

Synopsis

CREATE KEYSPACE | SCHEMA keyspace_name WITH REPLICATION = map
  AND DURABLE_WRITES = true | false

map is a map collection:

{ property : value, property, value : property, value ... }

Synopsis legend

Description

CREATE KEYSPACE creates a top-level namespace and sets the keyspace name, replica placement strategy class, replication options, and durable_writes options for the keyspace. Keyspace names are 32 or fewer alpha-numeric characters and underscores, the first of which is an alpha character. Keyspace names are case-insensitive. To make a name case-sensitive, enclose it in double quotation marks.

To set the replica placement strategy, construct a map of properties and values.

Table of map properties and values

Property Value Value Description
'class' 'SimpleStrategy' or 'NetworkTopologyStrategy' Required. The name of the replica placement strategy class for the new keyspace.
'replication_factor' An integer Required if class is SimpleStrategy; otherwise, not used. The number of replicas of data on multiple nodes.
'<datacenter name>' An integer Required if class is NetworkTopologyStrategy; otherwise, not used. The number of replicas of data on each node in the data center.
'<datacenter name>' An integer Optional if class is NetworkTopologyStrategy. The number of replicas of data on each node in the data center.
. . . . . . More optional replication factors for additional named data centers.

You can also use the alias CREATE SCHEMA.

Example of Setting the SimpleStrategy class

Construct the CREATE KEYSPACE statement by first declaring the name of the keyspace, followed by the WITH REPLICATION keywords and the equals symbol. Next, to create a keyspace that is not optimized for multiple data centers, use SimpleStrategy for the class value in the map. Set replication_factor properties, separated by a colon and enclosed in curly brackets. For example:

CREATE KEYSPACE Excelsior
  WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };

Using SimpleStrategy is fine for evaluating Cassandra. For production use or for use with mixed workloads, use NetworkTopologyStrategy.

Example of Setting the NetworkToplogyStrategy class

Before creating a keyspace that is optimized for multiple data centers, configure the cluster that will use the keyspace. Configure the cluster to use a network-aware snitch, such as the PropertyFileSnitch. Create a keyspace using NetworkToplogyStrategy for the class value in the map. Set one or more key-value pairs consisting of the data center name and number of replicas per data center, separated by a colon and enclosed in curly brackets. For example:

CREATE KEYSPACE "Excalibur"
  WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'dc1' : 3, 'dc2' : 2};

This example sets one replica for a data center named dc1 and two replicas for a data center named dc2. The data center name you use depends on the cluster-configured snitch you are using. There is a correlation between the data center name defined in the map and the data center name as recognized by the snitch you are using. The nodetool ring command prints out data center names and rack locations of your nodes if you are not sure what they are.

For more information about choosing a replication strategy for a cluster, see Choosing keyspace replication options.

Example of setting the durable_writes property

You can set the durable_writes option after the map specification of the CREATE KEYSPACE command. When set to false, data written to the keyspace bypasses the commit log. Be careful using this option because you risk losing data. Do not set this attribute on a keyspace using the SimpleStrategy.

CREATE KEYSPACE Risky
  WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy',
  'dc1' : 1 } AND durable_writes = false;

Checking created keyspaces

Check that the keyspaces were created:

select * from system.schema_keyspaces;
keyspace_name  | durable_writes | strategy_class                                       | strategy_options
---------------+----------------+------------------------------------------------------+----------------------------
     excelsior |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"3"}
     Excalibur |           True | org.apache.cassandra.locator.NetworkTopologyStrategy |      {"dc2":"2","dc1":"3"}
         risky |          False | org.apache.cassandra.locator.NetworkTopologyStrategy |                {"dc1":"1"}
        system |           True |           org.apache.cassandra.locator.LocalStrategy |                         {}
 system_traces |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"1"}

Cassandra converted the excelsior keyspace to lowercase because quotation marks were not used to create the keyspace and retained the initial capital letter for the Excalibur because quotation marks were used.