Keyspace in Apache Cassandra

Connect with

How to create keyspace in Apache Cassandra in different ways.How to create keyspace in Apache Cassandra in different ways. Learn to create cassandra keyspace, check keyspace, etc. keyspace is like schema in RDBMS and Coulum-family is a table in Cassandra.

1. Keyspace in Apache Cassandra

Creating a keyspace is the CQL counterpart to creating an SQL database. In Cassandra there are different options are available like replication factor, data center, replication strategy, etc. Typically, a cluster has one keyspace per application. Replication is controlled on a per-keyspace basis, so data that has different replication requirements typically reside in different keyspaces.

When you create a keyspace, specify a strategy class for replicating keyspaces. Using the SimpleStrategy class is fine for evaluating Cassandra or with one data center with multiple nodes. For production use or for use with mixed workloads, use with appropriate available options.

2. Create a keyspace in Apache Cassandra

CREATE KEYSPACE user_keyspace
  WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

Here , keyspace can be created with different available options in Cassandra, but I have opted to provide minimal optional with replication_factor=1, as this is test environment.

3. Create Keyspace with Different DataCenter

  WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'dc1' : 3, 'dc2' : 2};

here, dc1 is one data center with replication factor 3 and dc2 is another data center with replication factor 2. And NetworkTopologyStrategy is used in case of multi data center only.

4. Check created keyspaces

Check the keyspaces which were created earlier.

SELECT * FROM system.schema_keyspaces;


cqlsh:user_keyspace> SELECT * from system.schema_keyspaces;

 keyspace_name | durable_writes | strategy_class                                       | strategy_options
      userdemo |           True | org.apache.cassandra.locator.NetworkTopologyStrategy |      {"dc2":"2","dc1":"3"}
 user_keyspace |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"1"}
        system |           True |           org.apache.cassandra.locator.LocalStrategy |                         {}
 system_traces |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor":"2"}

(4 rows)

5. References

Datastax’s cqlsh
I hope you enjoyed this post of Keyspace in Apache Cassandra, and you can visit Apache Cassandra tutorial for more blog post.
Your suggestions or comments are welcome to improve this post.

Connect with

3 thoughts on “Keyspace in Apache Cassandra”

  1. Pingback: Rock

  2. Pingback: Simmi Arora

  3. Pingback: Urmila

Leave a Comment

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