I am using cassandra 2.x version and have a requirement to insert/update one row to multiple tables. all tables have different primary key. How can I add multiple assignments to update and more than 1 table name to QueryBuilder
. Using datastax driver. Any pointer would be helpful.
Asked
Active
Viewed 1,469 times
2

Xstian
- 8,184
- 10
- 42
- 72

NewJavaBee
- 53
- 2
- 10
-
Does a `batch` statement fulfill your need? If you are unfamiliar with batch statements see this [SO answer](http://stackoverflow.com/questions/28348717/how-do-atomic-batches-work-in-cassandra/28348718#28348718) on atomicity in batch statements. – Nathan Dec 18 '15 at 15:46
-
I am familiar with Batch, but not sure how can I prepare BatchStatement with multiple tables. – NewJavaBee Dec 18 '15 at 16:32
-
Looking at this again, is atomicity required? Otherwise you would simply create multiple prepared statements and execute each one after the event. – Nathan Dec 18 '15 at 19:16
-
Yeah. I need atomicity and I am using queryBuilder – NewJavaBee Dec 18 '15 at 22:44
1 Answers
1
Someone else asked how to batch update multiple tables on the google group. They referred to a C# driver example of multiple table updating. The principle applies to Java and I have copied the relevant BatchStatement
method from the cassandra java driver.
Relevant code quoted from Java driver below:
/**
* Adds a new statement to this batch.
* <p/>
* Note that {@code statement} can be any {@code Statement}. It is allowed to mix
* {@code RegularStatement} and {@code BoundStatement} in the same
* {@code BatchStatement} in particular. Adding another {@code BatchStatement}
* is also allowed for convenience and is equivalent to adding all the {@code Statement}
* contained in that other {@code BatchStatement}.
* <p/>
* When adding a {@code BoundStatement}, all of its values must be set, otherwise an
* {@code IllegalStateException} will be thrown when submitting the batch statement.
* See {@link BoundStatement} for more details, in particular how to handle {@code null}
* values.
* <p/>
* Please note that the options of the added Statement (all those defined directly by the
* {@link Statement} class: consistency level, fetch size, tracing, ...) will be ignored
* for the purpose of the execution of the Batch. Instead, the options used are the one
* of this {@code BatchStatement} object.
*
* @param statement the new statement to add.
* @return this batch statement.
* @throws IllegalStateException if adding the new statement means that this
* {@code BatchStatement} has more than 65536 statements (since this is the maximum number
* of statements for a BatchStatement allowed by the underlying protocol).
*/
public BatchStatement add(Statement statement) {
// We handle BatchStatement here (rather than in getIdAndValues) as it make it slightly
// easier to avoid endless loop if the use mistakenly pass a batch that depends on this
// object (or this directly).
if (statement instanceof BatchStatement) {
for (Statement subStatements : ((BatchStatement) statement).statements) {
add(subStatements);
}
} else {
if (statements.size() >= 0xFFFF)
throw new IllegalStateException("Batch statement cannot contain more than " + 0xFFFF + " statements.");
statements.add(statement);
}
return this;
}
Basic idea is create a variable for each table update and add each of the variables to the BatchStatement
function.

Nathan
- 3,082
- 1
- 27
- 42