|
|
高大的日记本
3 年前 |
public interface Session extends Closeable
newSimpleStatement
(
String
query,
Object
... values)
PreparedStatement
prepare
(
RegularStatement
statement)
PreparedStatement
prepare
(
String
query)
com.google.common.util.concurrent.ListenableFuture<
PreparedStatement
>
prepareAsync
(
RegularStatement
statement)
com.google.common.util.concurrent.ListenableFuture<
PreparedStatement
>
prepareAsync
(
String
query)
String getLoggedKeyspace()
Cluster.connect(String)
, or to the
last keyspace logged into through a "USE" CQL query if one was used.
null
if the session is logged to no keyspace.
Session init()
Cluster.connect()
method
Cluster
to create your Session, the returned session will be already
initialized. Even if you create a non-initialized session through
Cluster.newSession()
, that session will get automatically
initialized the first time that session is used for querying. This method
is thus only useful if you use
Cluster.newSession()
and want to
explicitly force initialization without querying.
Session initialization consists in connecting the Session to the known
Cassandra hosts (at least those that should not be ignored due to
the
LoadBalancingPolicy
in place).
If the Cluster instance this Session depends on is not itself
initialized, it will be initialized by this method.
If the session is already initialized, this method is a no-op.
Session
object.
NoHostAvailableException
- if this initialization triggers the
Cluster initialization and no host amongst the contact points can be
reached.
AuthenticationException
- if this initialization triggers the
Cluster initialization and an authentication error occurs while contacting
the initial contact points.
execute(new SimpleStatement(query))
.
query
- the CQL query to execute.
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to execute this query.
QueryExecutionException
- if the query triggered an execution
exception, i.e. an exception thrown by Cassandra when it cannot execute
the query with the requested consistency level successfully.
QueryValidationException
- if the query if invalid (syntax error,
unauthorized or any other validation problem).
ResultSet execute(String query, Object... values)
execute(new SimpleStatement(query, values))
.
query
- the CQL query to execute.
values
- values required for the execution of
query
. See
newSimpleStatement(String, Object...)
for more detail.
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to execute this query.
QueryExecutionException
- if the query triggered an execution
exception, i.e. an exception thrown by Cassandra when it cannot execute
the query with the requested consistency level successfully.
QueryValidationException
- if the query if invalid (syntax error,
unauthorized or any other validation problem).
UnsupportedFeatureException
- 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).
statement
- the CQL query to execute (that can be any
Statement
).
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to execute this query.
QueryExecutionException
- if the query triggered an execution
exception, i.e. an exception thrown by Cassandra when it cannot execute
the query with the requested consistency level successfully.
QueryValidationException
- if the query if invalid (syntax error,
unauthorized or any other validation problem).
UnsupportedFeatureException
- if the protocol version 1 is in use and
a feature not supported has been used. Features that are not supported by
the version protocol 1 include: BatchStatement, ResultSet paging and binary
values in RegularStatement.
ResultSetFuture executeAsync(String query)
executeAsync(new SimpleStatement(query))
.
query
- the CQL query to execute.
ResultSetFuture executeAsync(String query, Object... values)
executeAsync(new SimpleStatement(query, values))
.
query
- the CQL query to execute.
values
- values required for the execution of
query
. See
newSimpleStatement(String, Object...)
for more detail.
UnsupportedFeatureException
- 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).
SimpleStatement newSimpleStatement(String query)
execute(Statement)
.
query
- the CQL query to execute.
SimpleStatement newSimpleStatement(String query, Object... values)
execute(Statement)
.
Parameterized simple statements are useful when you want to execute a
query only once (and thus do not want to resort to prepared statements), but
do not want to convert all column values to string (typically, if you have blob
values, encoding them to a hexadecimal string is not very efficient). In
that case, you can provide a query string with bind marker to this method,
along with the values for those bind variables. When executed, the server will
prepare the provided query, bind the provided values to that prepare statement and
execute the resulting statement. Thus,
session.execute(session.newSimpleStatement(query, value1, value2, value3));
is functionally equivalent to
PreparedStatement ps = session.prepare(query);
session.execute(ps.bind(value1, value2, value3));
except that the former version:
values
provided to this method will
not be validated by the driver as is done by
BoundStatement.bind(java.lang.Object...)
since
query
is not parsed (and hence the driver cannot know what those value
should be). The codec to serialize each value will be chosen in the codec registry
associated with this session's cluster, based on the value's Java type
(this is the equivalent to calling
CodecRegistry.codecFor(Object)
).
If too many or too few values are provided, or if a value is not a valid one for
the variable it is bound to, an
InvalidQueryException
will be thrown
by Cassandra at execution time. A
CodecNotFoundException
may be
thrown by this constructor however, if the codec registry does not know how to
handle one of the values.
query
- the CQL query to execute.
values
- values required for the execution of
query
.
ResultSetFuture executeAsync(Statement statement)
ResultSetFuture
.
Note that for queries that doesn't return a result (INSERT, UPDATE and
DELETE), you will need to access the ResultSetFuture (that is call one of
its get method to make sure the query was successful.
statement
- the CQL query to execute (that can be either any
Statement
.
UnsupportedFeatureException
- if the protocol version 1 is in use and
a feature not supported has been used. Features that are not supported by
the version protocol 1 include: BatchStatement, ResultSet paging and binary
values in RegularStatement.
query
- the CQL query string to prepare
query
.
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to prepare this query.
PreparedStatement prepare(RegularStatement statement)
prepare(String)
,
but note that the resulting
PreparedStatement
will inherit the query properties
set on
statement
. Concretely, this means that in the following code:
RegularStatement toPrepare = new SimpleStatement("SELECT * FROM test WHERE k=?").setConsistencyLevel(ConsistencyLevel.QUORUM);
PreparedStatement prepared = session.prepare(toPrepare);
session.execute(prepared.bind("someValue"));
the final execution will be performed with Quorum consistency.
Please note that if the same CQL statement is prepared more than once, all
calls to this method will return the same
PreparedStatement
object
but the method will still apply the properties of the prepared
Statement
to this object.
statement
- the statement to prepare
statement
.
NoHostAvailableException
- if no host in the cluster can be
contacted successfully to prepare this statement.
IllegalArgumentException
- if
statement.getValues() != null
(values for executing a prepared statement should be provided after preparation
though the
PreparedStatement.bind(java.lang.Object...)
method or through a corresponding
BoundStatement
).
com.google.common.util.concurrent.ListenableFuture<PreparedStatement> prepareAsync(String query)
prepare(String)
except that it
does not block but return a future instead. Any error during preparation will
be thrown when accessing the future, not by this method itself.
query
- the CQL query string to prepare
query
.
com.google.common.util.concurrent.ListenableFuture<PreparedStatement> prepareAsync(RegularStatement statement)
prepareAsync(String)
,
but note that the resulting
PreparedStatement
will inherit the query properties
set on
statement
. Concretely, this means that in the following code:
RegularStatement toPrepare = new SimpleStatement("SELECT * FROM test WHERE k=?").setConsistencyLevel(ConsistencyLevel.QUORUM);
PreparedStatement prepared = session.prepare(toPrepare);
session.execute(prepared.bind("someValue"));
the final execution will be performed with Quorum consistency.
Please note that if the same CQL statement is prepared more than once, all
calls to this method will return the same
PreparedStatement
object
but the method will still apply the properties of the prepared
Statement
to this object.
statement
- the statement to prepare
statement
.
IllegalArgumentException
- if
statement.getValues() != null
(values for executing a prepared statement should be provided after preparation
though the
PreparedStatement.bind(java.lang.Object...)
method or through a corresponding
BoundStatement
).
prepare(RegularStatement)
CloseFuture.force()
can be called on the result future.
This method has no particular effect if the session was already closed
(in which case the returned future will return immediately).
Note that this method does not close the corresponding
Cluster
instance (which holds additional resources, in particular internal
executors that must be shut down in order for the client program to
terminate).
If you want to do so, use
Cluster.close()
, but note that it will
close all sessions created from that cluster.
void close()
closeAsync().get()
.
Note that this method does not close the corresponding
Cluster
instance (which holds additional resources, in particular internal
executors that must be shut down in order for the client program to
terminate).
If you want to do so, use
Cluster.close()
, but note that it will
close all sessions created from that cluster.
close
in interface
AutoCloseable
close
in interface
Closeable
close()
and wait until it returns (or call the get method on
closeAsync()
with a very short timeout and check this doesn't timeout).
true
if this Session instance has been closed,
false
otherwise.