public class CassandraTemplate extends Object implements CassandraOperations, ApplicationEventPublisherAware
CassandraOperations. It simplifies the use of 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 Session reference, or get
prepared in an application context and given to services as bean reference.
Note: The Session 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.
CassandraOperationsExecutableSelectOperation.ExecutableSelect<T>, ExecutableSelectOperation.SelectWithProjection<T>, ExecutableSelectOperation.SelectWithQuery<T>, ExecutableSelectOperation.SelectWithTable<T>, ExecutableSelectOperation.TerminatingSelect<T>ExecutableInsertOperation.ExecutableInsert<T>, ExecutableInsertOperation.InsertWithOptions<T>, ExecutableInsertOperation.InsertWithTable<T>, ExecutableInsertOperation.TerminatingInsert<T>ExecutableUpdateOperation.ExecutableUpdate, ExecutableUpdateOperation.TerminatingUpdate, ExecutableUpdateOperation.UpdateWithQuery, ExecutableUpdateOperation.UpdateWithTableExecutableDeleteOperation.DeleteWithQuery, ExecutableDeleteOperation.DeleteWithTable, ExecutableDeleteOperation.ExecutableDelete, ExecutableDeleteOperation.TerminatingDelete| Constructor and Description |
|---|
CassandraTemplate(CqlOperations cqlOperations,
CassandraConverter converter)
Creates an instance of
CassandraTemplate initialized with the given CqlOperations and
CassandraConverter. |
CassandraTemplate(com.datastax.driver.core.Session session)
Creates an instance of
CassandraTemplate initialized with the given Session and a default
MappingCassandraConverter. |
CassandraTemplate(com.datastax.driver.core.Session session,
CassandraConverter converter)
|
CassandraTemplate(SessionFactory sessionFactory,
CassandraConverter converter)
Creates an instance of
CassandraTemplate initialized with the given SessionFactory and
CassandraConverter. |
| Modifier and Type | Method and Description |
|---|---|
CassandraBatchOperations |
batchOps()
Returns a new
CassandraBatchOperations. |
long |
count(Class<?> entityClass)
Returns the number of rows for the given entity class.
|
long |
count(Query query,
Class<?> entityClass)
Returns the number of rows for the given entity class applying
Query. |
ExecutableDeleteOperation.ExecutableDelete |
delete(Class<?> domainType)
Begin creating a
DELETE operation for the given domainType. |
void |
delete(Object entity)
Delete the given entity and return the entity if the delete was applied.
|
WriteResult |
delete(Object entity,
QueryOptions options)
Delete the given entity applying
QueryOptions and return the entity if the delete was applied. |
boolean |
delete(Query query,
Class<?> entityClass)
Remove entities (rows)/columns from the table by
Query. |
boolean |
deleteById(Object id,
Class<?> entityClass)
Remove the given object from the table by id.
|
boolean |
exists(Object id,
Class<?> entityClass)
Determine whether a row of
entityClass with the given id exists. |
boolean |
exists(Query query,
Class<?> entityClass)
Determine whether the result for
entityClass Query yields at least one row. |
CassandraConverter |
getConverter()
Returns the underlying
CassandraConverter. |
CqlOperations |
getCqlOperations()
Expose the underlying
CqlOperations to allow CQL operations. |
protected MappingContext<? extends CassandraPersistentEntity<?>,CassandraPersistentProperty> |
getMappingContext()
Returns the
CassandraMappingContext used by this template to access mapping meta-data in order to store
(map) object to Cassandra tables. |
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. |
CqlIdentifier |
getTableName(Class<?> entityClass)
The table name used for the specified class by this template.
|
<T> ExecutableInsertOperation.ExecutableInsert<T> |
insert(Class<T> domainType)
Begin creating an
INSERT operation for given domainType. |
<T> T |
insert(T entity)
Insert the given entity and return the entity if the insert was applied.
|
<T> EntityWriteResult<T> |
insert(T entity,
InsertOptions options)
Insert the given entity applying
WriteOptions and return the entity if the insert was applied. |
<T> ExecutableSelectOperation.ExecutableSelect<T> |
query(Class<T> domainType)
Begin creating a Cassandra
SELECT query operation for the given domainType. |
<T> List<T> |
select(Query query,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a List of entities. |
<T> List<T> |
select(com.datastax.driver.core.Statement statement,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a List of entities. |
<T> List<T> |
select(String cql,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a List of entities. |
<T> T |
selectOne(Query query,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting item to an entity. |
<T> T |
selectOne(com.datastax.driver.core.Statement statement,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting item to an entity. |
<T> T |
selectOne(String cql,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting item to an entity. |
<T> T |
selectOneById(Object id,
Class<T> entityClass)
Execute the Select by
id for the given entityClass. |
void |
setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) |
<T> 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> Slice<T> |
slice(com.datastax.driver.core.Statement statement,
Class<T> entityClass)
Execute a
SELECT query with paging and convert the result set to a Slice of entities. |
<T> Stream<T> |
stream(Query query,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a Iterator of entities. |
<T> Stream<T> |
stream(com.datastax.driver.core.Statement statement,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a Iterator of entities. |
<T> Stream<T> |
stream(String cql,
Class<T> entityClass)
Execute a
SELECT query and convert the resulting items to a Iterator of entities. |
void |
truncate(Class<?> entityClass)
Execute a
TRUNCATE query to remove all entities of a given class. |
ExecutableUpdateOperation.ExecutableUpdate |
update(Class<?> domainType)
Begin creating an
UPDATE operation for the given domainType. |
boolean |
update(Query query,
Update update,
Class<?> entityClass)
Update the queried entities and return true if the update was applied.
|
<T> T |
update(T entity)
Update the given entity and return the entity if the update was applied.
|
<T> EntityWriteResult<T> |
update(T entity,
UpdateOptions options)
Update the given entity applying
WriteOptions and return the entity if the update was applied. |
public CassandraTemplate(com.datastax.driver.core.Session session)
CassandraTemplate initialized with the given Session and a default
MappingCassandraConverter.session - Session used to interact with Cassandra; must not be null.CassandraConverter,
Sessionpublic CassandraTemplate(com.datastax.driver.core.Session session,
CassandraConverter converter)
session - Session used to interact with Cassandra; must not be null.converter - CassandraConverter used to convert between Java and Cassandra types; must not be
null.CassandraConverter,
Sessionpublic CassandraTemplate(SessionFactory sessionFactory, CassandraConverter converter)
CassandraTemplate 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,
SessionFactorypublic CassandraTemplate(CqlOperations cqlOperations, CassandraConverter converter)
CassandraTemplate initialized with the given CqlOperations and
CassandraConverter.cqlOperations - CqlOperations used to interact with Cassandra; must not be null.converter - CassandraConverter used to convert between Java and Cassandra types; must not be
null.CassandraConverter,
Sessionpublic CassandraBatchOperations batchOps()
CassandraOperationsCassandraBatchOperations. Each CassandraBatchOperations instance can be executed only
once so you might want to obtain new CassandraBatchOperations instances for each batch.batchOps in interface CassandraOperationsCassandraBatchOperations associated with the given entity class.public CassandraConverter getConverter()
CassandraOperationsCassandraConverter.getConverter in interface CassandraOperationsCassandraConverter.public CqlOperations getCqlOperations()
CassandraOperationsCqlOperations to allow CQL operations.getCqlOperations in interface CassandraOperationsCqlOperations.CqlOperationspublic CqlIdentifier getTableName(Class<?> entityClass)
CassandraOperationsgetTableName in interface CassandraOperationsentityClass - The entity type must not be null.CqlIdentifierpublic void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
setApplicationEventPublisher in interface ApplicationEventPublisherAwarepublic <T> List<T> select(String cql, Class<T> entityClass)
CassandraOperationsSELECT query and convert the resulting items to a List of entities.select in interface CassandraOperationscql - must not be null.entityClass - The entity type must not be null.public <T> Stream<T> stream(String cql, Class<T> entityClass) throws DataAccessException
CassandraOperationsSELECT query and convert the resulting items to a Iterator of entities.
Returns a Iterator that wraps the Cassandra ResultSet.
stream in interface CassandraOperationsT - element return type.cql - query to execute. Must not be empty or null.entityClass - Class type of the elements in the Iterator stream. Must not be null.Iterator (stream) over the elements in the query result set.DataAccessException - if there is any problem executing the query.public <T> T selectOne(String cql, Class<T> entityClass)
CassandraOperationsSELECT query and convert the resulting item to an entity.selectOne in interface CassandraOperationscql - must not be null.entityClass - The entity type must not be null.public <T> List<T> select(com.datastax.driver.core.Statement statement, Class<T> entityClass)
CassandraOperationsSELECT query and convert the resulting items to a List of entities.select in interface CassandraOperationsstatement - must not be null.entityClass - The entity type must not be null.public <T> Slice<T> slice(com.datastax.driver.core.Statement statement, Class<T> entityClass)
CassandraOperationsSELECT 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 CassandraOperationsstatement - the CQL statement, must not be null.entityClass - The entity type must not be null.public <T> Stream<T> stream(com.datastax.driver.core.Statement statement, Class<T> entityClass) throws DataAccessException
CassandraOperationsSELECT query and convert the resulting items to a Iterator of entities.
Returns a Iterator that wraps the Cassandra ResultSet.
stream in interface CassandraOperationsT - element return type.statement - query to execute. Must not be empty or null.entityClass - Class type of the elements in the Iterator stream. Must not be null.Iterator (stream) over the elements in the query result set.DataAccessException - if there is any problem executing the query.public <T> T selectOne(com.datastax.driver.core.Statement statement,
Class<T> entityClass)
CassandraOperationsSELECT query and convert the resulting item to an entity.selectOne in interface CassandraOperationsstatement - must not be null.entityClass - The entity type must not be null.public <T> List<T> select(Query query, Class<T> entityClass) throws DataAccessException
CassandraOperationsSELECT query and convert the resulting items to a List of entities.select in interface CassandraOperationsquery - must not be null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public <T> Slice<T> slice(Query query, Class<T> entityClass) throws DataAccessException
CassandraOperationsSELECT query with paging and convert the result set to a Slice of entities.slice in interface CassandraOperationsquery - 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 <T> Stream<T> stream(Query query, Class<T> entityClass) throws DataAccessException
CassandraOperationsSELECT query and convert the resulting items to a Iterator of entities.
Returns a Iterator that wraps the Cassandra ResultSet.
stream in interface CassandraOperationsT - element return type.query - query to execute. Must not be empty or null.entityClass - Class type of the elements in the Iterator stream. Must not be null.Iterator (stream) over the elements in the query result set.DataAccessException - if there is any problem executing the query.public <T> T selectOne(Query query, Class<T> entityClass) throws DataAccessException
CassandraOperationsSELECT query and convert the resulting item to an entity.selectOne in interface CassandraOperationsquery - must not be null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public boolean update(Query query, Update update, Class<?> entityClass) throws DataAccessException
CassandraOperationsupdate in interface CassandraOperationsquery - 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 boolean delete(Query query, Class<?> entityClass) throws DataAccessException
CassandraOperationsQuery.delete in interface CassandraOperationsquery - must not be null.entityClass - The entity type must not be null.DataAccessException - if there is any problem executing the query.public long count(Class<?> entityClass)
CassandraOperationscount in interface CassandraOperationsentityClass - type of the entity; must not be null.public long count(Query query, Class<?> entityClass) throws DataAccessException
CassandraOperationsQuery.
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 CassandraOperationsquery - user-defined 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 boolean exists(Object id, Class<?> entityClass)
CassandraOperationsentityClass with the given id exists.exists in interface CassandraOperationsid - 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 boolean exists(Query query, Class<?> entityClass) throws DataAccessException
CassandraOperationsentityClass Query yields at least one row.exists in interface CassandraOperationsquery - user-defined 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> T selectOneById(Object id, Class<T> entityClass)
CassandraOperationsid for the given entityClass.selectOneById in interface CassandraOperationsid - 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> T insert(T entity)
CassandraOperationsinsert in interface CassandraOperationsentity - The entity to insert, must not be null.public <T> EntityWriteResult<T> insert(T entity, InsertOptions options)
CassandraOperationsWriteOptions and return the entity if the insert was applied.insert in interface CassandraOperationsentity - The entity to insert, must not be null.options - must not be null.EntityWriteResult for this operation.InsertOptions.empty()public <T> T update(T entity)
CassandraOperationsupdate in interface CassandraOperationsentity - The entity to update, must not be null.public <T> EntityWriteResult<T> update(T entity, UpdateOptions options)
CassandraOperationsWriteOptions and return the entity if the update was applied.update in interface CassandraOperationsentity - The entity to update, must not be null.options - must not be null.EntityWriteResult for this operation.UpdateOptions.empty()public void delete(Object entity)
CassandraOperationsdelete in interface CassandraOperationsentity - must not be null.public WriteResult delete(Object entity, QueryOptions options)
CassandraOperationsQueryOptions and return the entity if the delete was applied.delete in interface CassandraOperationsentity - must not be null.options - must not be null.WriteResult for this operation.QueryOptions.empty()public boolean deleteById(Object id, Class<?> entityClass)
CassandraOperationsdeleteById in interface CassandraOperationsid - 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 void truncate(Class<?> entityClass)
CassandraOperationsTRUNCATE query to remove all entities of a given class.truncate in interface CassandraOperationsentityClass - The entity type must not be null.public <T> ExecutableSelectOperation.ExecutableSelect<T> query(Class<T> domainType)
ExecutableSelectOperationSELECT query operation for the given domainType.query in interface ExecutableSelectOperationT - type of the application domain object.domainType - type to domain object to query; must not be null.ExecutableSelectOperation.ExecutableSelect.ExecutableSelectOperation.ExecutableSelectpublic <T> ExecutableInsertOperation.ExecutableInsert<T> insert(Class<T> domainType)
ExecutableInsertOperationINSERT operation for given domainType.insert in interface ExecutableInsertOperationdomainType - type of domain object to insert; must not be null.ExecutableInsertOperation.ExecutableInsert.ExecutableInsertOperation.ExecutableInsertpublic ExecutableUpdateOperation.ExecutableUpdate update(Class<?> domainType)
ExecutableUpdateOperationUPDATE operation for the given domainType.update in interface ExecutableUpdateOperationdomainType - type of domain object to update; must not be null.ExecutableUpdateOperation.ExecutableUpdate.public ExecutableDeleteOperation.ExecutableDelete delete(Class<?> domainType)
ExecutableDeleteOperationDELETE operation for the given domainType.delete in interface ExecutableDeleteOperationdomainType - type of domain object to delete; must not be null.ExecutableDeleteOperation.ExecutableDelete.ExecutableDeleteOperation.ExecutableDeleteprotected MappingContext<? extends CassandraPersistentEntity<?>,CassandraPersistentProperty> getMappingContext()
CassandraMappingContext used by this template to access mapping meta-data in order to store
(map) object to Cassandra tables.CassandraMappingContext used by this template.CassandraMappingContextprotected 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.StatementFactoryCopyright © 2011–2018 Pivotal Software, Inc.. All rights reserved.