你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

CosmosTemplate Class

  • java.lang.Object
    • com.azure.spring.data.cosmos.core.CosmosTemplate

Implements

CosmosOperations org.springframework.context.ApplicationContextAware

public class CosmosTemplate
implements CosmosOperations, org.springframework.context.ApplicationContextAware

Template class for cosmos db

Constructor Summary

Constructor Description
CosmosTemplate(CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter)

Initialization

CosmosTemplate(CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, IsNewAwareAuditingHandler cosmosAuditingHandler)

Initialization

CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter)

Initialization

CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, IsNewAwareAuditingHandler cosmosAuditingHandler)

Initialization

Method Summary

Modifier and Type Method and Description
void <S,T>deleteEntities(CosmosEntityInformation<T,?> information, Iterable<S> entities)

Deletes the entities using bulk

Iterable<S> <S,T>insertAll(CosmosEntityInformation<T,?> information, Iterable<S> entities)

Insert all items with bulk.

Iterable<T> <T,ID>findByIds(Iterable<ID> ids, Class<T> domainType, String containerName)

Find by ids

long count(SqlQuerySpec querySpec, String containerName)

Count

long count(CosmosQuery query, String containerName)

Count

Iterable<T> delete(CosmosQuery query, Class<T> domainType, String containerName)

Delete the DocumentQuery, need to query the domains at first, then delete the item from the result.

void deleteEntity(String containerName, T entity)

Deletes the entity

Boolean exists(CosmosQuery query, Class<T> domainType, String containerName)

Checks if document query items exist

Iterable<T> find(CosmosQuery query, Class<T> domainType, String containerName)

Finds the document query items

Iterable<T> findAll(PartitionKey partitionKey, Class<T> domainType)

Find the DocumentQuery, find all the items specified by domain type in the given container.

Iterable<T> findAll(Class<T> domainType)

Find the DocumentQuery, find all the items specified by domain type.

Iterable<T> findAll(String containerName, Class<T> domainType)

Find the DocumentQuery, find all the items specified by domain type in the given container.

org.springframework.data.domain.Page<T> findAll(Pageable pageable, Class<T> domainType, String containerName)

Find all items in a given container with partition key

T findById(Object id, Class<T> domainType)

Finds item by id

T findById(Object id, Class<T> domainType, PartitionKey partitionKey)

Finds item by id

T findById(String containerName, Object id, Class<T> domainType)

Finds item by id

T insert(T objectToSave, PartitionKey partitionKey)

Inserts item

T insert(String containerName, T objectToSave)

Inserts item into the given container

T insert(String containerName, T objectToSave, PartitionKey partitionKey)

Inserts item into the given container

org.springframework.data.domain.Page<T> paginationQuery(CosmosQuery query, Class<T> domainType, String containerName)

Pagination query

T patch(Object id, PartitionKey partitionKey, Class<T> domainType, CosmosPatchOperations patchOperations)

Patches item applies partial update (patch) to an item

T patch(Object id, PartitionKey partitionKey, Class<T> domainType, CosmosPatchOperations patchOperations, CosmosPatchItemRequestOptions options)

applies partial update (patch) to an item with CosmosPatchItemRequestOptions

org.springframework.data.domain.Page<T> runPaginationQuery(SqlQuerySpec querySpec, Pageable pageable, Class<?> domainType, Class<T> returnType)

Run the query.

Iterable<T> runQuery(SqlQuerySpec querySpec, Class<?> domainType, Class<T> returnType)

Run the query.

Iterable<T> runQuery(SqlQuerySpec querySpec, Sort sort, Class<?> domainType, Class<T> returnType)

Run the query.

org.springframework.data.domain.Slice<T> runSliceQuery(SqlQuerySpec querySpec, Pageable pageable, Class<?> domainType, Class<T> returnType)

Run custom SQL query

org.springframework.data.domain.Slice<T> sliceQuery(CosmosQuery query, Class<T> domainType, String containerName)

Slice query

void upsert(T object)

Upserts an item with partition key

void upsert(String containerName, T object)

Upserts an item into container with partition key

T upsertAndReturnEntity(String containerName, T object)

Upserts an item and return item properties

long count(String containerName)

Count

CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformation<?,?> information)

Creates container if not exists

void deleteAll(String containerName, Class<?> domainType)

Delete the DocumentQuery, delete all the items in the given container.

void deleteById(String containerName, Object id, PartitionKey partitionKey)

Deletes the item by id and partition key.

void deleteContainer(String containerName)

Delete container

String getContainerName(Class<?> domainType)

