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:
-
InitializingBeanCqlSession
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidEnsures the CassandraCqlSessionand 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.ConsistencyLevelReturn the exception translator used by this template to translate Cassandra specific Exceptions into Spring DAO's Exception Hierarchy.intDeprecated.com.datastax.oss.driver.api.core.CqlIdentifierintcom.datastax.oss.driver.api.core.ConsistencyLevelcom.datastax.oss.driver.api.core.CqlSessionDeprecated.since 2.0.Returns the CassandraSessionFactoryused by this template to perform Cassandra data access operations.Create a new RowMapper for reading columns as key-value pairs.protected PreparedStatementBindernewPreparedStatementBinder(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.SimpleStatementnewStatement(String cql) Create aSimpleStatementgivencql.voidsetConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel consistencyLevel) Set the consistency level for this template.voidsetExceptionTranslator(CqlExceptionTranslator exceptionTranslator) Sets the exception translator used by this template to translate Cassandra specific Exceptions into Spring DAO's Exception Hierarchy.voidsetExecutionProfile(String profileName) Set the driver execution profile for this template.voidsetExecutionProfileResolver(ExecutionProfileResolver executionProfileResolver) Set theExecutionProfileResolverfor this template.voidsetFetchSize(int fetchSize) Deprecated.since 3.0, usesetPageSize(int)voidsetKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier keyspace) Set thekeyspaceto be applied on statement-level for this template.voidsetPageSize(int fetchSize) Set the page size for this template.voidsetSerialConsistencyLevel(com.datastax.oss.driver.api.core.ConsistencyLevel consistencyLevel) Set the serial consistency level for this template.voidsetSession(com.datastax.oss.driver.api.core.CqlSession session) Sets the CassandraCqlSessionused by this template to perform Cassandra data access operations.voidsetSessionFactory(SessionFactory sessionFactory) Sets the CassandraSessionFactoryused by this template to perform Cassandra data access operations.protected static StringDetermine CQL from potential provider object.protected DataAccessExceptiontranslate(String task, String cql, RuntimeException ex) Translate the givenRuntimeExceptioninto a genericDataAccessException.protected DataAccessExceptionTranslate the givenRuntimeExceptioninto a genericDataAccessException.
-
Field Details
-
logger
Logger available to subclasses
-
-
Constructor Details
-
CassandraAccessor
public CassandraAccessor()
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()Ensures the CassandraCqlSessionand exception translator has been propertly set.- Specified by:
afterPropertiesSetin 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
ConsistencyLevelspecified 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 theExecutionProfileResolverfor this template.- Since:
- 3.0
- See Also:
-
DriverExecutionProfileExecutionProfileResolver
-
getExecutionProfileResolver
- Returns:
- the
ExecutionProfileResolverspecified 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 thekeyspaceto be applied on statement-level for this template. If not set, the defaultCqlSessionkeyspace 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
keyspaceto 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
ConsistencyLevelspecified for this template. - Since:
- 3.0
-
setSession
public void setSession(com.datastax.oss.driver.api.core.CqlSession session) Sets the CassandraCqlSessionused by this template to perform Cassandra data access operations. Thesessionwill replace the currentgetSessionFactory()withDefaultSessionFactory.- Parameters:
session- CassandraCqlSessionused by this template, must not benull.- See Also:
-
CqlSessionDefaultSessionFactory
-
getSession
Deprecated.since 2.0. This class uses aSessionFactoryto dispatch CQL calls amongst differentCqlSessions during its lifecycle.Returns the CassandraCqlSessionfromSessionFactoryused by this template to perform Cassandra data access operations.- Returns:
- the Cassandra
CqlSessionused by this template. - See Also:
-
CqlSession
-
setSessionFactory
Sets the CassandraSessionFactoryused by this template to perform Cassandra data access operations.- Parameters:
sessionFactory- CassandraCqlSessionused by this template. Must not benull.- Since:
- 2.0
- See Also:
-
CqlSession
-
getSessionFactory
Returns the CassandraSessionFactoryused by this template to perform Cassandra data access operations.- Returns:
- the Cassandra
SessionFactoryused by this template. - Since:
- 2.0
- See Also:
-
newStatement
Create aSimpleStatementgivencql.- Parameters:
cql- the CQL query.- Returns:
- the
SimpleStatementto 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 givenRuntimeExceptioninto a genericDataAccessException.The returned
DataAccessExceptionis supposed to contain the originalRuntimeExceptionas root cause. However, client code may not generally rely on this due toDataAccessExceptions possibly being caused by other resource APIs as well. That said, agetRootCause() instanceof DataAccessExceptioncheck (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 givenRuntimeExceptioninto a genericDataAccessException.The returned
DataAccessExceptionis supposed to contain the originalRuntimeExceptionas root cause. However, client code may not generally rely on this due toDataAccessExceptions possibly being caused by other resource APIs as well. That said, agetRootCause() instanceof DataAccessExceptioncheck (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
PreparedStatementBinderto 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().