/**
* 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;
}
1 回答
其他人在谷歌小组上问了how to batch update multiple tables . 他们referred to a C# driver example的多表更新 . 该原则适用于Java,我已经从cassandra java driver复制了相关的
BatchStatement
方法 .下面引用Java驱动程序的相关代码:
基本思想是为每个表更新创建一个变量,并将每个变量添加到
BatchStatement
函数中 .