Cassandra Java Driver: How are insert, update, and delete results reported?
Asked Answered
R

4

13

I am writing an application and I need to be able to tell if an inserts and updates succeed. I am using "INSERT ... IF NOT EXISTS" to get the light weight transaction behavior and noticed that the result set returned from execute, contains a row with updated data and an "[applied]" column that can be queried. That is great. But I have an update statement that is returning an empty ResultSet. It appears as though the update is succeeding but I want an programatic way to verify that.

To Clarify:

I have turned on some logging of the result sets returned by my mutations. I have found that "INSERT...IF NOT EXIST"s returns a ResultSet with a boolean column named "[applied]". If "[applied]" is false, it also returns the row that already exits.

With UPDATE, I always see an empty ResultSet.

So I have two questions:

  1. Where is the documentation on what the ResultSet should contain for each type of mutation? I did not see it in the CQL docs or in the Java Driver docs. I even tried looking at other language integrations' docs and did not find any description of the ResultSet contents for mutations.
  2. Is there any way to find out how many rows were modified by an UPDATE or deleted by a DELETE?
Rotate answered 15/1, 2014 at 20:47 Comment(0)
F
3

Even I am stuck with the same issue . One thing (a bad hack though) I discovered is if the update or insert fails the column definitions in the result set are more than one and if mutations succeeds the column definition contain only one column i.e "applied" . The problem is ResultSet does not contain the value of "applied" column which is "true" in case the mutation succeeds and "false" when the transactions prevent changing the data . I am using this hack as of now in my application but I don't think its a good solution , so even Im looking for a better solution.

Focus answered 15/4, 2014 at 4:57 Comment(2)
Yes, this is not really a hack as it is the only way Cassandra seems to communicate the result of the mutation. I am doing the same thing. I discovered this by logging the content of the result sets, which I expected to be empty. I wish the documentation were clearer on this behavior.Rotate
Cassandra doc.: An INSERT writes one or more columns to a record in a Cassandra table atomically and in isolation. No results are returned.Stoneware
I
3

In Cassandra insert/update/delete behave the same and they are called mutations. If your client is not returning any exceptions, then the mutation is done.

If you are concerned about consistency of your mutation calls, then add USING CONSISTENCY with higher levels.

http://www.datastax.com/docs/1.0/references/cql/index http://www.datastax.com/docs/1.1/dml/data_consistency

If you are after good consistency, I recommend using LOCAL_QUORUM for both reads and mutations. That way you don't have to worry about programmatically check a mutation because it will require a consequent read.

Interrogatory answered 16/1, 2014 at 1:59 Comment(2)
Thanks, this is helpful but it doesn't address the real issue. I have added some info to the question to help clarify my issue.Rotate
what does cassandra return when Mutation fails?Battery
F
3

Even I am stuck with the same issue . One thing (a bad hack though) I discovered is if the update or insert fails the column definitions in the result set are more than one and if mutations succeeds the column definition contain only one column i.e "applied" . The problem is ResultSet does not contain the value of "applied" column which is "true" in case the mutation succeeds and "false" when the transactions prevent changing the data . I am using this hack as of now in my application but I don't think its a good solution , so even Im looking for a better solution.

Focus answered 15/4, 2014 at 4:57 Comment(2)
Yes, this is not really a hack as it is the only way Cassandra seems to communicate the result of the mutation. I am doing the same thing. I discovered this by logging the content of the result sets, which I expected to be empty. I wish the documentation were clearer on this behavior.Rotate
Cassandra doc.: An INSERT writes one or more columns to a record in a Cassandra table atomically and in isolation. No results are returned.Stoneware
B
3

One workaround I am using to determine if INSERT worked or not is to check the ROW data returned from ResultSet:

ResultSet rs = session.execute(bs);
Row row = rs.one();
boolean insertFailed = row.getColumnDefinitions().contains("ownerid");

Here, ownerid is the primary row key. You can use your respective column name.

Bot answered 11/2, 2016 at 5:0 Comment(0)
D
1

Is there any way to find out how many rows were modified by an UPDATE or deleted by a DELETE?

As far as I can tell, no, because the DELETE or UPDATE is eventually consistent. It's possible that an INSERT with consistency level ONE or LOCAL_ONE didn't replicate fully, and the mutation was done on other host before the INSERT fully replicated. The mutation will still apply, but you can't know how many rows were actually affected.

Duren answered 10/4, 2015 at 18:26 Comment(1)
yes in the past year I have learned a lot more about the implications of eventual consistency. Thanks thoughRotate

© 2022 - 2024 — McMap. All rights reserved.