To get container name by domainType

String getContainerNameOverride(String containerName)

Check if an overridden version of containerName is present, and if so, return it

CosmosContainerProperties getContainerProperties(String containerName)

Get properties for specified container

MappingCosmosConverter getConverter()

To get converter

CosmosContainerProperties replaceContainerProperties(String containerName, CosmosContainerProperties properties)

Replace container properties for the specified container

void setApplicationContext(ApplicationContext applicationContext)

Sets the application context

Methods inherited from java.lang.Object

Constructor Details

CosmosTemplate

public CosmosTemplate(CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter)

Initialization

Parameters:

client - must not be null
databaseName - must not be null
cosmosConfig - must not be null
mappingCosmosConverter - must not be null

CosmosTemplate

public CosmosTemplate(CosmosAsyncClient client, String databaseName, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, IsNewAwareAuditingHandler cosmosAuditingHandler)

Initialization

Parameters:

client - must not be null
databaseName - must not be null
cosmosConfig - must not be null
mappingCosmosConverter - must not be null
cosmosAuditingHandler - can be null

CosmosTemplate

public CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter)

Initialization

Parameters:

cosmosFactory - must not be null
cosmosConfig - must not be null
mappingCosmosConverter - must not be null

CosmosTemplate

public CosmosTemplate(CosmosFactory cosmosFactory, CosmosConfig cosmosConfig, MappingCosmosConverter mappingCosmosConverter, IsNewAwareAuditingHandler cosmosAuditingHandler)

Initialization

Parameters:

cosmosFactory - must not be null
cosmosConfig - must not be null
mappingCosmosConverter - must not be null
cosmosAuditingHandler - can be null

Method Details

<S,T>deleteEntities

public void deleteEntities(CosmosEntityInformation information, Iterable entities)

Deletes the entities using bulk

Parameters:

information - the CosmosEntityInformation
entities - the Iterable entities to be inserted

<S,T>insertAll

public Iterable insertAll(CosmosEntityInformation information, Iterable entities)

Insert all items with bulk.

Parameters:

information - the CosmosEntityInformation
entities - the Iterable entities to be inserted

Returns:

Flux of result

<T,ID>findByIds

public Iterable findByIds(Iterable ids, Class domainType, String containerName)

Find by ids

Parameters:

ids
domainType
containerName

count

public long count(SqlQuerySpec querySpec, String containerName)

Count

Parameters:

querySpec
containerName

count

public long count(CosmosQuery query, String containerName)

Count

Parameters:

query
containerName

delete

public Iterable delete(CosmosQuery query, Class domainType, String containerName)

Delete the DocumentQuery, need to query the domains at first, then delete the item from the result. The cosmos db Sql API do _NOT_ support DELETE query, we cannot add one DeleteQueryGenerator. Uses bulk if possible.

Parameters:

query - The representation for query method.
domainType - Class of domain
containerName - Container name of database

Returns:

All the deleted items as List.

deleteEntity

public void deleteEntity(String containerName, T entity)

Deletes the entity

Parameters:

containerName - the container name
entity - the entity object

exists

public Boolean exists(CosmosQuery query, Class domainType, String containerName)

Checks if document query items exist

Parameters:

query - The representation for query method.
domainType - Class of domain
containerName - Container name of database

Returns:

if items exist

find

public Iterable find(CosmosQuery query, Class domainType, String containerName)

Finds the document query items

Parameters:

query - The representation for query method.
domainType - Class of domain
containerName - Container name of database

Returns:

All the found items as List.

findAll

public Iterable findAll(PartitionKey partitionKey, Class domainType)

Find the DocumentQuery, find all the items specified by domain type in the given container.

Parameters:

partitionKey
domainType

findAll

public Iterable findAll(Class domainType)

Find the DocumentQuery, find all the items specified by domain type.

Parameters:

domainType - the domain type

Returns:

found results in a List

findAll

public Iterable findAll(String containerName, Class domainType)

Find the DocumentQuery, find all the items specified by domain type in the given container.

Parameters:

containerName - the container name
domainType - the domain type

Returns:

found results in a List

findAll

public Page findAll(Pageable pageable, Class domainType, String containerName)

Find all items in a given container with partition key

Parameters:

pageable
domainType
containerName

findById

public T findById(Object id, Class domainType)

Finds item by id

Parameters:

id - must not be null
domainType - must not be null

Returns:

found item

findById

public T findById(Object id, Class domainType, PartitionKey partitionKey)

Finds item by id

Parameters:

id
domainType
partitionKey

findById

public T findById(String containerName, Object id, Class domainType)

