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

Cassandra creates tables

May 17, 2021 Cassandra

Table of contents

Create a table using Cqlsh

You can use the command CREATE TABLE to create a table. T he syntax for creating a table is given below.


('<column-definition>' , '<column-definition>')
(WITH <option> AND <option>)

Define the column

You can define a column, as shown below.

column name1 data type,
column name2 data type,


age int,
name text

The primary key

The primary key is the column used to uniquely identify the row. b20> ow. ou can define the primary key of the table, as shown below.

CREATE TABLE tablename(
   column1 name datatype PRIMARYKEY,
   column2 name data type,
   column3 name data type.


CREATE TABLE tablename(
   column1 name datatype PRIMARYKEY,
   column2 name data type,
   column3 name data type,
   PRIMARY KEY (column1)


Here's an example of creating a table in Cassandra using cqlsh. We're here:

  • Use keyspace tutorialspoint

  • Create a table named emp

It will have details such as employee name, id, city, salary and phone number. Employee id is the primary key.

cqlsh> USE tutorialspoint;
cqlsh:tutorialspoint>; CREATE TABLE emp(
   emp_id int PRIMARY KEY,
   emp_name text,
   emp_city text,
   emp_sal varint,
   emp_phone varint


The select statement will give you the pattern. Use the select statement to validate the table, as shown below.

cqlsh:tutorialspoint> select * from emp;

 emp_id | emp_city | emp_name | emp_phone | emp_sal

(0 rows)

Here you can observe tables created with a given column. Since we've removed the keyspace tutorial node, you won't find it in the keyspace list.

Create a table using the Java API

You can create tables using the Execute() method of the Session class. Follow the steps given below to create a table 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 Cluster.Builder.

//Adding contact point to the Cluster.Builder object
Cluster.Builder builder2 = build.addContactPoint( "" );

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 =;

You can use a single line of code to build cluster objects, as shown below.

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

Step 2: Create a session object

Use the cluster class's connect() method to create an instance of a Session object, 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 ” );

Here we use a key space called tp. Therefore, the session object is created as follows.

Session session = cluster.connect(“ tp” );

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 the following example, we'll create a table called emp. You must store the query in a string variable and pass it to the execute() method, as shown below.

String query = "CREATE TABLE emp(emp_id int PRIMARY KEY, "
   + "emp_name text, "
   + "emp_city text, "
   + "emp_sal varint, "
   + "emp_phone varint );";

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_Table {

   public static void main(String args[]){

      String query = "CREATE TABLE emp(emp_id int PRIMARY KEY, "
         + "emp_name text, "
         + "emp_city text, "
         + "emp_sal varint, "
         + "emp_phone varint );";
      //Creating Cluster object
      Cluster cluster = Cluster.builder().addContactPoint("").build();
      //Creating Session object
      Session session = cluster.connect("tp");
      //Executing the query
      System.out.println("Table 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.

$java Create_Table

Under normal conditions, it should produce the following output:

Table created