N|Solid
A keyspace is a container that holds the column families, user defined types. In Cassandra, Keyspace is similar to schema RDBMS Database. Keyspace holds column families, indexes, user defined types, data center awareness, strategy used in keyspace, replication factor, etc.

Syntax

  • map is a JSON value

A map of properties and values defines the two different types of keyspaces:

Table of map values

properties Value Value Description
class SimpleStrategy Or NetworkTopologyStrategy Required. The name of the replica placement strategy class for the new keyspace.
replication_factor number of replicas Required if class is SimpleStrategy; otherwise, not used. The number of replicas of data on multiple nodes.
first data center number of replicas Required if class is NetworkTopologyStrategy and you provide the name of the first data center. This value is the number of replicas of data on each node in the first data center.
‘next data center’ number of replicas Required if class is NetworkTopologyStrategy and you provide the name of the second data center. The value is the number of replicas of data on each node in the data center.

Strategy: While declaring strategy name in Cassandra. There are two kinds of strategies declared in Cassandra Syntax.
1. Simple Strategy: Use only for a single data center. SimpleStrategy places the first replica on a node determined by the partitioner. Additional replicas are placed on the next nodes clockwise in the ring without considering topology (rack or data center location).
2. Network Topology Strategy:Use NetworkTopologyStrategy when you have (or plan to have) your cluster deployed across multiple data centers. This strategy specify how many replicas you want in each data center.

Durable Write (true or false)
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.

Example execution creating keyspace in Cassandra database.
1. Creating Keyspace with SimpleStrategy

  1. Creating Keyspace with NetworkTopologyStrategy
    For using NetworkTopologyStrategy in single server(for example). Check the datacenter name using nodetool.

    Cassandra uses datacenter1 as the default data center name. Create a keyspace named app_putracode_nts on a single node cluster, for example:

    output

Alter Keyspace / Schema Cassandra

Command Alter keyspace can change
1. Strategy
2. durable writes
3. replication

Syntax

example command

Drop Keyspace / Schema Cassandra

Remove the keyspace and data.
Syntax

example

Using keyword if exists for checkking the keyspace first, if keyspace exists will be removed.