Cassandra Tutorial
Cassandra Keyspace
Cassandra Table Index
Cassandra Query (CQL)
Cassandra Collections
Cassandra Interview
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:
- CREATE KEYSPACE <identifier> WITH <properties>
Or
- Create keyspace KeyspaceName with replicaton={'class':strategy name,
- '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".
- CREATE KEYSPACE javatpoint
- WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};
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.
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.
- CREATE KEYSPACE sssit
- WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 }
- AND DURABLE_WRITES = false;
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.
Using a Keyspace
To use the created keyspace, you have to use the USE command.
Syntax:
- USE <identifier>
See this example:
Here, we are using javatpoint keyspace.