/    /  Cassandra – Create KeySpace

Cassandra – Create KeySpace

KeySpace in NoSQL database is just like a schema in regular RDBMS concept, Anyhow it does not have any concrete structure. In NoSQL database, there will be one keyspace per application.

A Keyspace contains column families or super columns. Each super column contains one or more column family, each column family contains at least one column.

Syntax:

CREATE  KEYSPACE | SCHEMA  IF NOT EXISTS keyspace_name 
WITH REPLICATION = map
AND DURABLE_WRITES =  true | false

Replication factor:

It is the total number of replicas across the cluster.

Example:

Replication factor 1 ===> one copy of each row on one node.
Replication factor 2 ===> two copies of each row which are placed on different nodes.

Replica Placement Strategy:

There are two types of strategies.

1. Simple Strategy

when you have One Data center, this strategy place the first replica on the node selected by partitioner and remaining replicas in clockwise direction.

Example:

Let us create a KeySpace i2Tutorials

CREATE KEYSPACE i2Tutorials
WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};

Note:We used replication factor 3 which is ideal.

2. Network Topology Strategy

This is used when we have more than 2 data centers. This strategy places multiple replicas in each data.

Example:

CREATE KEYSPACE i2Tutorials
WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1' : 1, 'DC2' : 3}
AND durable_writes = false;

Lets Create Keyspace called i2Tutorials.

cqlsh> CREATE KEYSPACE i2Tutorials
WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
Cqlsh> Describe keyspaces;
i2tutorials  system_schema system_auth system system_distributed system_traces

Validate the KeySpace creation

cqlsh:system_schema> SELECT * FROM system_schema.keyspaces;
keyspace_name      | durable_writes | replication
--------------------+----------------+----------------------------------------
I2Tutorials | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor':'3'}
system_auth | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor':'1'}
system_schema | True | {'class': 'org.apache.cassandra.locator.LocalStrategy'}
system_distributed | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor':'3'}
system | True |	{'class': 'org.apache.cassandra.locator.LocalStrategy'}
system_traces | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor':'2'}