...
For testing different scenarios keyspace and table created
Code Block | ||
---|---|---|
| ||
CREATE KEYSPACE test WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 2}; CREATE TABLE user(name text, id text, PRIMARY KEY (name)); |
In java
Code Block | ||
---|---|---|
| ||
PreparedStatement preparedStatement = session.prepare("insert into test2test.user (id, name) values (?, ?)"); PreparedStatement preparedStatement2 = session.prepare("update test2test.user set id='id_updated-1' where name=?"); PreparedStatement preparedStatement3 = session.prepare("update test2test.user set id='id_updated-2' where name=?"); int i = 1; while(i <= 1000) { batchStatement.add(preparedStatement.bind("id_"+i, "user-" + i)); ++i; } batchStatement.add(preparedStatement2.bind("user-1")); batchStatement.add(preparedStatement3.bind("user-2")); |
...
The above query will throw exception
Code Block | |
---|---|
wide | Caused by: com.datastax.driver.core.exceptions.WriteTimeoutException: Cassandra timeout during write query at consistency ONEQUORUM (12 replica were required but only 01 acknowledged the write) |
Observation
The 1000 row data got inserted into user table and got updated also as above batch execution.
The WriteTimeoutException.getWriteType() == BATCH, ensures data is inserted into the table
Approach
To handle above WriteTimeoutException
we can use BatchStatement.Type
Code Block |
---|
BatchStatement batchStatement = new BatchStatement(BatchStatement.Type.LOGGED); |
Batch type of LOGGED, guarantees atomic insertion of data, and WriteTimeoutException.getWriteType() == BATCH, eventually written to the appropriate replicas and the developer doesn't have to do anything.
Thus, the above two checks handles partial write.