Templates by BIGtheme NET

How to retrieve data from Super Column Family Using Hector API

In this Article, I will show How to retrieve data from Super column family using hector API.
What is Super column family. Simple Use case that helps to understand
Super column family in cassandra.

Tools Uses :

1) Apache-cassandra-2.1.6
2) eclipse version Luna 4.4.1.
3) Maven 3.3.3
4) JDK 1.6

What is Supercolumn Family ?

1) Super columns interns store a column family.
2) A column family with a column family as a value of key.

Simple Use Case :

I have a CustomerList, this holds different customers.
each customer name is unique, this name is a key for CustomerList column family.

Value of these keys are also a column family.
Suppose for customer “Stuart”, “username” and “address” are two column families.

CustomerList={ 
   Stuart:{
     username:{firstname:"Stuart",lastname:"Mike"}
     address:{city:"California",postcode:"12345"}
     ....
   }
   Smith:{
     username:{firstname:"Smith", lastname:"Johnson"}
     account:{bank:"WELS FORGO", branch:"Oakland"}
     ....
   }
   ......
 }

In this Use case,

1) CustomerListis Super Column Family.
2) Stuart and “Smith” are row keys of super column family CustomerListis.
3) “username”, “address”, and “account” are Super column names.
4) “firstname”, “lastname”, “city”, etc are Column names.

superColumnFamily_usecase

Write a Simple Java Program to create a Super column family :

1) Create a simple maven project.

2) Add the dependencies

3) Write a simple program to insert use case data into Super column family.

4) Start the Cassandra server.

5) Run the program and verify the data in cassandra.

Add the given dependency to hector API,


	me.prettyprint
	hector-core
	0.8.0-2

Write a simple program to create Super column family :

This program will do the following,

1) Create a Cluster object.

Cluster cluster = null;
cluster = HFactory.getOrCreateCluster( "Cassandra DB Operations Cluster", "localHost:9160" );

HFactory is the hector convenience class with bunch of static methods.
getOrCreateCluster() is a static method, that tries to create a Cluster instance for an
existing Cassandra cluster.

If another class already called getOrCreateCluster, the factory returns the cached instance.
If the instance doesn’t exist in memory, a new ThriftCluster is created and cached.

This method is expecting two parameters,
(a) clusterName – This should be unique name (we should not have two clusters with same name )
and this name will be used as key to store the cluster object in map of clusters.
(b) hostIp – Using provided hostIp value, internally this method create CassandraHostConfigurator
instance and pass that as second parameter.

2) Create or use existing key space

    Keyspace keySpace = HFactory.createKeyspace("devjavasource", cluster);

Here “devjavasource” is the existed keyspace, I am using the same.
createKeyspace() static method in HFactory class will Creates a Keyspace with the default
consistency level policy (default is – ON_FAIL_TRY_ALL_AVAILABLE).

This consistency level,

What should the client do if a call to cassandra node fails
and we suspect that the node is down.
(e.g. it’s a communication error, not an application error).

There are three different consistency levels,

(a) FAIL_FAST – On communication failure, just return the error to the client and don’t retry.

(b) ON_FAIL_TRY_ONE_NEXT_AVAILABLE – On communication error try one more server before giving up.
Before giving up, cassandra node try one more server node is up to process the client request.

(c) ON_FAIL_TRY_ALL_AVAILABLE – On communication error try all known servers before giving up.
This is the case, If and only if all nodes are down. Then only client get communication failure.
That is why Cassandra is more stable and we can deliver most robust applications.

3) Create SuperColumnQuery to read data from Super column family:

Simple way of reading data from Super column family is by using
SuperColumnQuery. A SuperColumnQuery is used for querying the value of
a single entire supercolumn from a SC family.

We can create or get SuperColumnQuery object by calling createSuperColumnQuery()
static method of HFactory class.

This method expects five arguments, first one is keyspace and another
four are StringSerializer objects.

// Create or get instance of SuperColumnQuery
SuperColumnQuery<String, String, String, String> superColumnQuery = 
                HFactory.createSuperColumnQuery(keySpace, SE, SE, SE, SE);
// If we want single row keys of super column family.
// we can set those key value here.
superColumnQuery.setKey("Stuart");
(or) 
superColumnQuery.setKey("Stuart");

