Templates by BIGtheme NET

Update Cassandra Column Family Data using hector API

In this Article, I will show How to update cassandra data with hector API.
How to write a simple java program that insert data into Cassandra database.

Tools Uses :

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

Steps to write Java Program to update Data :

1) Create a simple maven project.

2) Add the dependencies

3) Write a simple program to update existing Cassandra data.

4) Start the Cassandra server.

5) Run the program.

Add the given dependency to hector API,


	me.prettyprint
	hector-core
	0.8.0-2

Write a simple program to update existing Cassandra data :

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) Update the data.

There is no direct method to update data,
what we need to do is insert with latest values,
this will replace the original value.

Syntax is,

// Given statement update the City value of the property P-1.
// From C1 to C1_updated
mutator.insert("P-1", columnFamilyName, HFactory.createStringColumn("City", "C1_updated"));
// Given statement update the Owner value of the property P-1.
// From O1 to O1_updated
mutator.insert("P-1", columnFamilyName, HFactory.createStringColumn("Owner", "O1_updated"));

This Mutator inserts values first delete old values and insert
new values to the cluster.

There are two main ways to use a mutator:

1) Use the insert/delete methods to immediately insert of delete values.

2) Use the addInsertion/addDeletion methods to schedule batch operations
and then execute() all of them in batch. This is for batch update.

But Mutator class is not thread-safe.

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.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;

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 create one with name
	// "devjavasource"
	keySpace = HFactory.createKeyspace("devjavasource", cluster);

	System.out.println("Retrieve Data from Cassandra Database with Hector API ...");
	System.out.println("========================================================");
	printPropertyDetails( keySpace );	
	
	//Update data in Cassandra Database
	Mutator<String> mutator = HFactory.createMutator(keySpace, SE);
	// Insert first property values, with key P-1 -> C1	S1	O1	2BHK, 1200 sq ft
	mutator.insert("P-1", columnFamilyName, HFactory.createStringColumn("City", "C1_updated"));
	mutator.insert("P-1", columnFamilyName, HFactory.createStringColumn("Owner", "O1_updated"));
	
	// Insert first property values, with key P-2 -> C2	S2	O2	2BHK, 1000 sq ft
	mutator.insert("P-2", columnFamilyName, HFactory.createStringColumn("City", "C2_updated"));
	mutator.insert("P-2", columnFamilyName, HFactory.createStringColumn("State", "S2_updated"));
	mutator.insert("P-2", columnFamilyName, HFactory.createStringColumn("Owner", "O2_updated"));
	mutator.insert("P-2", columnFamilyName, HFactory.createStringColumn("Description", "2BHK, 1000 sq ft updated"));
	
	System.out.println("Retrieve Data from Cassandra Database with Hector API After update...");
	System.out.println("======================================================================");
	printPropertyDetails( keySpace );
	
	} catch (Exception exp) {
		exp.printStackTrace();
	} finally {
		cluster.getConnectionManager().shutdown();
	}
 }	
	static void printPropertyDetails(final Keyspace keySpace){
		QueryResult<HColumn<String, String>> result = null;
		// Create a Column Query
		final ColumnQuery<String, String, String> columnQuery = HFactory.createStringColumnQuery(keySpace);
		columnQuery.setColumnFamily(columnFamilyName);
		
		for( String key :KEYS ){
		System.out.println("KEY Value is: " + key );
		columnQuery.setKey(key);
		for(String col: COLUMNS)
		{
		columnQuery.setName(col);
		result = columnQuery.execute();
		System.out.println("Column Key: " + result.get().getName() 
				+ " , Column Value: " + result.get().getValue());
		}
		System.out.println("========================================================");
		}
	}
	final static List<String> KEYS = Arrays.asList("P-1","P-2");
	final static List<String> COLUMNS = Arrays.asList("City","State","Owner","Description");
	final static StringSerializer SE = StringSerializer.get();
	final static String columnFamilyName ="property";
}

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

Run Maven project :

Select and Run As -> Java Application.

Out Put :

Retrieve Data from Cassandra Database with Hector API ...
========================================================
KEY Value is: P-1
Column Key: City , Column Value: C1
Column Key: State , Column Value: S1
Column Key: Owner , Column Value: O1
Column Key: Description , Column Value: 2BHK, 1200 sq ft
========================================================
KEY Value is: P-2
Column Key: City , Column Value: C2
Column Key: State , Column Value: S2
Column Key: Owner , Column Value: O2
Column Key: Description , Column Value: 2BHK, 1000 sq ft
========================================================

Retrieve Data from Cassandra Database with Hector API After update...
======================================================================
KEY Value is: P-1
Column Key: City , Column Value: C1_updated
Column Key: State , Column Value: S1
Column Key: Owner , Column Value: O1_updated
Column Key: Description , Column Value: 2BHK, 1200 sq ft
========================================================
KEY Value is: P-2
Column Key: City , Column Value: C2_updated
Column Key: State , Column Value: S2_updated
Column Key: Owner , Column Value: O2_updated
Column Key: Description , Column Value: 2BHK, 1000 sq ft updated
========================================================

You can download complete project, Here

CassandraDbService

*** Venkat – Happy leaning ****