Templates by BIGtheme NET

How to insert data into Super Column Family Using Hector API

In this Article, I will show How insert data into 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 or use existing key space

A Mutator is the interface we use to inserts or deletes
values from the cluster.

In case of Super column family, insert method expects three arguments.

1) Super column key
(In my use case – Stuart and Smith are the super column keys)

2) Super Column Family – “CustomerList” is the Super column family.

3) List of Super columns, Here the columns are another column families.
– Super columns are “username”, “address”, and “account”.
– Column names are “firstname”, “lastname”, “city”, “postcode”, “bank”
and “baranch”.

Mutator mutator = HFactory.createMutator(keySpace, SE);		
// 1 - super column key
// 2 - super column family
// 3 - list of super columns
mutator.insert("Stuart", "CustomerList", 
	HFactory.createSuperColumn("username", 
		Arrays.asList(HFactory.createStringColumn("firstname", "Stuart"), 
		HFactory.createStringColumn("lastname", "Mick")), 
		SE, SE, SE));
mutator.insert("Stuart", "CustomerList", 
	HFactory.createSuperColumn("address", 
		Arrays.asList(HFactory.createStringColumn("city", "California"), 
		HFactory.createStringColumn("postcode", "12345")), 
		SE, SE, SE));            
mutator.insert("Smith", "CustomerList", 
	HFactory.createSuperColumn("username", 
		Arrays.asList(HFactory.createStringColumn("firstname", "Smith"), 
		HFactory.createStringColumn("lastname", "Jhonson")), 
		SE, SE, SE));
mutator.insert("Smith", "CustomerList", 
	HFactory.createSuperColumn("account", 
		Arrays.asList(HFactory.createStringColumn("bank", "WELS FORGO"), 
		HFactory.createStringColumn("baranch", "Oakland")), 
		SE, SE, SE));

Complete Source code is Here,
App.java

package com.devjavasource.cassandra.CassandraDbService;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.ThriftCfDef;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ColumnType;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.factory.HFactory;

public class App {
	public static void main(String[] args) {
		Cluster cluster = null;	
		
		try {
			cluster = HFactory.getOrCreateCluster("production",
					"localHost:9160");
			// If the key space is not exist, you have to crate one with name "devjavasource"
			Keyspace keySpace = HFactory.createKeyspace("devjavasource", cluster);
			
			// Define a Column Family 
			ColumnFamilyDefinition cfDefination = HFactory.createColumnFamilyDefinition(
					"devjavasource", "CustomerList",
					ComparatorType.UTF8TYPE);	
			// Defines cfDefination as super column
			((ThriftCfDef) cfDefination).setColumnType(ColumnType.SUPER);
			
			// Add a column family in cluster
			cluster.addColumnFamily(cfDefination);
           
            System.out.println("CustomerList SuperColumn Family is created ...");

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

	static StringSerializer SE = StringSerializer.get();
}

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 :

Inserted successfully

You can download complete project, Here

CassandraDbService

*** Venkat – Happy leaning ****