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 CqlSession.
InitializingBean,
CqlSession| 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
CqlSession 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 |
getConsistencyLevel() |
CqlExceptionTranslator |
getExceptionTranslator()
Return the exception translator used by this template to translate Cassandra specific Exceptions into Spring DAO's
Exception Hierarchy.
|
ExecutionProfileResolver |
getExecutionProfileResolver() |
int |
getFetchSize()
Deprecated.
since 3.0, use
getPageSize(). |
com.datastax.oss.driver.api.core.CqlIdentifier |
getKeyspace() |
int |
getPageSize() |
com.datastax.oss.driver.api.core.ConsistencyLevel |
getSerialConsistencyLevel() |
com.datastax.oss.driver.api.core.CqlSession |
getSession()
Deprecated.
since 2.0. This class uses a
SessionFactory to dispatch CQL calls amongst different
CqlSessions 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 <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 a
SimpleStatement given cql. |
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 the
ExecutionProfileResolver for this template. |
void |
setFetchSize(int fetchSize)
Deprecated.
since 3.0, use
setPageSize(int) |
void |
setKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier keyspace)
Set the
keyspace 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 Cassandra
CqlSession 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,
RuntimeException ex)
Translate the given
RuntimeException into a generic DataAccessException. |
protected DataAccessException |
translateExceptionIfPossible(RuntimeException ex)
Translate the given
RuntimeException into a generic DataAccessException. |
public void afterPropertiesSet()
CqlSession and exception translator has been propertly set.afterPropertiesSet in interface InitializingBeanpublic void setConsistencyLevel(@Nullable com.datastax.oss.driver.api.core.ConsistencyLevel consistencyLevel)
Statement.setConsistencyLevel(ConsistencyLevel),
RetryPolicy@Nullable public com.datastax.oss.driver.api.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 setExecutionProfile(String profileName)
Statement.setExecutionProfileName(String),
ExecutionProfileResolverpublic void setExecutionProfileResolver(ExecutionProfileResolver executionProfileResolver)
ExecutionProfileResolver for this template.DriverExecutionProfile,
ExecutionProfileResolverpublic ExecutionProfileResolver getExecutionProfileResolver()
ExecutionProfileResolver specified for this template.@Deprecated public void setFetchSize(int fetchSize)
setPageSize(int)StatementBuilder.setPageSize(int)@Deprecated public int getFetchSize()
getPageSize().public void setKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier keyspace)
keyspace to be applied on statement-level for this template. If not set, the default
CqlSession keyspace will be used.keyspace - the keyspace to apply, must not be null.SimpleStatement.setKeyspace(CqlIdentifier),
BatchStatement.setKeyspace(CqlIdentifier)@Nullable public com.datastax.oss.driver.api.core.CqlIdentifier getKeyspace()
keyspace to be applied on statement-level for this template.public void setPageSize(int fetchSize)
StatementBuilder.setPageSize(int)public int getPageSize()
public void setSerialConsistencyLevel(@Nullable com.datastax.oss.driver.api.core.ConsistencyLevel consistencyLevel)
Statement.setSerialConsistencyLevel(ConsistencyLevel),
ConsistencyLevel@Nullable public com.datastax.oss.driver.api.core.ConsistencyLevel getSerialConsistencyLevel()
ConsistencyLevel specified for this template.public void setSession(com.datastax.oss.driver.api.core.CqlSession session)
CqlSession used by this template to perform Cassandra data access operations. The
session will replace the current getSessionFactory() with DefaultSessionFactory.session - Cassandra CqlSession used by this template, must not benull.CqlSession,
DefaultSessionFactory@Deprecated public com.datastax.oss.driver.api.core.CqlSession getSession()
SessionFactory to dispatch CQL calls amongst different
CqlSessions during its lifecycle.CqlSession from SessionFactory used by this template to perform Cassandra
data access operations.CqlSession used by this template.CqlSessionpublic void setSessionFactory(SessionFactory sessionFactory)
SessionFactory used by this template to perform Cassandra data access operations.sessionFactory - Cassandra CqlSession used by this template. Must not benull.CqlSession@Nullable public SessionFactory getSessionFactory()
SessionFactory used by this template to perform Cassandra data access operations.SessionFactory used by this template.SessionFactoryprotected com.datastax.oss.driver.api.core.cql.SimpleStatement newStatement(String cql)
SimpleStatement given cql.cql - the CQL query.SimpleStatement to use.protected com.datastax.oss.driver.api.core.cql.Statement<?> applyStatementSettings(com.datastax.oss.driver.api.core.cql.Statement<?> statement)
statement - the CQL Statement to preparesetConsistencyLevel(ConsistencyLevel),
setSerialConsistencyLevel(ConsistencyLevel),
setPageSize(int),
setExecutionProfile(String),
setExecutionProfileResolver(ExecutionProfileResolver)@Nullable protected DataAccessException translateExceptionIfPossible(RuntimeException ex)
RuntimeException into a generic DataAccessException.
The returned DataAccessException is supposed to contain the original RuntimeException 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 RuntimeExceptionRuntimeExceptionDataAccessExceptionprotected DataAccessException translate(String task, @Nullable String cql, RuntimeException ex)
RuntimeException into a generic DataAccessException.
The returned DataAccessException is supposed to contain the original RuntimeException 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 RuntimeExceptionRuntimeExceptionNestedRuntimeException.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 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–2021 Pivotal Software, Inc.. All rights reserved.