View RSS Feed

Cassandra

Cassandra Delete Operation

Rate this Entry
by , 02-23-2012 at 06:21 PM (1371 Views)
In this section, we will discuss that how we can delete a value from Cassandra database using Java code.
Java Code: This is the code to explain Cassandra Delete Operation
import org.apache.cassandra.thrift.Deletion;
...
long timestamp =System.currentTimeMillis();
List columns = new ArrayList();
columns.add("email".getBytes());
SlicePredicate slicePredicate = new SlicePredicate();
slicePredicate.setColumn_names(columns);
Deletion deletion = new Deletion(timestamp);
deletion.setPredicate(slicePredicate);
Mutation mutation = new Mutation();
mutation.setDeletion(deletion);
List<Mutation> mutations = new ArrayList<Mutation>();
mutations.add(mutation);
Map<String, List<Mutation>> job = new HashMap<String, List<Mutation>>();
job.put("Authors", mutations);
Map<String, Map<String, List<Mutation>>> batch = 
 new HashMap<String, Map<String, List<Mutation>>>();
batch.put("Ronald Mathies", job);
client.batch_mutate("Blog", batch, ConsistencyLevel.ALL);
It is almost similar to updating data in Cassandra database. Only difference is, we do not create a Columns. We create a Deletion object in this case, which takes a predicate to determine what we are interested to delete. In the above code, SlicePredicate is used to store list of columns which we want to remove from database.

Submit "Cassandra Delete Operation" to Facebook Submit "Cassandra Delete Operation" to Digg Submit "Cassandra Delete Operation" to del.icio.us Submit "Cassandra Delete Operation" to StumbleUpon Submit "Cassandra Delete Operation" to Google

Comments