Create a keyspace

In Apache Cassandra® and DataStax Enterprise (DSE), the first schema to create is a keyspace using CQL.

Prerequisites

Keyspace and table name conventions:

The name of a keyspace or a table is a string of alphanumeric characters and underscores, but it must begin with a letter. If case must be maintained, the name must be encased in double quotes, such as "MyTable.

Since tables are defined within a keyspace, you can either use the keyspace as part of the table creation command, or create a table in the current keyspace. To specify the keyspace as part of a table name, use the keyspace name, a period (.), and table name, such as cycling.cyclist-stats.

Simple keyspace

  1. Start cqlsh and create a keyspace.

    CREATE KEYSPACE IF NOT EXISTS cycling
    WITH REPLICATION = {
      'class' : 'SimpleStrategy',
      'replication_factor' : 1
    };

    The keyspace name, replication class, and replication factor must all be specified.

NTS keyspace

  1. Start cqlsh and create a keyspace.

    CREATE KEYSPACE cycling 
    WITH REPLICATION = { 
      'class' : 'NetworkTopologyStrategy', 
      'datacenter1' : 1
    };

    The keyspace name, replication class, and replication factor must all be specified.

Keyspace with datacenters

  1. Verify the datacenter name or names using nodetool status.

    • nodetool command

    • Result

    nodetool status
    Datacenter: datacenter1
    ================
    Status=Up/Down
    |/ State=Normal/Leaving/Joining/Moving
    --  Address         Load       Tokens       Owns    Host ID                               Rack
    UN  10.200.181.134  4.97 MiB   1            ?       c9027497-011c-4390-ae59-e4ed1ac794cc  rack1
    UN  10.200.181.135  5.16 MiB   1            ?       af2af8ec-5fa2-4a04-ac9c-c75669d8d3a0  rack1

    Ensure that the datacenter name matches exactly in the next step, as the name is case-sensitive.

  2. Start cqlsh and create a keyspace.

    CREATE KEYSPACE IF NOT EXISTS cycling
    WITH REPLICATION = {
      'class' : 'NetworkTopologyStrategy',
      'datacenter1': '3', 
      'datacenter2': '3'
    };

    The keyspace name, replication class, and replication factor must all be specified. The replication factor is set for each datacenter.

Use created keyspace

Switch to the keyspace:

USE cycling;

Related information:

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