Reading Data from Apache Cassandra using Java

We already explained  the fundamentals of Apache Cassandra . We also discussed the configuration of Apache Cassandra . In this chapter we are looking into how data is retrieving from Apache Cassandra using java. In the  just previous chapter we discussed about inserting data into Apache Cassandra.In this chapter we are reading the same piece of data inserted there .

Reading Data from Apache Cassandra using Java

Apache Cassandra should be installed and configured  in local machine to go further in this chapter .These things were explained in the article about configuration of Apache Cassandra. The libraries listed there should be added to the  project as dependencies.Also we need to insert the data to Cassandra by running the code explained in the previous article(Because we are trying to retrieve the same data inserted there). Now the keyspace ‘USERKEYSPACE‘  contains  column family ‘userColumnFamily‘ . And the column family contains data  as shown in the schematic.

Where names is the row key and  1 , and 3 are column names. Values are there against column names  as shown above.

ReadSample.java

import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;

public class ReadSample {
Cluster cluster = null;
Keyspace keySpace = null;
ColumnQuery columnQuery = null;
QueryResult> result = null;

public ReadSample() {

}

public void readDetails() {
cluster = HFactory.getOrCreateCluster("Test Cluster", "localHost:9160");
keySpace = HFactory.createKeyspace("USERKEYSPACE", cluster);
columnQuery = HFactory.createStringColumnQuery(keySpace);
columnQuery.setColumnFamily("userColumnFamily").setKey("names")
.setName("1");
result = columnQuery.execute();
System.out.println("Key = " + result.get().getName() + " ; Value = "
+ result.get().getValue());
columnQuery.setColumnFamily("userColumnFamily").setKey("names")
.setName("2");
result = columnQuery.execute();
System.out.println("Key = " + result.get().getName() + " ; Value = "
+ result.get().getValue());
columnQuery.setColumnFamily("userColumnFamily").setKey("names")
.setName("3");
result = columnQuery.execute();
System.out.println("Key = " + result.get().getName() + " ; Value = "
+ result.get().getValue());
System.out.println("Finished...");

}

public static void main(String[] args) {
ReadSample sample = new ReadSample();
sample.readDetails();
}
}

Output

Compile and run the  code . The output will be :

Key = 1 ; Value = Bijoy

Key = 2 ; Value = Karthik

Key = 3 ; Value = JayaKrishnan

Finished…

See Related:

Apache Cassandra Tutorial

Configuring Apache Cassandra in local machine

Inserting data in to Apache Cassandra database

Listing columns of a column family using java

Deleting column from Apache Cassandra using Java

Inserting objects into Apache Cassandra using Hector API

Reading object data from Apache Cassandra using Hector API