Cassandra Update Operation
by, 02-23-2012 at 07:19 PM (1195 Views)
To update data in the Cassandra database, batch_update method is used to perform this action. In the below code, we want to update the email address in the database.
In the above code, a Column is created that contains mutation which we want to do. We give it a key and the new value for email address. After this, we add the Column to ColumnOrSuperColumn aggregate and this is added into a Mutation object. New columns can be created or existing columns can be deleted by this Mutation. It will create a column in the database, if supplied key does not exist in the database. If key already exists in database, it will overwrite its value. Also you can tell Mutation object that you are interested in deletion of this value.Java Code: This is the code to explain Cassandra Update Operationlong timestamp = System.currentTimeMillis(); Column column = new Column("email".getBytes("utf-8"), "email@example.com".getBytes("utf-8"), timestamp); ColumnOrSuperColumn columnOrSuperColumn = new ColumnOrSuperColumn(); columnOrSuperColumn.setColumn(column); Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(columnOrSuperColumn); 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);