Finds item by id

Parameters:

containerName - must not be null
id - must not be null
domainType - must not be null

Returns:

found item

insert

public T insert(T objectToSave, PartitionKey partitionKey)

Inserts item

Parameters:

objectToSave - must not be null
partitionKey - must not be null

Returns:

the inserted item

insert

public T insert(String containerName, T objectToSave)

Inserts item into the given container

Parameters:

containerName - must not be null
objectToSave - must not be null

Returns:

the inserted item

insert

public T insert(String containerName, T objectToSave, PartitionKey partitionKey)

Inserts item into the given container

Parameters:

containerName - must not be null
objectToSave - must not be null
partitionKey - must not be null

Returns:

the inserted item

paginationQuery

public Page paginationQuery(CosmosQuery query, Class domainType, String containerName)

Pagination query

Parameters:

query
domainType
containerName

patch

public T patch(Object id, PartitionKey partitionKey, Class domainType, CosmosPatchOperations patchOperations)

Patches item applies partial update (patch) to an item

Parameters:

id - must not be null
partitionKey - must not be null
domainType
patchOperations - must not be null

Returns:

the patched item

patch

public T patch(Object id, PartitionKey partitionKey, Class domainType, CosmosPatchOperations patchOperations, CosmosPatchItemRequestOptions options)

applies partial update (patch) to an item with CosmosPatchItemRequestOptions

Parameters:

id - must not be null
partitionKey - must not be null
domainType - must not be null
patchOperations - must not be null
options - Optional CosmosPatchItemRequestOptions, e.g. options.setFilterPredicate("FROM products p WHERE p.used = false");

Returns:

the patched item

runPaginationQuery

public Page runPaginationQuery(SqlQuerySpec querySpec, Pageable pageable, Class domainType, Class returnType)

Run the query.

Parameters:

querySpec
pageable
domainType
returnType

runQuery

public Iterable runQuery(SqlQuerySpec querySpec, Class domainType, Class returnType)

Run the query.

Parameters:

querySpec
domainType
returnType

runQuery

public Iterable runQuery(SqlQuerySpec querySpec, Sort sort, Class domainType, Class returnType)

Run the query.

Parameters:

querySpec
sort
domainType
returnType

runSliceQuery

public Slice runSliceQuery(SqlQuerySpec querySpec, Pageable pageable, Class domainType, Class returnType)

Run custom SQL query

Parameters:

querySpec
pageable
domainType
returnType

sliceQuery

public Slice sliceQuery(CosmosQuery query, Class domainType, String containerName)

Slice query

Parameters:

query
domainType
containerName

upsert

public void upsert(T object)

Upserts an item with partition key

Parameters:

object - upsert object

upsert

public void upsert(String containerName, T object)

Upserts an item into container with partition key

Parameters:

containerName - the container name
object - upsert object

upsertAndReturnEntity

public T upsertAndReturnEntity(String containerName, T object)

Upserts an item and return item properties

Parameters:

containerName - the container name
object - upsert object

Returns:

upsert object entity

count

public long count(String containerName)

Count

Parameters:

containerName

createContainerIfNotExists

public CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformation information)

Creates container if not exists

Parameters:

information

deleteAll

public void deleteAll(String containerName, Class domainType)

Delete the DocumentQuery, delete all the items in the given container.

Parameters:

containerName - Container name of database
domainType - the domain type

deleteById

public void deleteById(String containerName, Object id, PartitionKey partitionKey)

Deletes the item by id and partition key.

Parameters:

containerName - Container name of database
id - item id
partitionKey - the partition key

deleteContainer

public void deleteContainer(String containerName)

Delete container

Parameters:

containerName

getContainerName

public String getContainerName(Class domainType)

To get container name by domainType

Parameters:

domainType

getContainerNameOverride

public String getContainerNameOverride(String containerName)

Check if an overridden version of containerName is present, and if so, return it

Parameters:

containerName - Container name of database

Returns:

containerName

getContainerProperties

public CosmosContainerProperties getContainerProperties(String containerName)

Get properties for specified container

Parameters:

containerName

getConverter

public MappingCosmosConverter getConverter()

To get converter

replaceContainerProperties

public CosmosContainerProperties replaceContainerProperties(String containerName, CosmosContainerProperties properties)

Replace container properties for the specified container

Parameters:

containerName
properties

setApplicationContext

public void setApplicationContext(ApplicationContext applicationContext)

Sets the application context

Parameters:

applicationContext - must not be null

Throws:

org.springframework.beans.BeansException - the bean exception

Applies to