Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cassandra creates a key space


May 17, 2021 Cassandra


Table of contents


Use Cqlsh to create a Keyspace

The keyspace in Cassandra is a namespace that defines data replication on a node. E ach node in the cluster contains a key space. The syntax for creating key space using the statement CREATE KEYSPACE is given below.

Statement

CREATE KEYSPACE <identifier> WITH <properties>

That is

CREATE KEYSPACE “KeySpace Name”
WITH replication = {'class': ‘Strategy name’, 'replication_factor' : ‘No.Of   replicas’};

CREATE KEYSPACE “KeySpace Name”
WITH replication = {'class': ‘Strategy name’, 'replication_factor' : ‘No.Of  replicas’}

AND durable_writes = ‘Boolean value’;

The CREATE KEYSPACE statement has two properties: replication and durable_writes.

Copy

The replication option specifies the replica location policy and the number of copies required. The following table lists all replica location policies.

The name of the policy Describe
Simple strategy Specify a simple replication factor for the cluster.
Network topology policy With this option, you can set replication factors for each data center individually.
The old network topology policy With this option, you can set replication factors for each data center individually.

With this option, you can indicate whether Cassandra uses cometlog for the current KeySpace update. This option is not mandatory and is set to true by default.

Example

Here's an example of creating KeySpace.

  • Here we create a KeySpace called TutorialsPoint.

  • We use the first copy placement policy, the Simple Policy.

  • We chose to have a copy factor of 1 copy.

cqlsh.> CREATE KEYSPACE tutorialspoint
WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};

Verify

You can use the command Describe to verify that a table is created. If you use this command for key space, it displays all the key spaces created as shown below.

cqlsh> DESCRIBE keyspaces;

tutorialspoint system system_traces 

Here you can observe the newly created KeySpace tutorialspoint.

Durable_writes

By default, the durable_writes of the table is set to true, but it can be set to false. You cannot set this property to a simplex policy.

Example

The following is an example of the use of example persistent write properties.

cqlsh> CREATE KEYSPACE test
... WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 }
... AND DURABLE_WRITES = false;

Verify

You can verify that test KeySpace's properties are set to durable_writes by querying the system key space. This query provides all KeySpaces and their properties.

cqlsh> SELECT * FROM system.schema_keyspaces;

  keyspace_name | durable_writes |                                       strategy_class | strategy_options
----------------+----------------+------------------------------------------------------+----------------------------

           test |          False | org.apache.cassandra.locator.NetworkTopologyStrategy | {"datacenter1" : "3"}

 tutorialspoint |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor" : "4"}

         system |           True |           org.apache.cassandra.locator.LocalStrategy | { }

  system_traces |           True |          org.apache.cassandra.locator.SimpleStrategy | {"replication_factor" : "2"}

(4 rows)

Here you can observe the test KeySpace durable_writes the property set to false.

Use Keyspace

You can use the keywordUSE to use KeySpace, which you created. The syntax is as follows:

Syntax:USE <identifier>

Example

In the following example, we use KeySpace tutorialspoint.

cqlsh> USE tutorialspoint;
cqlsh:tutorialspoint>

Create a Keyspace using the Java API

You can create a Keyspace using the Sex class's execute() method. Follow these steps to create a key space using the Java API.

Step 1: Create a cluster object

First, create an instance of the Cluster.builder class called com.datastax.driver.core, as shown below.

//Creating Cluster.Builder object

Cluster.Builder builder1 = Cluster.builder();

Use the addContactPoint() method of the Cluster.Builder object to add a contact point (the IP address of the node). This method returns to Cluster.Builder.

//Adding contact point to the Cluster.Builder object

Cluster.Builder builder2 = build.addContactPoint( "127.0.0.1" );

Create a cluster object with the new builder object. T o do this, there is a method called build () in the Cluster.Builder class. The following code shows how to create cluster objects.

//Building a cluster
Cluster cluster = builder.build();

You can build a cluster object in a single line of code, as shown below.

Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();

Step 2: Create a session object

Create an instance of a Session object using the Connect() method of the Cluster class, as shown below.

Session session = cluster.connect( );

This method creates a new session and initializes it. If you already have a key space, you can set it to the existing key space by passing the keyspace name in string format to this method, as shown below.

Session session = cluster.connect(“ Your keyspace name ” );

Step 3: Execute the query

You can use the Execute() method of the Session class to perform CQL queries. P ass the query to the execute() method in string format or state class objects. Whether you pass it to this method in string format will execute on cqlsh.

In this example, we create a KeySpace called tp. We use the first replica placement strategy, the simple policy, and we choose the replication factor to be 1 copy.

You must store the query in a string variable and pass it to the execute() method, as shown below.

String query = "CREATE KEYSPACE tp WITH replication "
   + "= {'class':'SimpleStrategy', 'replication_factor':1}; ";
session.execute(query);

Step 4: Use KeySpace

You can use the easycute() method to use the KeySpace you created, as shown below.

execute(“ USE tp ” );

Here's a complete program for creating and using key spaces in Cassandra using the Java API.

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

public class Create_KeySpace {

   public static void main(String args[]){

      //Query
      String query = "CREATE KEYSPACE tp WITH replication "
         + "= {'class':'SimpleStrategy', 'replication_factor':1};";
                    
      //creating Cluster object
      Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
    
      //Creating Session object
      Session session = cluster.connect();
     
      //Executing the query
      session.execute(query);
     
      //using the KeySpace
      session.execute("USE tp");
      System.out.println("Keyspace created"); 
   }
}

Use the class name and .java save the above program and browse to the save location. Compile and execute the program as shown in the following image.

$javac Create_KeySpace.java
$java Create_KeySpace

Under normal conditions, it produces the following output:

Keyspace created