Cassandra Practice Exercises: Essential Techniques for Mastering the Framework

Cassandra Practice: Creating a Keyspace and Table

In this practice, we will cover how to create a keyspace and table in Cassandra. This will include connecting to Cassandra, creating a keyspace, and creating a table.

Step 1: Connect to Cassandra To start, you will need to connect to Cassandra using the cqlsh utility. To connect to Cassandra, run the following command:

Copy codecqlsh

This will start the cqlsh utility, and you should see a prompt that looks like this:

shellCopy codecqlsh>

Step 2: Create a Keyspace Once you have connected to Cassandra, you can create a keyspace. A keyspace is a container for tables and other objects in Cassandra. To create a keyspace, you can use the following CREATE KEYSPACE statement:

sqlCopy codeCREATE KEYSPACE mykeyspace
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

This will create a keyspace named mykeyspace with a replication factor of 1. The replication factor determines how many copies of the data will be stored in the cluster.

Step 3: Create a Table Now that you have created a keyspace, you can create a table. To create a table, you can use the following CREATE TABLE statement:

sqlCopy codeCREATE TABLE mykeyspace.mytable (
    id int PRIMARY KEY,
    name text,
    age int
);

This will create a table named mytable in the mykeyspace keyspace. The table has three columns: id, name, and age. The id column is the primary key, which means that it is used to uniquely identify rows in the table.

Conclusion In this practice, we covered how to create a keyspace and table in Cassandra. We connected to Cassandra, created a keyspace, and created a table. This should provide a good foundation for further exploration and experimentation with Cassandra.

Tags: No tags

Add a Comment

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