// If we want single Super column values of super column family.
// we can set those key value here.
superColumnQuery.setSuperName("username"); (or)
superColumnQuery.setSuperName("address"); (or)
superColumnQuery.setSuperName("account");

//execute the query and get results
QueryResult<HSuperColumn<String, String, String>> result = superColumnQuery.execute();	        		
supercolumn = result.get();

// Itterate the loop and get all the values
int size = supercolumn.getSize();
HColumn<String, String> hCol = null;
for(int cnt=0; cnt<size; cnt++){
	hCol = supercolumn.get(cnt);
	if( hCol != null)
	{
		System.out.println("Column Name: " + hCol.getName() );
		System.out.println("Column Value: " + hCol.getValue() );
	}
}

Complete Source code is Here,
App.java

package com.devjavasource.cassandra.CassandraDbService;

import java.util.Arrays;
import java.util.List;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HSuperColumn;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.SuperColumnQuery;

public class App {
	public static void main(String[] args) {
		Cluster cluster = null;
		Keyspace keySpace = null;
		QueryResult<HSuperColumn<String, String, String>> result = null;
		HSuperColumn<String, String, String> supercolumn = null;
		
		try {
			cluster = HFactory.getOrCreateCluster("production",
					"localHost:9160");
			// If the key space is not exist, you have to crate one with name "devjavasource"
			keySpace = HFactory.createKeyspace("devjavasource", cluster);

            SuperColumnQuery<String, String, String, String> superColumnQuery = 
                HFactory.createSuperColumnQuery(keySpace, SE, SE, SE, SE);
            
            superColumnQuery.setColumnFamily("CustomerList");
            
            System.out.println("Retrive Data from Cassandra Database with Hector Api ...");
        	System.out.println("========================================================");	
        	
        	int size = 0;
        	HColumn<String, String> hCol = null;
        	for( String key :KEYS ){
	        	System.out.println("Super Column Row key is: " + key );        	
	        	superColumnQuery.setKey(key);
	        	for(String col: SUPER_COLUMN_NAMES)
	        	{
	        		superColumnQuery.setSuperName(col);
	        		result = superColumnQuery.execute();	        		
	        		supercolumn = result.get();
	        		
	        		if( supercolumn != null)
	        		{
	        			System.out.println("Super Column Name is: " + col);	        			
	        			superColumnQuery.setKey(key).setSuperName(col);
	        			supercolumn = result.get();
	        			size = supercolumn.getSize();
	        			for(int cnt=0; cnt<size; cnt++){
	        				hCol = supercolumn.get(cnt);
	        				if( hCol != null)
	        				{
		        				System.out.println("Column Name: " + hCol.getName() );
			        			System.out.println("Column Value: " + hCol.getValue() );
	        				}
	        			}	        					
	        		}
	        	}	        	 
        	}
        	System.out.println("========================================================");

		} catch (Exception exp) {
			exp.printStackTrace();
		} finally {
			cluster.getConnectionManager().shutdown();
		}
	}

	final static StringSerializer SE = StringSerializer.get();
	
	final static List<String> KEYS = Arrays.asList("Stuart","Smith");
	final static List<String> SUPER_COLUMN_NAMES = Arrays.asList("username","address","account");	
}

4) Start the Cassandra server :

Cassandra server should be up and running.
If the server is not running, run the server using following command.

Command to start Casandra server is,
C:\apache-cassandra-2.1.6\bin>cassandra.bat -f

5) Run Maven project :

Select and Run As -> Java Application.

Out Put :

Retrive Data from Cassandra Database with Hector Api ...
========================================================
Super Column Row key is: Stuart
Super Column Name is: username
Column Name: firstname
Column Value: Stuart
Column Name: lastname
Column Value: Mick
Super Column Name is: address
Column Name: city
Column Value: California
Column Name: postcode
Column Value: 12345
Super Column Row key is: Smith
Super Column Name is: username
Column Name: firstname
Column Value: Smith
Column Name: lastname
Column Value: Jhonson
Super Column Name is: account
Column Name: bank
Column Value: WELS FORGO
Column Name: baranch
Column Value: Oakland
========================================================

You can download complete project, Here

CassandraDbService

*** Venkat – Happy leaning ****