Class CassandraAccessor

java.lang.Object
org.springframework.data.cassandra.core.cql.CassandraAccessor
All Implemented Interfaces:
InitializingBean
Direct Known Subclasses:
AsyncCqlTemplate, AsyncCqlTemplate, CqlTemplate

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.

Author:
David Webb, Mark Paluch, John Blum, Tomasz Lelek
See Also:
  • Field Details

    • logger

      protected final Log logger
      Logger available to subclasses
  • Constructor Details

    • CassandraAccessor

      public CassandraAccessor()
  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Ensures the Cassandra CqlSession and exception translator has been propertly set.
      Specified by:
      afterPropertiesSet in interface InitializingBean
    • 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

      @Nullable public com.datastax.oss.driver.api.core.ConsistencyLevel getConsistencyLevel()
      Returns:
      the ConsistencyLevel specified for this template.
    • setExceptionTranslator

      public void setExceptionTranslator(CqlExceptionTranslator exceptionTranslator)
      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

      public CqlExceptionTranslator 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

      public void setExecutionProfile(String profileName)
      Set the driver execution profile for this template.
      Since:
      3.0
      See Also:
    • setExecutionProfileResolver

      public void setExecutionProfileResolver(ExecutionProfileResolver executionProfileResolver)
      Set the ExecutionProfileResolver for this template.
      Since:
      3.0
      See Also:
    • getExecutionProfileResolver

      public ExecutionProfileResolver getExecutionProfileResolver()
      Returns:
      the ExecutionProfileResolver specified for this template.
      Since:
      3.0
    • setFetchSize

      @Deprecated public void setFetchSize(int fetchSize)
      Deprecated.
      since 3.0, use setPageSize(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 public int getFetchSize()
      Deprecated.
      since 3.0, use getPageSize().
      Returns:
      the fetch size specified for this template.
    • setKeyspace

      public void setKeyspace(com.datastax.oss.driver.api.core.CqlIdentifier keyspace)
      Set the keyspace to be applied on statement-level for this template. If not set, the default CqlSession 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

      @Nullable public com.datastax.oss.driver.api.core.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

      @Nullable public com.datastax.oss.driver.api.core.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 Cassandra CqlSession used by this template to perform Cassandra data access operations. The session will replace the current getSessionFactory() with DefaultSessionFactory.
      Parameters:
      session - Cassandra CqlSession used by this template, must not benull.
      See Also:
    • getSession

      @Deprecated public 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.
      Returns the Cassandra CqlSession from SessionFactory used by this template to perform Cassandra data access operations.
      Returns:
      the Cassandra CqlSession used by this template.
      See Also:
      • CqlSession
    • setSessionFactory

      public void setSessionFactory(SessionFactory sessionFactory)
      Sets the Cassandra SessionFactory used by this template to perform Cassandra data access operations.
      Parameters:
      sessionFactory - Cassandra CqlSession used by this template. Must not benull.
      Since:
      2.0
      See Also:
      • CqlSession
    • getSessionFactory

      @Nullable public SessionFactory getSessionFactory()
      Returns the Cassandra SessionFactory used by this template to perform Cassandra data access operations.
      Returns:
      the Cassandra SessionFactory used by this template.
      Since:
      2.0
      See Also:
    • newStatement

      protected com.datastax.oss.driver.api.core.cql.SimpleStatement newStatement(String cql)
      Create a SimpleStatement given cql.
      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

      @Nullable protected DataAccessException translateExceptionIfPossible(RuntimeException ex)
      Translate the given 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.

      Parameters:
      ex - the offending RuntimeException
      Returns:
      the DataAccessException, wrapping the RuntimeException
      See Also:
    • translate

      protected DataAccessException translate(String task, @Nullable String cql, RuntimeException ex)
      Translate the given 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.

      Parameters:
      task - readable text describing the task being attempted
      cql - CQL query or update that caused the problem (may be null)
      ex - the offending RuntimeException
      Returns:
      the DataAccessException, wrapping the RuntimeException
      See Also:
    • newPreparedStatementBinder

      protected PreparedStatementBinder newPreparedStatementBinder(Object[] args)
      Create a new arg-based PreparedStatementSetter using the args passed in. By default, we'll create an ArgumentPreparedStatementBinder. This method allows for the creation to be overridden by subclasses.
      Parameters:
      args - object array with arguments
      Returns:
      the new PreparedStatementBinder to use
    • newColumnMapRowMapper

      protected RowMapper<Map<String,Object>> newColumnMapRowMapper()
      Create a new RowMapper for reading columns as key-value pairs.
      Returns:
      the RowMapper to use
      See Also:
    • newSingleColumnRowMapper

      protected <T> RowMapper<T> newSingleColumnRowMapper(Class<T> requiredType)
      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

      @Nullable protected static String toCql(@Nullable Object cqlProvider)
      Determine CQL from potential provider object.
      Parameters:
      cqlProvider - object that's potentially a CqlProvider
      Returns:
      the CQL string, or null
      See Also: