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
CREATE KEYSPACE | SCHEMA IF NOT EXISTS keyspace_name
WITH REPLICATION = map
AND DURABLE_WRITES = true | false
- map is a JSON value
A map of properties and values defines the two different types of keyspaces:
{ 'class' : 'SimpleStrategy', 'replication_factor' : <integer> };
{ 'class' : 'NetworkTopologyStrategy'[, '<data center>' : <integer>, '<data center>' : <integer>] . . . };
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
cassandra@cqlsh> CREATE KEYSPACE app_putracode WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };
cassandra@cqlsh> describe keyspaces;
system_schema system excelsior system_distributed
system_auth app_putracode keyspace1 system_traces
- Creating Keyspace with
NetworkTopologyStrategy
For usingNetworkTopologyStrategy
in single server(for example). Check the datacenter name usingnodetool
.root@e465eeadc356:/# nodetool status; Datacenter: datacenter1 ======================= Status=Up/Down |/ State=Normal/Leaving/Joining/Moving -- Address Load Tokens Owns (effective) Host ID Rack UN 172.17.0.3 257.5 KiB 256 100.0% 1ae23679-8421-4e1d-8bc8-fe556631b916 rack1
Cassandra uses datacenter1 as the default data center name. Create a keyspace named
app_putracode_nts
on a single node cluster, for example:CREATE KEYSPACE app_putracode_nts WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };
output
root@e465eeadc356:/# cqlsh localhost -u cassandra -p cassandra Connected to Test Cluster at localhost:9042. [cqlsh 5.0.1 | Cassandra 3.11.1 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cassandra@cqlsh> CREATE KEYSPACE app_putracode_nts WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 }; cassandra@cqlsh> describe keyspaces; system_schema app_putracode keyspace1 system_auth excelsior system_distributed system app_putracode_nts system_traces
Alter Keyspace / Schema Cassandra
Command Alter keyspace can change
1. Strategy
2. durable writes
3. replication
Syntax
ALTER KEYSPACE | SCHEMA keyspace_name
WITH REPLICATION = map
| WITH DURABLE_WRITES = true | false
AND DURABLE_WRITES = true | false
example command
ALTER KEYSPACE app_putracode WITH REPLICATION={ 'class': 'NetworkTopologyStrategy', 'datacenter1':1};
Drop Keyspace / Schema Cassandra
Remove the keyspace and data.
Syntax
DROP KEYSPACE | SCHEMA IF EXISTS keyspace_name
example
cassandra@cqlsh> DROP KEYSPACE IF EXISTS app_putracode;
cassandra@cqlsh> DROP KEYSPACE IF EXISTS app_putracode;
cassandra@cqlsh> DROP KEYSPACE app_putracode;
ConfigurationException: Cannot drop non existing keyspace 'app_putracode'.
cassandra@cqlsh>
Using keyword if exists for checkking the keyspace first, if keyspace exists will be removed.