我正在使用cassandra 2.x版本,并且要求将一行插入/更新到多个表。所有表都有不同的主键。如何将多个分配添加到更新,并将多个表名添加到QueryBuilder
。使用datastax驱动程序。任何指针都会有所帮助。
答案 0 :(得分:1)
其他人在Google群组中询问了how to batch update multiple tables。他们referred to a C# driver example多表更新。该原则适用于Java,我从cassandra java driver复制了相关的BatchStatement
方法。
以下Java驱动程序引用的相关代码:
/**
* 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;
}
基本思想是为每个表更新创建一个变量,并将每个变量添加到BatchStatement
函数。