public class BatchStatement extends Statement
Statementso they get executed as a batch.
Note: BatchStatement is not supported with the native protocol version 1: you will get an
UnsupportedFeatureException when submitting one if version 1 of the protocol is in use
(i.e. if you've force version 1 through
Cluster.Builder.withProtocolVersion(com.datastax.driver.core.ProtocolVersion) or you use
Cassandra 1.2). Note however that you can still use CQL Batch statements even with
the protocol version 1.
Setting a BatchStatement's serial consistency level is only supported with the native protocol
version 3 or higher (see
|Modifier and Type||Class and Description|
The type of batch to use.
|Constructor and Description|
Creates a new
Creates a new batch statement of the provided type.
|Modifier and Type||Method and Description|
Adds a new statement to this batch.
Adds multiple statements to this batch.
Clears this batch, removing all statements added so far.
Returns the keyspace this query operates on.
Returns the routing key (in binary raw form) to use for token aware routing of this query.
The statements that have been added to this batch so far.
Whether this statement is idempotent, i.e.
Returns the number of bytes required to encode this statement.
Sets the keyspace the queries in this batch statement operate on.
Sets the serial consistency level for the query.
Returns the number of elements in this batch.
disableTracing, enableTracing, executingAs, getConsistencyLevel, getDefaultTimestamp, getFetchSize, getHost, getOutgoingPayload, getReadTimeoutMillis, getRetryPolicy, getRoutingToken, getSerialConsistencyLevel, isBatchIdempotent, isTracing, setConsistencyLevel, setDefaultTimestamp, setFetchSize, setHost, setIdempotent, setOutgoingPayload, setPagingState, setPagingState, setPagingStateUnsafe, setReadTimeoutMillis, setRetryPolicy
public BatchStatement(BatchStatement.Type batchType)
batchType- the type of batch.
public BatchStatement add(Statement statement)
statement can be any
Statement. It is allowed to mix
BoundStatement in the same
BatchStatement in particular.
BatchStatement is also allowed for convenience and is equivalent to
adding all the
Statement contained in that other
Due to a protocol-level limitation, adding a
RegularStatement with named values is
currently not supported; an
IllegalArgument will be thrown.
When adding a
BoundStatement, all of its values must be set, otherwise an
IllegalStateException will be thrown when submitting the batch statement. See
BoundStatement for more details, in particular how to handle
Please note that the options of the added Statement (all those defined directly by the
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
statement- the new statement to add.
IllegalStateException- if adding the new statement means that this
BatchStatementhas more than 65536 statements (since this is the maximum number of statements for a BatchStatement allowed by the underlying protocol).
IllegalArgumentException- if adding a regular statement that uses named values.
public BatchStatement addAll(Iterable<? extends Statement> statements)
This is a shortcut method that calls
add(com.datastax.driver.core.Statement) on all the statements from
statements- the statements to add.
public Collection<Statement> getStatements()
public BatchStatement clear()
public int size()
public int requestSizeInBytes(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)
The calculated size may be overestimated by a few bytes, but is never underestimated. If the size cannot be calculated, this method returns -1.
Note that the returned value is not cached, but instead recomputed at every method call.
public BatchStatement setSerialConsistencyLevel(ConsistencyLevel serialConsistency)
This is only supported with version 3 or higher of the native protocol. If you call this
method when version 2 is in use, you will get an
submitting the statement. With version 2, protocol batches with conditions have their serial
consistency level hardcoded to SERIAL; if you need to execute a batch with LOCAL_SERIAL, you
will have to use a CQL batch.
serialConsistency- the serial consistency level to set.
serialConsistencyis not one of
public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry)
The routing key is optional in that implementers are free to return
routing key is an hint used for token-aware routing (see
TokenAwarePolicy), and if provided should correspond to the
binary value for the query partition key. However, not providing a routing key never causes a
query to fail and if the load balancing policy used is not token aware, then the routing key
can be safely ignored.
protocolVersion- the protocol version that will be used if the actual implementation needs to serialize something to compute the key.
codecRegistry- the codec registry that will be used if the actual implementation needs to serialize something to compute this key.
public BatchStatement setKeyspace(String keyspace)
TokenAwarePolicyto help identify which replicas are applicable to send this statement to.
Note: This expects the internal representation of the keyspace. In the typical case, the internal representation is equivalent to the CQL representation. However, if you are using a keyspace that requires the use of quotes in CQL (a quoted identifier), i.e.: "MyKS", this method will return the unquoted representation instead, i.e. MyKS.
keyspace- the name of the keyspace that queries in this batch operate on.
public String getKeyspace()
Note that not all query specify on which keyspace they operate on, and so this method can
null. Firstly, some queries do not operate inside a keyspace: keyspace
USE queries, user creation, etc. Secondly, even query that operate within a
keyspace do not have to specify said keyspace directly, in which case the currently logged in
keyspace (the one set through a
USE query (or through the use of
Cluster.connect(String))). Lastly, as for the routing key, this keyspace information is only a
hint for token-aware routing (since replica placement depend on the replication strategy in use
which is a per-keyspace property) and having this method return
null (or even a bogus
keyspace name) will never cause the query to fail.
Note: This returns the internal representation of the keyspace. In the typical case, the internal representation is equivalent to the CQL representation. However, if you are using a keyspace that requires the use of quotes in CQL (a quoted identifier), i.e.: "MyKS", this method will return the unquoted representation instead, i.e. MyKS.
public Boolean isIdempotent()
If a statement is not idempotent, the driver will ensure that it never gets executed more than once, which means:
retrieson write timeouts or request errors;
Note that this method can return
null, in which case the driver will default to
By default, this method returns
null for all statements, except for
BuiltStatement- value will be inferred from the query: if it updates counters, prepends/appends to a list, or uses a function call or
QueryBuilder.raw(String)anywhere in an inserted value, the result will be
false; otherwise it will be
Statement.setIdempotent(boolean)forces a value that overrides calculated value.
Note that when a statement is prepared (
Session.prepare(String)), its idempotence
flag will be propagated to all
PreparedStatements created from it.
Copyright © 2012–2019. All rights reserved.