Updating Object data in Apache Cassandra using Hector API

We already discussed the fundamentals of Apache Cassandra database.We discussed the basic operations like insert , read and delete . In the last few chapters we were discussing how object data is  inserting into Apache Cassandra  and  how object data is retrieving from Apache Cassandra database using Hector API.In this chapter we will be updating the same object data we inserted before.

Updating Object data in Apache Cassandra using Hector API

In the previous chapter we inserted two Student objects into ‘myColumnFamily‘. Each object is having unique row key. The ‘id‘ of each object is using as row key.So our column family structure before update is :

objectinsert

Here we are updating the second object .We are simply changing the ‘name’ of the second object. The new column family structure  after update will be like this:

 updatedcolumnfamily

Now lets see the Java code.Before  Before running  , the Apache Cassandra should be started and the ‘USERKEYSPACE‘ should be configured as explained in the previous discussion. Also we should include the libraries listed there  in to the work space .

UpdateObjectsample.java

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.cassandrasamples.beans.Student;
import me.prettyprint.cassandra.model.HColumnImpl;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.ColumnSliceIterator;
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.ComparatorType;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.SliceQuery;

public class UpdateObjectsample {
Cluster cluster = null;
Keyspace keySpace = null;

public UpdateObjectsample() {

}

public void getConfig() {
cluster = HFactory.getOrCreateCluster("Test Cluster", "localHost:9160");
keySpace = HFactory.createKeyspace("USERKEYSPACE", cluster);
}

public void read(String rowKey) {
if (null != cluster && null != keySpace) {
ColumnFamilyDefinition columnFamily = HFactory
.createColumnFamilyDefinition("USERKEYSPACE",
"myColumnFamily", ComparatorType.UTF8TYPE);
SliceQuery query = HFactory
.createSliceQuery(keySpace, StringSerializer.get(),
StringSerializer.get(), StringSerializer.get())
.setKey(rowKey).setColumnFamily(columnFamily.getName());
ColumnSliceIterator iterator = new ColumnSliceIterator(
query, null, "\u00FFF", false);
while (iterator.hasNext()) {
HColumnImpl column = (HColumnImpl) iterator
.next();
System.out
.println(column.getName() + " = " + column.getValue());
}
}

}

public void update(Student student) {
if (null != cluster && null != keySpace) {
ColumnFamilyDefinition columnFamily = HFactory
.createColumnFamilyDefinition("USERKEYSPACE",
"myColumnFamily", ComparatorType.UTF8TYPE);
StringSerializer stringSerializer = StringSerializer.get();
Mutator mutator = HFactory.createMutator(keySpace,stringSerializer);
Map map = processKeyValueMap(student);
Set set = map.entrySet();
Iterator i = set.iterator();
while (i.hasNext()) {
Map.Entry entry = (Map.Entry) i.next();
mutator.addInsertion(student.getId(), columnFamily
.getName(), HFactory.createStringColumn(entry
.getKey().toString(), entry.getValue().toString()));
System.out.println("Updated..");
}
mutator.execute();
}
}

public Map processKeyValueMap(Student student) {
HashMap returnMap = new HashMap();
if (null != student.getId() && null != student.getName()) {
returnMap.put("id", student.getId());
returnMap.put("name", student.getName());

}
return returnMap;

}

public static void main(String[] args) {
UpdateObjectsample sample = new UpdateObjectsample();
sample.getConfig();
System.out.println("Before update..");
sample.read("1");
sample.read("2");
Student student = new Student();
student.setId("2");
student.setName("JayaKrishnan");
sample.update(student);
System.out.println("After update..");
sample.read("1");
sample.read("2");
}

}

Output

Run the application . The output will be like this:

Before update..

id = 1

name = Bijoy

id = 2

name = Karthik

Updated..

Updated..

After update..

id = 1

name = Bijoy

id = 2

name = JayaKrishnan

See Related Discussions:

Apache Cassandra overview

Configuring Apache Cassandra in local machine

Inserting data into Apache Cassandra using Java

Reading data from Apache Cassandra using Java

Listing columns in Apache Cassandra using Java

Deleting columns from Apache Cassandra using Java

Inserting object data into Apache Cassandra database

Reading object data from Apache Cassandra using Hector API