public class AsyncCassandraTemplate extends Object implements AsyncCassandraOperations, ApplicationEventPublisherAware, ApplicationContextAware
AsyncCassandraOperations. It simplifies the use of asynchronous Cassandra usage and
helps to avoid common errors. It executes core Cassandra workflow. This class executes CQL queries or updates,
initiating iteration over ResultSet and catching Cassandra exceptions and translating them to the generic,
more informative exception hierarchy defined in the org.springframework.dao package.
Can be used within a service implementation via direct instantiation with a CqlSession reference, or get
prepared in an application context and given to services as bean reference.
This class supports the use of prepared statements when enabling setUsePreparedStatements(boolean). All
statements created by methods of this class (such as select(Query, Class) or
update(Query, org.springframework.data.cassandra.core.query.Update, Class) will be executed as prepared
statements. Also, statements accepted by methods (such as select(String, Class) or
and others) will be prepared prior to execution. Note that Statement
objects passed to methods must be SimpleStatement so that these can be prepared.
Note: The CqlSession should always be configured as a bean in the application context, in the first case
given to the service directly, in the second case to the prepared template.
AsyncCassandraOperations| Constructor and Description |
|---|
AsyncCassandraTemplate(AsyncCqlTemplate asyncCqlTemplate,
CassandraConverter converter)
Creates an instance of
AsyncCassandraTemplate initialized with the given AsyncCqlTemplate and
CassandraConverter. |
AsyncCassandraTemplate(com.datastax.oss.driver.api.core.CqlSession session)
Creates an instance of
AsyncCassandraTemplate initialized with the given CqlSession and a default
MappingCassandraConverter. |
AsyncCassandraTemplate(com.datastax.oss.driver.api.core.CqlSession session,
CassandraConverter converter)
Creates an instance of
AsyncCassandraTemplate initialized with the given CqlSession and
CassandraConverter. |
AsyncCassandraTemplate(SessionFactory sessionFactory,
CassandraConverter converter)
Creates an instance of
AsyncCassandraTemplate initialized with the given SessionFactory and
CassandraConverter. |
| Modifier and Type | Method and Description |
|---|---|
ListenableFuture<Long> |
count(Class<?> entityClass)
Returns the number of rows for the given entity class.
|
ListenableFuture<Long> |
count(Query query,
Class<?> entityClass)
Returns the number of rows for the given entity class applying
Query. |
ListenableFuture<WriteResult> |
delete(Object entity,
QueryOptions options)
Delete the given entity applying
QueryOptions and return the entity if the delete was applied. |
ListenableFuture<Boolean> |
delete(Query query,
Class<?> entityClass)
Remove entities (rows)/columns from the table by
Query. |
<T> ListenableFuture<T> |
delete(T entity)
Delete the given entity and return the entity if the delete was applied.
|
ListenableFuture<Boolean> |
deleteById(Object id,
Class<?> entityClass)
Remove the given object from the table by id.
|
ListenableFuture<com.datastax.oss.driver.api.core.cql.AsyncResultSet> |
execute(com.datastax.oss.driver.api.core.cql.Statement<?> statement)
Execute the a Cassandra
Statement. |
ListenableFuture<Boolean> |
exists(Object id,
Class<?> entityClass)
Determine whether a row of
entityClass with the given id exists. |
ListenableFuture<Boolean> |
exists(Query query,
Class<?> entityClass)
Determine whether the result for
entityClass Query yields at least one row. |
AsyncCqlOperations |
getAsyncCqlOperations()
Expose the underlying
AsyncCqlOperations to allow asynchronous CQL operations. |
CassandraConverter |
getConverter()
Returns the underlying
CassandraConverter. |
protected org.springframework.data.cassandra.core.EntityOperations |
getEntityOperations()
Returns the
EntityOperations used to perform data access operations on an entity inside a Cassandra data
source. |
protected SpelAwareProxyProjectionFactory |
getProjectionFactory()
Returns a reference to the configured
ProjectionFactory used by this template to process CQL query
projections. |
protected StatementFactory |
getStatementFactory()
Returns the
StatementFactory used by this template to construct and run Cassandra CQL statements. |
<T> ListenableFuture<T> |
insert(T entity)
Insert the given entity and return the entity if the insert was applied.
|
<T> ListenableFuture<EntityWriteResult<T>> |
insert(T entity,
InsertOptions options)
Insert the given entity applying
WriteOptions and return the entity if the insert was applied. |
boolean |
isUsePreparedStatements()
Returns whether this instance is configured to use
prepared statements. |
protected <T> T |
maybeCallBeforeConvert(T object,
com.datastax.oss.driver.api.core.CqlIdentifier tableName) |
protected <T> T |
maybeCallBeforeSave(T object,
com.datastax.oss.driver.api.core.CqlIdentifier tableName,
com.datastax.oss.driver.api.core.cql.Statement<?> statement) |
protected <E extends CassandraMappingEvent<T>,T> |
maybeEmitEvent(E event) |
<T> ListenableFuture<List<T>> |
select(Query query,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a List of entities. |
<T> ListenableFuture<Void> |
select(Query query,
Consumer<T> entityConsumer,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items notifying Consumer for each entity. |
<T> ListenableFuture<List<T>> |
select(com.datastax.oss.driver.api.core.cql.Statement<?> statement,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a List of entities. |
<T> ListenableFuture<Void> |
select(com.datastax.oss.driver.api.core.cql.Statement<?> statement,
Consumer<T> entityConsumer,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items notifying Consumer for each entity. |
<T> ListenableFuture<List<T>> |
select(String cql,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a List of entities. |
<T> ListenableFuture<Void> |
select(String cql,
Consumer<T> entityConsumer,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items notifying Consumer for each entity. |
<T> ListenableFuture<T> |
selectOne(Query query,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting item to an entity. |
<T> ListenableFuture<T> |
selectOne(com.datastax.oss.driver.api.core.cql.Statement<?> statement,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting item to an entity. |
<T> ListenableFuture<T> |
selectOne(String cql,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting item to an entity. |
<T> ListenableFuture<T> |
selectOneById(Object id,
Class<T> entityClass)
Execute the Select by
id for the given entityClass. |
void |
setApplicationContext(ApplicationContext applicationContext) |
void |
setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) |
void |
setEntityCallbacks(EntityCallbacks entityCallbacks)
Configure
EntityCallbacks to pre-/post-process entities during persistence operations. |
void |
setUsePreparedStatements(boolean usePreparedStatements)
Enable/disable
prepared statements usage. |
<T> ListenableFuture<Slice<T>> |
slice(Query query,
Class<T> entityClass)
Execute a
SELECT query with paging and convert the result set to a Slice of entities. |
<T> ListenableFuture<Slice<T>> |
slice(com.datastax.oss.driver.api.core.cql.Statement<?> statement,
Class<T> entityClass)
Execute a
SELECT query with paging and convert the result set to a Slice of entities. |
ListenableFuture<Void> |
truncate(Class<?> entityClass)
Execute a
TRUNCATE query to remove all entities of a given class. |
ListenableFuture<Boolean> |
update(Query query,
Update update,
Class<?> entityClass)
Update the queried entities and return true if the update was applied.
|
<T> ListenableFuture<T> |
update(T entity)
Update the given entity and return the entity if the update was applied.
|
<T> ListenableFuture<EntityWriteResult<T>> |
update(T entity,
UpdateOptions options)
Update the given entity applying
WriteOptions and return the entity if the update was applied. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitdeletepublic AsyncCassandraTemplate(com.datastax.oss.driver.api.core.CqlSession session)
AsyncCassandraTemplate initialized with the given CqlSession and a default
MappingCassandraConverter.session - CqlSession used to interact with Cassandra; must not be null.CassandraConverter,
Sessionpublic AsyncCassandraTemplate(com.datastax.oss.driver.api.core.CqlSession session,
CassandraConverter converter)
AsyncCassandraTemplate initialized with the given CqlSession and
CassandraConverter.session - CqlSession used to interact with Cassandra; must not be null.converter - CassandraConverter used to convert between Java and Cassandra types; must not be
null.CassandraConverter,
CqlSessionpublic AsyncCassandraTemplate(SessionFactory sessionFactory, CassandraConverter converter)
AsyncCassandraTemplate initialized with the given SessionFactory and
CassandraConverter.sessionFactory - SessionFactory used to interact with Cassandra; must not be null.converter - CassandraConverter used to convert between Java and Cassandra types; must not be
null.CassandraConverter,
CqlSessionpublic AsyncCassandraTemplate(AsyncCqlTemplate asyncCqlTemplate, CassandraConverter converter)
AsyncCassandraTemplate initialized with the given AsyncCqlTemplate and
CassandraConverter.asyncCqlTemplate - AsyncCqlTemplate used to interact with Cassandra; must not be null.converter - CassandraConverter used to convert between Java and Cassandra types; must not be
null.CassandraConverter,
CqlSessionpublic void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
setApplicationEventPublisher in interface ApplicationEventPublisherAwarepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException
setApplicationContext in interface ApplicationContextAwareBeansExceptionpublic void setEntityCallbacks(@Nullable EntityCallbacks entityCallbacks)
EntityCallbacks to pre-/post-process entities during persistence operations.entityCallbacks - public AsyncCqlOperations getAsyncCqlOperations()
AsyncCassandraOperationsAsyncCqlOperations to allow asynchronous CQL operations.getAsyncCqlOperations in interface AsyncCassandraOperationsAsyncCqlOperations.AsyncCqlOperationspublic CassandraConverter getConverter()
AsyncCassandraOperationsCassandraConverter.getConverter in interface AsyncCassandraOperationsCassandraConverter.public boolean isUsePreparedStatements()
prepared statements. If enabled
(default), then all persistence methods (such as select(java.lang.String, java.lang.Class<T>), update(org.springframework.data.cassandra.core.query.Query, org.springframework.data.cassandra.core.query.Update, java.lang.Class<?>), and others) will make use of
prepared statements. Note that methods accepting a Statement must be called with SimpleStatement
instances to participate in statement preparation.public void setUsePreparedStatements(boolean usePreparedStatements)
prepared statements usage. If enabled (default), then all persistence
methods (such as select(java.lang.String, java.lang.Class<T>), update(org.springframework.data.cassandra.core.query.Query, org.springframework.data.cassandra.core.query.Update, java.lang.Class<?>), and others) will make use of prepared statements. Note that
methods accepting a Statement must be called with SimpleStatement instances to participate in
statement preparation.usePreparedStatements - whether to use prepared statements.protected org.springframework.data.cassandra.core.EntityOperations getEntityOperations()
EntityOperations used to perform data access operations on an entity inside a Cassandra data
source.EntityOperations for this template.EntityOperationsprotected SpelAwareProxyProjectionFactory getProjectionFactory()
ProjectionFactory used by this template to process CQL query
projections.ProjectionFactory used by this template to process CQL query
projections.SpelAwareProxyProjectionFactoryprotected StatementFactory getStatementFactory()
StatementFactory used by this template to construct and run Cassandra CQL statements.StatementFactory used by this template to construct and run Cassandra CQL statements.StatementFactorypublic <T> ListenableFuture<List<T>> select(String cql, Class<T> entityClass)
AsyncCassandraOperationsSELECT query and convert the resulting items to a List of entities.select in interface AsyncCassandraOperationscql - must not be null.entityClass - The entity type must not be null.public <T> ListenableFuture<Void> select(String cql, Consumer<T> entityConsumer, Class<T> entityClass) throws DataAccessException
AsyncCassandraOperationsSELECT query and convert the resulting items notifying Consumer for each entity.select in interface AsyncCassandraOperationscql - must not be null.entityConsumer - object that will be notified on each entity, one object at a time, must not be
null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public <T> ListenableFuture<T> selectOne(String cql, Class<T> entityClass)
AsyncCassandraOperationsSELECT query and convert the resulting item to an entity.selectOne in interface AsyncCassandraOperationscql - must not be null.entityClass - The entity type must not be null.public ListenableFuture<com.datastax.oss.driver.api.core.cql.AsyncResultSet> execute(com.datastax.oss.driver.api.core.cql.Statement<?> statement) throws DataAccessException
AsyncCassandraOperationsStatement. Any errors that result from executing this command will be converted
into Spring's DAO exception hierarchy.execute in interface AsyncCassandraOperationsstatement - a Cassandra Statement, must not be null.AsyncResultSet.DataAccessException - if there is any problem executing the query.public <T> ListenableFuture<List<T>> select(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> entityClass)
AsyncCassandraOperationsSELECT query and convert the resulting items to a List of entities.select in interface AsyncCassandraOperationsstatement - must not be null.entityClass - The entity type must not be null.public <T> ListenableFuture<Void> select(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Consumer<T> entityConsumer, Class<T> entityClass) throws DataAccessException
AsyncCassandraOperationsSELECT query and convert the resulting items notifying Consumer for each entity.select in interface AsyncCassandraOperationsstatement - must not be null.entityConsumer - object that will be notified on each entity, one object at a time, must not be
null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public <T> ListenableFuture<T> selectOne(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> entityClass)
AsyncCassandraOperationsSELECT query and convert the resulting item to an entity.selectOne in interface AsyncCassandraOperationsstatement - must not be null.entityClass - The entity type must not be null.public <T> ListenableFuture<Slice<T>> slice(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> entityClass)
AsyncCassandraOperationsSELECT query with paging and convert the result set to a Slice of entities. A sliced
query translates the effective fetch size to the page size.slice in interface AsyncCassandraOperationsstatement - the CQL statement, must not be null.entityClass - The entity type must not be null.CassandraPageRequestpublic <T> ListenableFuture<List<T>> select(Query query, Class<T> entityClass) throws DataAccessException
AsyncCassandraOperationsSELECT query and convert the resulting items to a List of entities.select in interface AsyncCassandraOperationsquery - must not be null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public <T> ListenableFuture<Void> select(Query query, Consumer<T> entityConsumer, Class<T> entityClass) throws DataAccessException
AsyncCassandraOperationsSELECT query and convert the resulting items notifying Consumer for each entity.select in interface AsyncCassandraOperationsquery - must not be null.entityConsumer - object that will be notified on each entity, one object at a time, must not be
null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public <T> ListenableFuture<T> selectOne(Query query, Class<T> entityClass) throws DataAccessException
AsyncCassandraOperationsSELECT query and convert the resulting item to an entity.selectOne in interface AsyncCassandraOperationsquery - must not be null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public <T> ListenableFuture<Slice<T>> slice(Query query, Class<T> entityClass) throws DataAccessException
AsyncCassandraOperationsSELECT query with paging and convert the result set to a Slice of entities.slice in interface AsyncCassandraOperationsquery - the query object used to create a CQL statement, must not be null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.CassandraPageRequestpublic ListenableFuture<Boolean> update(Query query, Update update, Class<?> entityClass) throws DataAccessException
AsyncCassandraOperationsupdate in interface AsyncCassandraOperationsquery - must not be null.update - must not be null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public ListenableFuture<Boolean> delete(Query query, Class<?> entityClass) throws DataAccessException
AsyncCassandraOperationsQuery.delete in interface AsyncCassandraOperationsquery - must not be null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public ListenableFuture<Long> count(Class<?> entityClass)
AsyncCassandraOperationscount in interface AsyncCassandraOperationsentityClass - type of the entity; must not be null.public ListenableFuture<Long> count(Query query, Class<?> entityClass) throws DataAccessException
AsyncCassandraOperationsQuery. This overridden method allows users
to further refine the selection criteria using a Query predicate to determine how many entities of the
given type match the criteria.count in interface AsyncCassandraOperationsquery - user-provided count Query to execute; must not be null.entityClass - type of the entity; must not be null.DataAccessException - if any problem occurs while executing the query.public ListenableFuture<Boolean> exists(Object id, Class<?> entityClass)
AsyncCassandraOperationsentityClass with the given id exists.exists in interface AsyncCassandraOperationsid - Id value. For single primary keys it's the plain value. For composite primary keys either, it's an
instance of either PrimaryKeyClass or
MapId. Must not be null.entityClass - type of the entity; must not be null.public ListenableFuture<Boolean> exists(Query query, Class<?> entityClass) throws DataAccessException
AsyncCassandraOperationsentityClass Query yields at least one row.exists in interface AsyncCassandraOperationsquery - user-provided exists Query to execute; must not be null.entityClass - type of the entity; must not be null.DataAccessException - if any problem occurs while executing the query.public <T> ListenableFuture<T> selectOneById(Object id, Class<T> entityClass)
AsyncCassandraOperationsid for the given entityClass.selectOneById in interface AsyncCassandraOperationsid - the Id value. For single primary keys it's the plain value. For composite primary keys either the
PrimaryKeyClass or
MapId. Must not be null.entityClass - The entity type must not be null.public <T> ListenableFuture<T> insert(T entity)
AsyncCassandraOperationsinsert in interface AsyncCassandraOperationsentity - The entity to insert, must not be null.public <T> ListenableFuture<EntityWriteResult<T>> insert(T entity, InsertOptions options)
AsyncCassandraOperationsWriteOptions and return the entity if the insert was applied.insert in interface AsyncCassandraOperationsentity - The entity to insert, must not be null.options - must not be null.EntityWriteResult for this operation.InsertOptions.empty()public <T> ListenableFuture<T> update(T entity)
AsyncCassandraOperationsupdate in interface AsyncCassandraOperationsentity - The entity to update, must not be null.public <T> ListenableFuture<EntityWriteResult<T>> update(T entity, UpdateOptions options)
AsyncCassandraOperationsWriteOptions and return the entity if the update was applied.update in interface AsyncCassandraOperationsentity - The entity to update, must not be null.options - must not be null.EntityWriteResult for this operation.UpdateOptions.empty()public <T> ListenableFuture<T> delete(T entity)
AsyncCassandraOperationsdelete in interface AsyncCassandraOperationsentity - must not be null.public ListenableFuture<WriteResult> delete(Object entity, QueryOptions options)
AsyncCassandraOperationsQueryOptions and return the entity if the delete was applied.delete in interface AsyncCassandraOperationsentity - must not be null.options - must not be null.WriteResult for this operation.QueryOptions.empty()public ListenableFuture<Boolean> deleteById(Object id, Class<?> entityClass)
AsyncCassandraOperationsdeleteById in interface AsyncCassandraOperationsid - the Id value. For single primary keys it's the plain value. For composite primary keys either the
PrimaryKeyClass or
MapId. Must not be null.entityClass - The entity type must not be null.public ListenableFuture<Void> truncate(Class<?> entityClass)
AsyncCassandraOperationsTRUNCATE query to remove all entities of a given class.truncate in interface AsyncCassandraOperationsentityClass - The entity type must not be null.protected <E extends CassandraMappingEvent<T>,T> void maybeEmitEvent(E event)
protected <T> T maybeCallBeforeConvert(T object,
com.datastax.oss.driver.api.core.CqlIdentifier tableName)
protected <T> T maybeCallBeforeSave(T object,
com.datastax.oss.driver.api.core.CqlIdentifier tableName,
com.datastax.oss.driver.api.core.cql.Statement<?> statement)
Copyright © 2011–2021 Pivotal Software, Inc.. All rights reserved.