public class CassandraAccessor extends Object implements InitializingBean
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 Session.
InitializingBean,
Session| Modifier and Type | Class and Description |
|---|---|
protected static class |
CassandraAccessor.RowCallbackHandlerResultSetExtractor
Adapter to enable use of a
RowCallbackHandler inside a ResultSetExtractor. |
| Modifier and Type | Field and Description |
|---|---|
protected org.slf4j.Logger |
logger
Logger available to subclasses
|
| Constructor and Description |
|---|
CassandraAccessor() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterPropertiesSet()
Ensures the Cassandra
Session and exception translator has been propertly set. |
protected <T extends com.datastax.driver.core.PreparedStatement> |
applyStatementSettings(T statement)
Prepare the given CQL Statement (or
PreparedStatement), applying statement
settings such as retry policy and consistency level. |
protected <T extends com.datastax.driver.core.Statement> |
applyStatementSettings(T statement)
Prepare the given CQL Statement (or
PreparedStatement), applying statement
settings such as fetch size, retry policy, and consistency level. |
com.datastax.driver.core.ConsistencyLevel |
getConsistencyLevel() |
CqlExceptionTranslator |
getExceptionTranslator()
Return the exception translator used by this template to translate Cassandra specific Exceptions into Spring DAO's
Exception Hierarchy.
|
int |
getFetchSize() |
com.datastax.driver.core.policies.RetryPolicy |
getRetryPolicy() |
com.datastax.driver.core.Session |
getSession()
Deprecated.
since 2.0. This class uses a
SessionFactory to dispatch CQL calls amongst different
Sessions during its lifecycle. |
SessionFactory |
getSessionFactory()
Returns the Cassandra
SessionFactory used by this template to perform Cassandra data access operations. |
protected RowMapper<Map<String,Object>> |
newColumnMapRowMapper()
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 CassandraAccessor.RowCallbackHandlerResultSetExtractor |
newResultSetExtractor(RowCallbackHandler rowCallbackHandler)
Constructs a new instance of the
ResultSetExtractor initialized with and adapting the given
RowCallbackHandler. |
protected <T> RowMapperResultSetExtractor<T> |
newResultSetExtractor(RowMapper<T> rowMapper)
Constructs a new instance of the
ResultSetExtractor initialized with and adapting the given
RowMapper. |
protected <T> RowMapperResultSetExtractor<T> |
newResultSetExtractor(RowMapper<T> rowMapper,
int rowsExpected)
Constructs a new instance of the
ResultSetExtractor initialized with and adapting the given
RowMapper. |
protected <T> RowMapper<T> |
newSingleColumnRowMapper(Class<T> requiredType)
Create a new RowMapper for reading result objects from a single column.
|
void |
setConsistencyLevel(com.datastax.driver.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 |
setFetchSize(int fetchSize)
Set the fetch size for this template.
|
void |
setRetryPolicy(com.datastax.driver.core.policies.RetryPolicy retryPolicy)
Set the retry policy for this template.
|
void |
setSession(com.datastax.driver.core.Session session)
Sets the Cassandra
Session used by this template to perform Cassandra data access operations. |
void |
setSessionFactory(SessionFactory sessionFactory)
Sets the Cassandra
SessionFactory used by this template to perform Cassandra data access operations. |
protected static String |
toCql(Object cqlProvider)
Determine CQL from potential provider object.
|
protected DataAccessException |
translate(String task,
String cql,
com.datastax.driver.core.exceptions.DriverException ex)
Translate the given
DriverException into a generic DataAccessException. |
protected DataAccessException |
translateExceptionIfPossible(com.datastax.driver.core.exceptions.DriverException ex)
Translate the given
DriverException into a generic DataAccessException. |
public void afterPropertiesSet()
Session and exception translator has been propertly set.afterPropertiesSet in interface InitializingBeanpublic void setConsistencyLevel(@Nullable com.datastax.driver.core.ConsistencyLevel consistencyLevel)
Statement.setConsistencyLevel(ConsistencyLevel),
RetryPolicy@Nullable public com.datastax.driver.core.ConsistencyLevel getConsistencyLevel()
ConsistencyLevel specified for this template.public void setExceptionTranslator(CqlExceptionTranslator exceptionTranslator)
exceptionTranslator - exception translator to set; must not be null.CqlExceptionTranslatorpublic CqlExceptionTranslator getExceptionTranslator()
CqlExceptionTranslatorpublic void setFetchSize(int fetchSize)
Statement.setFetchSize(int)public int getFetchSize()
public void setRetryPolicy(@Nullable com.datastax.driver.core.policies.RetryPolicy retryPolicy)
Statement.setRetryPolicy(RetryPolicy),
RetryPolicy@Nullable public com.datastax.driver.core.policies.RetryPolicy getRetryPolicy()
RetryPolicy specified for this template.public void setSession(com.datastax.driver.core.Session session)
Session used by this template to perform Cassandra data access operations. The
session will replace the current getSessionFactory() with DefaultSessionFactory.session - Cassandra Session used by this template, must not benull.Session,
DefaultSessionFactory@Deprecated public com.datastax.driver.core.Session getSession()
SessionFactory to dispatch CQL calls amongst different
Sessions during its lifecycle.Session from SessionFactory used by this template to perform Cassandra data
access operations.Session used by this template.Sessionpublic void setSessionFactory(SessionFactory sessionFactory)
SessionFactory used by this template to perform Cassandra data access operations.sessionFactory - Cassandra Session used by this template. Must not benull.Session@Nullable public SessionFactory getSessionFactory()
SessionFactory used by this template to perform Cassandra data access operations.SessionFactory used by this template.SessionFactoryprotected <T extends com.datastax.driver.core.PreparedStatement> T applyStatementSettings(T statement)
PreparedStatement), applying statement
settings such as retry policy and consistency level.statement - the CQL Statement to preparesetRetryPolicy(RetryPolicy),
setConsistencyLevel(ConsistencyLevel)protected <T extends com.datastax.driver.core.Statement> T applyStatementSettings(T statement)
PreparedStatement), applying statement
settings such as fetch size, retry policy, and consistency level.statement - the CQL Statement to preparesetFetchSize(int),
setRetryPolicy(RetryPolicy),
setConsistencyLevel(ConsistencyLevel)@Nullable protected DataAccessException translateExceptionIfPossible(com.datastax.driver.core.exceptions.DriverException ex)
DriverException into a generic DataAccessException.
The returned DataAccessException is supposed to contain the original DriverException as root cause.
However, client code may not generally rely on this due to DataAccessExceptions possibly being caused by
other resource APIs as well. That said, a getRootCause() instanceof DataAccessException check (and
subsequent cast) is considered reliable when expecting Cassandra-based access to have happened.
ex - the offending DriverExceptionDriverExceptionDataAccessExceptionprotected DataAccessException translate(String task, @Nullable String cql, com.datastax.driver.core.exceptions.DriverException ex)
DriverException into a generic DataAccessException.
The returned DataAccessException is supposed to contain the original DriverException as root cause.
However, client code may not generally rely on this due to DataAccessExceptions possibly being caused by
other resource APIs as well. That said, a getRootCause() instanceof DataAccessException check (and
subsequent cast) is considered reliable when expecting Cassandra-based access to have happened.
task - readable text describing the task being attemptedcql - CQL query or update that caused the problem (may be null)ex - the offending DriverExceptionDriverExceptionNestedRuntimeException.getRootCause(),
Consistent
exception hierarchyprotected PreparedStatementBinder newPreparedStatementBinder(Object[] args)
ArgumentPreparedStatementBinder. This method allows for the creation to be overridden by subclasses.args - object array with argumentsPreparedStatementBinder to useprotected CassandraAccessor.RowCallbackHandlerResultSetExtractor newResultSetExtractor(RowCallbackHandler rowCallbackHandler)
ResultSetExtractor initialized with and adapting the given
RowCallbackHandler.rowCallbackHandler - RowCallbackHandler to adapt as a ResultSetExtractor.ResultSetExtractor implementation adapting an instance of the RowCallbackHandler.AsyncCqlTemplate.RowCallbackHandlerResultSetExtractor,
ResultSetExtractor,
RowCallbackHandlerprotected <T> RowMapperResultSetExtractor<T> newResultSetExtractor(RowMapper<T> rowMapper)
ResultSetExtractor initialized with and adapting the given
RowMapper.rowMapper - RowMapper to adapt as a ResultSetExtractor.ResultSetExtractor implementation adapting an instance of the RowMapper.ResultSetExtractor,
RowMapper,
RowMapperResultSetExtractorprotected <T> RowMapperResultSetExtractor<T> newResultSetExtractor(RowMapper<T> rowMapper, int rowsExpected)
ResultSetExtractor initialized with and adapting the given
RowMapper.rowMapper - RowMapper to adapt as a ResultSetExtractor.rowsExpected - number of expected rows in the ResultSet.ResultSetExtractor implementation adapting an instance of the RowMapper.ResultSetExtractor,
RowMapper,
RowMapperResultSetExtractorprotected RowMapper<Map<String,Object>> newColumnMapRowMapper()
ColumnMapRowMapperprotected <T> RowMapper<T> newSingleColumnRowMapper(Class<T> requiredType)
requiredType - the type that each result object is expected to matchSingleColumnRowMapper@Nullable protected static String toCql(@Nullable Object cqlProvider)
cqlProvider - object that's potentially a CqlProviderCqlProviderCopyright © 2011–2018 Pivotal Software, Inc.. All rights reserved.