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

Cassandra reads the data


May 17, 2021 Cassandra


Table of contents


Use the selection clause to read the data

Select clauses are used to read data from tables in Cassandra. W ith this clause, you can read the entire table, a single column, or a specific cell. The syntax of the SELECT clause is given below.

SELECT FROM <tablename>

Example

Suppose you have a table in the keyspace named emp that has the following details:

emp_id emp_name emp_city emp_phone emp_sal
1 Ram Hyderabad 9848022338 50000
2 robin Null 9848022339 50000
3 rahman Chennai 9848022330 50000
4 rajeev Pune 9848022331 30000

The following example shows how to read the entire table using the SELECT clause. Here we read a table emp .

cqlsh:tutorialspoint> select * from emp;

 emp_id |  emp_city | emp_name |  emp_phone | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |   ram    | 9848022338 | 50000
      2 | null      |   robin  | 9848022339 | 50000
      3 | Chennai   |   rahman | 9848022330 | 50000
      4 | Pune      |   rajeev | 9848022331 | 30000
		
(4 rows)

Read the required columns

The following example shows how to read a specific column in a table.

cqlsh:tutorialspoint> SELECT emp_name, emp_sal from emp;

 emp_name | emp_sal
----------+---------
      ram | 50000
    robin | 50000
   rajeev | 30000
   rahman | 50000 
	
(4 rows)

Where clause

Using the WHERE clause, you can set constraints on the required columns. The syntax is as follows:

SELECT FROM <table name> WHERE <condition>;

Note: The WHERE clause can only be used for columns that are part of or have a secondary index on the primary key.

In the following example, we are reading the details of an employee who is paid 50,000. First, set the secondary index to the emp_sal.

cqlsh:tutorialspoint> CREATE INDEX ON emp(emp_sal);
cqlsh:tutorialspoint> SELECT * FROM emp WHERE emp_sal=50000;

 emp_id |  emp_city | emp_name |  emp_phone | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |      ram | 9848022338 | 50000
      2 |      null |    robin | 9848022339 | 50000
      3 |   Chennai |   rahman | 9848022330 | 50000

Use the Java API to read data

You can read data from a table using the Easycute() method of the Session class. Follow the steps given below to execute multiple statements with the help of the Java API.

Step 1: Create a cluster object

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( "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. Use the following code to create cluster objects.

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

You can use a single line of code to build cluster objects, 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 do so by formatting the KeySpace name as an existing key space in string format, as shown below.

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

Here we use KeySpace 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 this method in string format will execute on cqlsh.

In this example, we retrieve the data from the emp table. Store the query in a string and pass it to the execute() method of the session class, as shown below.

String query = ”SELECT 8 FROM emp”;
session.execute(query);

Use the execute() method of the Session class to execute the query.

Step 4: Get the DesignSet object

The select query returns the results as a ResultSet object, so the results are stored in the RESULTSET class object, as shown below.

ResultSet result = session.execute( );

The full program that reads the data from the table is given below.

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

public class Read_Data {

   public static void main(String args[])throws Exception{
    
      //queries
      String query = "SELECT * FROM emp";

      //Creating Cluster object
      Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
    
      //Creating Session object
      Session session = cluster.connect("tutorialspoint");
    
      //Getting the ResultSet
      ResultSet result = session.execute(query);
    
      System.out.println(result.all());
   }
}

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 Read_Data.java
$java Read_Data

Under normal conditions, it should produce the following output:

[Row[1, Hyderabad, ram, 9848022338, 50000], Row[2, Delhi, robin,
9848022339, 50000], Row[4, Pune, rajeev, 9848022331, 30000], Row[3,
Chennai, rahman, 9848022330, 50000]]