Apache Cassandra 1.0 Documentation

CREATE KEYSPACE

This document corresponds to an earlier product version. Make sure you are using the version that corresponds to your version.

Latest Cassandra documentation | Earlier Cassandra documentation

Define a new keyspace and its replica placement strategy.

Synopsis

CREATE KEYSPACE <ks_name>
    WITH strategy_class = <value>
    [ AND strategy_options:<option> = <value> [...] ];

Description

CREATE KEYSPACE creates a top-level namespace and sets the replica placement strategy (and associated replication options) for the keyspace. Valid keyspace names are strings of alpha-numeric characters and underscores, and must begin with a letter. Properties such as replication strategy and count are specified during creation using the following accepted keyword arguments:

Keyword Description
strategy_class Required. The name of the replica placement strategy for the new keyspace, such as SimpleStrategy and NetworkTopologyStrategy.
strategy_options Optional. Additional arguments appended to the option name.

Use the strategy_options keyword, separated by a colon, :, to specify a strategy option. For example, a strategy option of DC1 with a value of 1 would be specified as strategy_options:DC1 = 1; replication_factor for SimpleStrategy could be strategy_options:replication_factor=3.

See Choosing Keyspace Replication Options for guidance on how to best configure replication strategy and strategy options for your cluster.

CREATE KEYSPACE Excelsior WITH strategy_class = 'SimpleStrategy'
  AND strategy_options:replication_factor = 1;
CREATE KEYSPACE Excalibur WITH strategy_class = 'NetworkTopologyStrategy'
  AND strategy_options:DC1 = 1 AND strategy_options:DC2 = 3;

For NetworkTopologyStrategy, you specify the number of replicas per data center in the format of strategy_options:<datacenter_name>=<number>. Note that what you specify for <datacenter_name> depends on the cluster-configured snitch you are using. There is a correlation between the data center name defined in the keyspace strategy_options 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.

Examples

Define a new keyspace using the simple replication strategy:

CREATE KEYSPACE MyKeyspace WITH strategy_class = 'SimpleStrategy'
 AND strategy_options:replication_factor = 1;

Define a new keyspace using a network-aware replication strategy and snitch. This example assumes you are using the PropertyFileSnitch and your data centers are named DC1 and DC2 in the cassandra-topology.properties file:

CREATE KEYSPACE MyKeyspace WITH strategy_class = 'NetworkTopologyStrategy'
 AND strategy_options:DC1 = 3 AND strategy_options:DC2 = 3;