Apache Cassandra 1.2 Documentation

Creating and updating a keyspace

The first CQL command you need to know is CREATE KEYSPACE. A keyspace is the CQL counterpart to the SQL database, but different in that the Cassandra keyspace is a namespace that defines how data is replicated on nodes. Typically, a cluster has one keyspace per application. Replication is controlled on a per-keyspace basis, so data that has different replication requirements typically resides in different keyspaces. Keyspaces are not designed to be used as a significant map layer within the data model. Keyspaces are designed to control data replication for a set of tables.

To create a keyspace, use the CREATE KEYSPACE command.

cqlsh> CREATE KEYSPACE demodb
         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. For more information about keyspace options, see About data replication.

Using a keyspace

After creating a keyspace, select the keyspace for use, just as you connect to a database in SQL:

cqlsh> USE demodb;

Next, create a table and populate it with data.

Changing the replication factor

Increasing the replication factor increases the total number of copies of keyspace data stored in a Cassandra cluster. This example shows how to change the replication factor of a keyspace.

  1. Update each keyspace in the cluster and change its replication strategy options. For example, to update the number of replicas in when using SimpleStrategy replica placement strategy:

    ALTER KEYSPACE "Excalibur" WITH REPLICATION =
      { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
    

    Or if using NetworkTopologyStrategy:

    ALTER KEYSPACE system_auth WITH REPLICATION =
      {'class' : 'NetworkTopologyStrategy', 'dc1' : 3, 'dc2' : 2};
    
  2. On each affected node, run nodetool repair. Wait until repair completes on a node before moving to the next node.