Class CassandraAccessor
- All Implemented Interfaces:
InitializingBean
- Direct Known Subclasses:
AsyncCqlTemplate
,AsyncCqlTemplate
,CqlTemplate
CassandraAccessor
provides access to a Cassandra SessionFactory
and the
CassandraExceptionTranslator
.
Classes providing a higher abstraction level usually extend CassandraAccessor
to provide a richer set of
functionality on top of a SessionFactory
using CqlSession
.
- Author:
- David Webb, Mark Paluch, John Blum, Tomasz Lelek
- See Also:
-
InitializingBean
CqlSession
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Ensures the CassandraCqlSession
and exception translator has been propertly set.protected com.datastax.oss.driver.api.core.cql.Statement<?>
applyStatementSettings
(com.datastax.oss.driver.api.core.cql.Statement<?> statement) Prepare the given CQL Statement applying statement settings such as page size and consistency level.com.datastax.oss.driver.api.core.ConsistencyLevel
Return the exception translator used by this template to translate Cassandra specific Exceptions into Spring DAO's Exception Hierarchy.int
Deprecated.com.datastax.oss.driver.api.core.CqlIdentifier
int
com.datastax.oss.driver.api.core.ConsistencyLevel
com.datastax.oss.driver.api.core.CqlSession
Deprecated.since 2.0.Returns the CassandraSessionFactory
used by this template to perform Cassandra data access operations.Create a new RowMapper for reading columns as key-value pairs.protected PreparedStatementBinder
newPreparedStatementBinder
(Object[] args) Create a new arg-based PreparedStatementSetter using the args passed in.protected <T> RowMapper<T>
newSingleColumnRowMapper
(Class<T> requiredType) Create a new RowMapper for reading result objects from a single column.protected com.datastax.oss.driver.api.core.cql.SimpleStatement
newStatement
(String cql) Create aSimpleStatement
givencql
.void
setConsistencyLevel
(com.datastax.oss.driver.api.core.ConsistencyLevel consistencyLevel) Set the consistency level for this template.void
setExceptionTranslator
(CqlExceptionTranslator exceptionTranslator) Sets the exception translator used by this template to translate Cassandra specific Exceptions into Spring DAO's Exception Hierarchy.void
setExecutionProfile
(String profileName) Set the driver execution profile for this template.void
setExecutionProfileResolver
(ExecutionProfileResolver executionProfileResolver) Set theExecutionProfileResolver
for this template.void
setFetchSize
(int fetchSize) Deprecated.since 3.0, usesetPageSize(int)
void
setKeyspace
(com.datastax.oss.driver.api.core.CqlIdentifier keyspace) Set thekeyspace
to be applied on statement-level for this template.void
setPageSize
(int fetchSize) Set the page size for this template.void
setSerialConsistencyLevel
(com.datastax.oss.driver.api.core.ConsistencyLevel consistencyLevel) Set the serial consistency level for this template.void
setSession
(com.datastax.oss.driver.api.core.CqlSession session) Sets the CassandraCqlSession
used by this template to perform Cassandra data access operations.void
setSessionFactory
(SessionFactory sessionFactory) Sets the CassandraSessionFactory
used by this template to perform Cassandra data access operations.protected static String
Determine CQL from potential provider object.protected DataAccessException
translate
(String task, String cql, RuntimeException ex) Translate the givenRuntimeException
into a genericDataAccessException
.protected DataAccessException
Translate the givenRuntimeException
into a genericDataAccessException
.
-
Field Details
-
logger
Logger available to subclasses
-
-
Constructor Details
-
CassandraAccessor
public CassandraAccessor()
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()Ensures the CassandraCqlSession
and exception translator has been propertly set.- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
setConsistencyLevel
public void setConsistencyLevel(@Nullable com.datastax.oss.driver.api.core.ConsistencyLevel consistencyLevel) Set the consistency level for this template. Consistency level defines the number of nodes involved into query processing. Relaxed consistency level settings use fewer nodes but eventual consistency is more likely to occur while a higher consistency level involves more nodes to obtain results with a higher consistency guarantee.- See Also:
-
Statement.setConsistencyLevel(ConsistencyLevel)
RetryPolicy
-
getConsistencyLevel
- Returns:
- the
ConsistencyLevel
specified for this template.
-
setExceptionTranslator
Sets the exception translator used by this template to translate Cassandra specific Exceptions into Spring DAO's Exception Hierarchy.- Parameters:
exceptionTranslator
- exception translator to set; must not be null.- See Also:
-
getExceptionTranslator
Return the exception translator used by this template to translate Cassandra specific Exceptions into Spring DAO's Exception Hierarchy.- Returns:
- the Cassandra exception translator.
- See Also:
-
setExecutionProfile
Set the driver execution profile for this template.- Since:
- 3.0
- See Also:
-
Statement.setExecutionProfileName(String)
ExecutionProfileResolver
-
setExecutionProfileResolver
Set theExecutionProfileResolver
for this template.- Since:
- 3.0
- See Also:
-
DriverExecutionProfile
ExecutionProfileResolver
-
getExecutionProfileResolver
- Returns:
- the
ExecutionProfileResolver
specified for this template. - Since:
- 3.0
-
setFetchSize
Deprecated.since 3.0, usesetPageSize(int)
Set the fetch size for this template. This is important for processing large result sets: Setting this higher than the default value will increase processing speed at the cost of memory consumption; setting this lower can avoid transferring row data that will never be read by the application. Default is -1, indicating to use the CQL driver's default configuration (i.e. to not pass a specific fetch size setting on to the driver).- See Also:
-
StatementBuilder.setPageSize(int)
-
getFetchSize
Deprecated.since 3.0, usegetPageSize()
.- Returns:
- the fetch size specified for this template.
-
setKeyspace
public void setKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier keyspace) Set thekeyspace
to be applied on statement-level for this template. If not set, the defaultCqlSession
keyspace will be used.- Parameters:
keyspace
- the keyspace to apply, must not be null.- Since:
- 3.1
- See Also:
-
SimpleStatement.setKeyspace(CqlIdentifier)
BatchStatement.setKeyspace(CqlIdentifier)
-
getKeyspace
- Returns:
- the
keyspace
to be applied on statement-level for this template. - Since:
- 3.1
-
setPageSize
public void setPageSize(int fetchSize) Set the page size for this template. This is important for processing large result sets: Setting this higher than the default value will increase processing speed at the cost of memory consumption; setting this lower can avoid transferring row data that will never be read by the application. Default is -1, indicating to use the CQL driver's default configuration (i.e. to not pass a specific page size setting on to the driver).- See Also:
-
StatementBuilder.setPageSize(int)
-
getPageSize
public int getPageSize()- Returns:
- the page size specified for this template.
-
setSerialConsistencyLevel
public void setSerialConsistencyLevel(@Nullable com.datastax.oss.driver.api.core.ConsistencyLevel consistencyLevel) Set the serial consistency level for this template.- Since:
- 3.0
- See Also:
-
Statement.setSerialConsistencyLevel(ConsistencyLevel)
ConsistencyLevel
-
getSerialConsistencyLevel
- Returns:
- the serial
ConsistencyLevel
specified for this template. - Since:
- 3.0
-
setSession
public void setSession(com.datastax.oss.driver.api.core.CqlSession session) Sets the CassandraCqlSession
used by this template to perform Cassandra data access operations. Thesession
will replace the currentgetSessionFactory()
withDefaultSessionFactory
.- Parameters:
session
- CassandraCqlSession
used by this template, must not benull.- See Also:
-
CqlSession
DefaultSessionFactory
-
getSession
Deprecated.since 2.0. This class uses aSessionFactory
to dispatch CQL calls amongst differentCqlSession
s during its lifecycle.Returns the CassandraCqlSession
fromSessionFactory
used by this template to perform Cassandra data access operations.- Returns:
- the Cassandra
CqlSession
used by this template. - See Also:
-
CqlSession
-
setSessionFactory
Sets the CassandraSessionFactory
used by this template to perform Cassandra data access operations.- Parameters:
sessionFactory
- CassandraCqlSession
used by this template. Must not benull.- Since:
- 2.0
- See Also:
-
CqlSession
-
getSessionFactory
Returns the CassandraSessionFactory
used by this template to perform Cassandra data access operations.- Returns:
- the Cassandra
SessionFactory
used by this template. - Since:
- 2.0
- See Also:
-
newStatement
Create aSimpleStatement
givencql
.- Parameters:
cql
- the CQL query.- Returns:
- the
SimpleStatement
to use. - Since:
- 3.0
-
applyStatementSettings
protected com.datastax.oss.driver.api.core.cql.Statement<?> applyStatementSettings(com.datastax.oss.driver.api.core.cql.Statement<?> statement) Prepare the given CQL Statement applying statement settings such as page size and consistency level.- Parameters:
statement
- the CQL Statement to prepare- See Also:
-
translateExceptionIfPossible
Translate the givenRuntimeException
into a genericDataAccessException
.The returned
DataAccessException
is supposed to contain the originalRuntimeException
as root cause. However, client code may not generally rely on this due toDataAccessException
s possibly being caused by other resource APIs as well. That said, agetRootCause() instanceof DataAccessException
check (and subsequent cast) is considered reliable when expecting Cassandra-based access to have happened.- Parameters:
ex
- the offendingRuntimeException
- Returns:
- the DataAccessException, wrapping the
RuntimeException
- See Also:
-
translate
Translate the givenRuntimeException
into a genericDataAccessException
.The returned
DataAccessException
is supposed to contain the originalRuntimeException
as root cause. However, client code may not generally rely on this due toDataAccessException
s possibly being caused by other resource APIs as well. That said, agetRootCause() instanceof DataAccessException
check (and subsequent cast) is considered reliable when expecting Cassandra-based access to have happened.- Parameters:
task
- readable text describing the task being attemptedcql
- CQL query or update that caused the problem (may be null)ex
- the offendingRuntimeException
- Returns:
- the DataAccessException, wrapping the
RuntimeException
- See Also:
-
newPreparedStatementBinder
Create a new arg-based PreparedStatementSetter using the args passed in. By default, we'll create anArgumentPreparedStatementBinder
. This method allows for the creation to be overridden by subclasses.- Parameters:
args
- object array with arguments- Returns:
- the new
PreparedStatementBinder
to use
-
newColumnMapRowMapper
Create a new RowMapper for reading columns as key-value pairs.- Returns:
- the RowMapper to use
- See Also:
-
newSingleColumnRowMapper
Create a new RowMapper for reading result objects from a single column.- Parameters:
requiredType
- the type that each result object is expected to match- Returns:
- the RowMapper to use
- See Also:
-
toCql
Determine CQL from potential provider object.- Parameters:
cqlProvider
- object that's potentially aCqlProvider
- Returns:
- the CQL string, or null
- See Also:
-
getPageSize()
.