Tuesday, 24 July 2018

Cassandra Create Keyspace

Cassandra Create Keyspace

Cassandra Query Language (CQL) facilitates developers to communicate with Cassandra. The syntax of Cassandra query language is very similar to SQL.

What is Keyspace?

A keyspace is an object that is used to hold column families, user defined types. A keyspace is like RDBMS database which contains column families, indexes, user defined types, data center awareness, strategy used in keyspace, replication factor, etc.
In Cassandra, "Create Keyspace" command is used to create keyspace.
Syntax:
  1. CREATE KEYSPACE <identifier> WITH <properties>   
Or
  1. Create keyspace KeyspaceName with replicaton={'class':strategy name,   
  2. 'replication_factor'No of replications on different nodes}   

Different components of Cassandra Keyspace

Strategy: There are two types of strategy declaration in Cassandra syntax:
  • Simple Strategy: Simple strategy is used in the case of one data center. In this strategy, the first replica is placed on the selected node and the remaining nodes are placed in clockwise direction in the ring without considering rack or node location.
  • Network Topology Strategy: This strategy is used in the case of more than one data centers. In this strategy, you have to provide replication factor for each data center separately.
Replication Factor: Replication factor is the number of replicas of data placed on different nodes. More than two replication factor are good to attain no single point of failure. So, 3 is good replication factor.
Example:
Let's take an example to create a keyspace named "javatpoint".
  1. CREATE KEYSPACE javatpoint  
  2. WITH replication = {'class':'SimpleStrategy''replication_factor' : 3};   
Cassandra Create keyspace 1
Keyspace is created now.

Verification:

To check whether the keyspace is created or not, use the "DESCRIBE" command. By using this command you can see all the keyspaces that are created.
Cassandra Create keyspace 2
There is another property of CREATE KEYSPACE in Cassandra.

Durable_writes

By default, the durable_writes properties of a table is set to true, you can also set this property to false. But, this property cannot be set to simplex strategy.
Example:
Let's take an example to see the usage of durable_write property.
  1. CREATE KEYSPACE sssit  
  2. WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy''datacenter1' : 3 }  
  3.  AND DURABLE_WRITES = false;  
Cassandra Create keyspace 3

Verification:

To check whether the keyspace is created or not, use the "DESCRIBE" command. By using this command you can see all the keyspaces that are created.
Cassandra Create keyspace 4

Using a Keyspace

To use the created keyspace, you have to use the USE command.
Syntax:
  1. USE <identifier>  
See this example:
Here, we are using javatpoint keyspace.
Cassandra Create keyspace 5

0 comments:

Post a Comment