How to create Keyspace in Cassandra

In this example I am referring Cassandra 2.1.5 version

Creating a keyspace

Use the following to create a keyspace

create keyspace cassand with replication={'class':'SimpleStrategy', 'replication_factor':3} and durable_writes=true
Here class attributes indicates the replication strategy. 3 types are strategies are applicable:
  • Simple Strategy – Provides simple replication for the cluster.
  • Network Topology Strategy – Each data-center can have different replication factor.
  • Old Network Topology Strategy – This is the legacy one.

By default the value of durable_writes is true. However, it can set to false as well. Setting its value to false indicates that the writes will not be written the CommitLog.

To list the down all the keyspaces use command:
select * from system.schema_keyspaces;
The output will be:
keyspace_name | durable_writes | strategy_class | strategy_options
---------------+----------------+---------------------------------------------+----------------------------
cassand | True | org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"3"}
system | True | org.apache.cassandra.locator.LocalStrategy | {}
system_traces | True | org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"2"}
To list down all the keyspace names use command:
describe keyspaces
Output:
system_traces cassand system
Alter a keyspace
To alter a keyspace use the below command:
alter keyspace cassand with replication={'class':'SimpleStrategy', 'replication_factor':2} and durable_writes=true
Here we are changing the replication_factor value from 3 to 2. You can also change the class attribute value as well as durable_writes value from true to false.
Delete a keyspace
For dropping a keyspace simply use the command:
drop keyspace cassand;
You can verify the results by typing command:
describe keyspaces:
Output:
system_traces system

Leave a Reply

Your email address will not be published. Required fields are marked *