Templates by BIGtheme NET

How to get Super column count in Super Column Family by key

In this Article, I will show How to get Super column count in Super Column Family
by key 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 SuperCountQuery :

we can create an instance of SuperCountQuery, by calling createSuperCountQuery()
static method of HFactory class.

This method expects three arguments, first one is keyspace and another two are
value of superNameSerializer.

After creation of SuperCountQuery instance,
super column family, super column key and super columns rang should be set.

// Create or get SuperCountQuery object
SuperCountQuery<String, String> superCountQuery = 
		HFactory.createSuperCountQuery(keySpace, SE, SE);

//Set super column Family name
superCountQuery.setColumnFamily("CustomerList");

// Set super column key (Stuart or Smith)
superCountQuery.setKey(key);

// Set range of super columns
superCountQuery.setRange("", "", 2);

// execute and get the count
superCountQuery.execute().get()

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.factory.HFactory;
import me.prettyprint.hector.api.query.SuperCountQuery;

public class App {
	public static void main(String[] args) {
		Cluster cluster = null;
		Keyspace keySpace = 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);
            
            SuperCountQuery<String, String> superCountQuery = 
            		HFactory.createSuperCountQuery(keySpace, SE, SE);
            superCountQuery.setColumnFamily("CustomerList");            
            
            System.out.println("Retrive Data from Cassandra Database with Hector Api ...");
        	System.out.println("========================================================");
        	for( String key :KEYS ){
	        	System.out.println("Super Column Row key is: " + key );
	        	 superCountQuery.setKey(key);
	             superCountQuery.setRange("", "", 2);
	             // Counts super column for a key in a super column family            
	             System.out.println("Super column Count is: " + superCountQuery.execute().get());
	        }
        	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");	
}

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 Count is: 2
Super Column Row key is: Smith
Super column Count is: 2
========================================================

You can download complete project, Here

CassandraDbService

*** Venkat – Happy leaning ****