Archives for 

Apache Cassandra

This category explains the features of Apache Cassandra with examples

Deleting a column from Apache Cassandra using Java

We already discussed the fundamentals of Apache Cassandra in a previous article. We also discussed how to configure Apache Cassandra database in local computer.We discussed the operations like insertion into Apache Cassandra and reading into Apache Cassandra database. In this chapter we are discussing how a column  from a column family is deleting  from Cassandra using  Java program . The program explains here is using Hector API.So  we should include the libraries listed in the configuration page  to the workspace.

Deleting a column from Apache Cassandra using Java

We are using the same ‘USERKEYSPACE‘  created using the Cassandra command line client as explained in the configuration page.We are using the same column family  ‘userColumnFamily‘ created  earlier.We are trying to delete one column among the three we inserted there.So our column family structure   before deletion is:

So ‘names‘ is the row key . ‘1‘ , ‘2‘ and ‘3‘ are the column names.We  are going to delete the column with column name ‘3‘. Now lets see the Java code.

DeleteSample.java

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

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

public DeleteSample() {

}

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

}

public void readDetails() {
if (null != cluster && null != keySpace) {
SliceQuery query = HFactory
.createSliceQuery(keySpace, StringSerializer.get(),
StringSerializer.get(), StringSerializer.get())
.setKey("names").setColumnFamily("userColumnFamily");
ColumnSliceIterator iterator = new ColumnSliceIterator(
query, null, "\u00FFF", false);
while (iterator.hasNext()) {
HColumnImpl column = (HColumnImpl) iterator
.next();
System.out.println("Column name = " + column.getName()
+ "; Column value = " + column.getValue());
}
}
}

public void deleteColumn() {
if (null != cluster && null != keySpace) {
StringSerializer stringSerializer = StringSerializer.get();
Mutator mutator = HFactory.createMutator(keySpace,
stringSerializer);
mutator.delete("names", "userColumnFamily", "3", stringSerializer);
}
}

public static void main(String[] args) {
DeleteSample sample = new DeleteSample();
sample.getConfig();
System.out.println("Before Deleting a column ...");
sample.readDetails();
System.out.println("Deleting....");
sample.deleteColumn();
System.out.println("After deletion....");
sample.readDetails();
System.out.println("Done......");
}

}

Output

Before running the code the libraries listed in the configuration page should be added as dependency and Apache Cassandra should be started as discussed there. Then run the code.

Before Deleting a column …

Column name = 1; Column value = Bijoy

Column name = 2; Column value = Karthik

Column name = 3; Column value = JayaKrishnan

Deleting….

After deletion….

Column name = 1; Column value = Bijoy

Column name = 2; Column value = Karthik

Done……

See Related Posts:

Apache Cassandra Tutorial

Configuring Apache Cassandra in local computer

Inserting data into Apache Cassandra using Java

Reading data from Apache Cassandra using Java

Listing columns in a column family using Java

Inserting objects into Apache Cassandra using Hector API

Reading object data from Apache Cassandra using Hector API