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
,
Session
public 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
,
Session
public 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
,
SessionFactory
public 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
,
Session
public CassandraConverter getConverter()
CassandraOperations
CassandraConverter
.getConverter
in interface CassandraOperations
CassandraConverter
.public CqlOperations getCqlOperations()
CassandraOperations
CqlOperations
to allow CQL operations.getCqlOperations
in interface CassandraOperations
CqlOperations
.CqlOperations
protected 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.CassandraMappingContext
protected 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.StatementFactory
public <T> List<T> select(String cql, Class<T> entityClass)
CassandraOperations
SELECT
query and convert the resulting items to a List
of entities.select
in interface CassandraOperations
cql
- must not be null.entityClass
- The entity type must not be null.public <T> Stream<T> stream(String cql, Class<T> entityClass) throws DataAccessException
CassandraOperations
SELECT
query and convert the resulting items to a Iterator
of entities.
Returns a Iterator
that wraps the Cassandra ResultSet
.
stream
in interface CassandraOperations
T
- 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)
CassandraOperations
SELECT
query and convert the resulting item to an entity.selectOne
in interface CassandraOperations
cql
- 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)
CassandraOperations
SELECT
query and convert the resulting items to a List
of entities.select
in interface CassandraOperations
statement
- 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)
CassandraOperations
SELECT
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 CassandraOperations
statement
- 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
CassandraOperations
SELECT
query and convert the resulting items to a Iterator
of entities.
Returns a Iterator
that wraps the Cassandra ResultSet
.
stream
in interface CassandraOperations
T
- 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)
CassandraOperations
SELECT
query and convert the resulting item to an entity.selectOne
in interface CassandraOperations
statement
- must not be null.entityClass
- The entity type must not be null.public <T> List<T> select(Query query, Class<T> entityClass) throws DataAccessException
CassandraOperations
SELECT
query and convert the resulting items to a List
of entities.select
in interface CassandraOperations
query
- 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
CassandraOperations
SELECT
query with paging and convert the result set to a Slice
of entities.slice
in interface CassandraOperations
query
- 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.CassandraPageRequest
public <T> Stream<T> stream(Query query, Class<T> entityClass) throws DataAccessException
CassandraOperations
SELECT
query and convert the resulting items to a Iterator
of entities.
Returns a Iterator
that wraps the Cassandra ResultSet
.
stream
in interface CassandraOperations
T
- 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
CassandraOperations
SELECT
query and convert the resulting item to an entity.selectOne
in interface CassandraOperations
query
- 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
CassandraOperations
update
in interface CassandraOperations
query
- 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
CassandraOperations
Query
.delete
in interface CassandraOperations
query
- 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)
CassandraOperations
count
in interface CassandraOperations
entityClass
- must not be null.public boolean exists(Object id, Class<?> entityClass)
CassandraOperations
entityClass
with the given id
exists.exists
in interface CassandraOperations
id
- 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)
CassandraOperations
id
for the given entityClass
.selectOneById
in interface CassandraOperations
id
- 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)
CassandraOperations
insert
in interface CassandraOperations
entity
- The entity to insert, must not be null.public WriteResult insert(Object entity, InsertOptions options)
CassandraOperations
WriteOptions
and return the entity if the insert was applied.insert
in interface CassandraOperations
entity
- The entity to insert, must not be null.options
- must not be null.WriteResult
for this operation.InsertOptions.empty()
public void update(Object entity)
CassandraOperations
update
in interface CassandraOperations
entity
- The entity to update, must not be null.public WriteResult update(Object entity, UpdateOptions options)
CassandraOperations
WriteOptions
and return the entity if the update was applied.update
in interface CassandraOperations
entity
- The entity to update, must not be null.options
- must not be null.WriteResult
for this operation.UpdateOptions.empty()
public void delete(Object entity)
CassandraOperations
delete
in interface CassandraOperations
entity
- must not be null.public WriteResult delete(Object entity, QueryOptions options)
CassandraOperations
QueryOptions
and return the entity if the delete was applied.delete
in interface CassandraOperations
entity
- must not be null.options
- must not be null.WriteResult
for this operation.QueryOptions.empty()
public boolean deleteById(Object id, Class<?> entityClass)
CassandraOperations
deleteById
in interface CassandraOperations
id
- 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)
CassandraOperations
TRUNCATE
query to remove all entities of a given class.truncate
in interface CassandraOperations
entityClass
- The entity type must not be null.public CqlIdentifier getTableName(Class<?> entityClass)
CassandraOperations
getTableName
in interface CassandraOperations
entityClass
- The entity type must not be null.CqlIdentifier
public CassandraBatchOperations batchOps()
CassandraOperations
CassandraBatchOperations
. Each CassandraBatchOperations
instance can be executed only
once so you might want to obtain new CassandraBatchOperations
instances for each batch.batchOps
in interface CassandraOperations
CassandraBatchOperations
associated with the given entity class.Copyright © 2011–2019 Pivotal Software, Inc.. All rights reserved.