public class CassandraTemplate extends Object implements CassandraOperations
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.
CassandraOperations| 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.
|
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 the row
entityClass with the given id exists. |
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 used to store (map)
object to Cassandra tables. |
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.
|
void |
insert(Object entity)
Insert the given entity and return the entity if the insert was applied.
|
WriteResult |
insert(Object entity,
InsertOptions options)
Insert the given entity applying
WriteOptions and return the entity if the insert was applied. |
<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. |
<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. |
void |
update(Object entity)
Update the given entity and return the entity if the update was applied.
|
WriteResult |
update(Object entity,
UpdateOptions options)
Update the given entity applying
WriteOptions and return the entity if the update was applied. |
boolean |
update(Query query,
Update update,
Class<?> entityClass)
Update the queried entities and return true 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 CassandraConverter getConverter()
CassandraOperationsCassandraConverter.getConverter in interface CassandraOperationsCassandraConverter.public CqlOperations getCqlOperations()
CassandraOperationsCqlOperations to allow CQL operations.getCqlOperations in interface CassandraOperationsCqlOperations.CqlOperationsprotected MappingContext<? extends CassandraPersistentEntity<?>,CassandraPersistentProperty> getMappingContext()
CassandraMappingContext used by this template to access mapping meta-data used to store (map)
object to Cassandra tables.CassandraMappingContext used by this template.CassandraMappingContextprotected 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> 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 - must not be null.public boolean exists(Object id, Class<?> entityClass)
CassandraOperationsentityClass with the given id exists.exists 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 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 void insert(Object entity)
CassandraOperationsinsert in interface CassandraOperationsentity - The entity to insert, must not be null.public WriteResult insert(Object 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.WriteResult for this operation.InsertOptions.empty()public void update(Object entity)
CassandraOperationsupdate in interface CassandraOperationsentity - The entity to update, must not be null.public WriteResult update(Object 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.WriteResult 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 CqlIdentifier getTableName(Class<?> entityClass)
CassandraOperationsgetTableName in interface CassandraOperationsentityClass - The entity type must not be null.CqlIdentifierpublic 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.Copyright © 2011–2018 Pivotal Software, Inc.. All rights reserved.