Class CosmosContainer
-
Method Summary
Modifier and TypeMethodDescription<T> CosmosItemResponse<T>
createItem
(T item) Creates a new item synchronously and returns its respective Cosmos item response.<T> CosmosItemResponse<T>
createItem
(T item, CosmosItemRequestOptions options) Creates a new item synchronously and returns its respective Cosmos item response while specifying additional options.<T> CosmosItemResponse<T>
createItem
(T item, PartitionKey partitionKey, CosmosItemRequestOptions options) Creates a new item synchronously and returns its respective Cosmos item response while specifying additional options.delete()
Deletes the current cosmos container.delete
(CosmosContainerRequestOptions options) Deletes the current Cosmos container while specifying additional options such as If-Match.deleteAllItemsByPartitionKey
(PartitionKey partitionKey, CosmosItemRequestOptions options) Deletes all items in the Container with the specified partitionKey value.deleteItem
(String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options) Deletes an item in the current container.<T> CosmosItemResponse<Object>
deleteItem
(T item, CosmosItemRequestOptions options) Deletes an item in the current container.void
enableGlobalThroughputControlGroup
(ThroughputControlGroupConfig groupConfig, GlobalThroughputControlConfig globalControlConfig) Enable the throughput control group with global control mode.void
Enable the throughput control group with local control mode.<TContext> Iterable<CosmosBulkOperationResponse<TContext>>
executeBulkOperations
(Iterable<CosmosItemOperation> operations) Executes list of operations in Bulk.<TContext> Iterable<CosmosBulkOperationResponse<TContext>>
executeBulkOperations
(Iterable<CosmosItemOperation> operations, CosmosBulkExecutionOptions bulkOptions) Executes list of operations in Bulk.executeCosmosBatch
(CosmosBatch cosmosBatch) Executes the transactional batch.executeCosmosBatch
(CosmosBatch cosmosBatch, CosmosBatchRequestOptions requestOptions) Executes the transactional batch.Obtains a list ofFeedRange
that can be used to parallelize Feed operations.getId()
Gets the current container id.Gets the Cosmos scripts using the current container as context.void
Initializes the container by warming up the caches and connections for the current read region.<T> CosmosItemResponse<T>
patchItem
(String itemId, PartitionKey partitionKey, CosmosPatchOperations cosmosPatchOperations, CosmosPatchItemRequestOptions options, Class<T> itemType) Run patch operations on an Item.<T> CosmosItemResponse<T>
patchItem
(String itemId, PartitionKey partitionKey, CosmosPatchOperations cosmosPatchOperations, Class<T> itemType) Run patch operations on an Item.<T> CosmosPagedIterable<T>
queryChangeFeed
(CosmosChangeFeedRequestOptions options, Class<T> classType) Query for items in the change feed of the current container using theCosmosChangeFeedRequestOptions
.<T> CosmosPagedIterable<T>
queryItems
(SqlQuerySpec querySpec, CosmosQueryRequestOptions options, Class<T> classType) Query items in the current container returning the results asCosmosPagedIterable
.<T> CosmosPagedIterable<T>
queryItems
(String query, CosmosQueryRequestOptions options, Class<T> classType) Query items in the current container returning the results asCosmosPagedIterable
.read()
Reads the current container.read
(CosmosContainerRequestOptions options) Reads the current container while specifying additional options such as If-Match.<T> CosmosPagedIterable<T>
readAllItems
(PartitionKey partitionKey, CosmosQueryRequestOptions options, Class<T> classType) Reads all the items of a logical partition returning the results asCosmosPagedIterable
.<T> CosmosPagedIterable<T>
readAllItems
(PartitionKey partitionKey, Class<T> classType) Reads all the items of a logical partition returning the results asCosmosPagedIterable
.<T> CosmosItemResponse<T>
readItem
(String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options, Class<T> itemType) Reads an item in the current container while specifying additional options.<T> CosmosItemResponse<T>
readItem
(String itemId, PartitionKey partitionKey, Class<T> itemType) Reads an item in the current container.<T> FeedResponse<T>
readMany
(List<CosmosItemIdentity> itemIdentityList, Class<T> classType) Reads many documents.<T> FeedResponse<T>
readMany
(List<CosmosItemIdentity> itemIdentityList, String sessionToken, Class<T> classType) Reads many documents.Gets the throughput for the current container.replace
(CosmosContainerProperties containerProperties) Replaces the current container properties.replace
(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options) Replaces the current container properties while specifying additional options such as If-Match.<T> CosmosItemResponse<T>
replaceItem
(T item, String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options) Replaces an item in the current container.replaceThroughput
(ThroughputProperties throughputProperties) Sets the throughput for the current container.<T> CosmosItemResponse<T>
upsertItem
(T item) Upserts an Cosmos item in the current container.<T> CosmosItemResponse<T>
upsertItem
(T item, CosmosItemRequestOptions options) Upserts a item Cosmos sync item while specifying additional options.<T> CosmosItemResponse<T>
upsertItem
(T item, PartitionKey partitionKey, CosmosItemRequestOptions options) Upserts a item Cosmos sync item while specifying additional options.
-
Method Details
-
getId
Gets the current container id.- Returns:
- the container id.
-
read
Reads the current container.- Returns:
- the Cosmos container response with the read container.
-
read
Reads the current container while specifying additional options such as If-Match.- Parameters:
options
- the options.- Returns:
- the Cosmos container response.
-
delete
Deletes the current Cosmos container while specifying additional options such as If-Match.- Parameters:
options
- the options.- Returns:
- the cosmos container response.
-
delete
Deletes the current cosmos container.- Returns:
- the cosmos container response.
-
replace
Replaces the current container properties.- Parameters:
containerProperties
- the container properties.- Returns:
- the cosmos container response.
-
replace
public CosmosContainerResponse replace(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options) Replaces the current container properties while specifying additional options such as If-Match.- Parameters:
containerProperties
- the container properties.options
- the options.- Returns:
- the cosmos container response.
-
replaceThroughput
Sets the throughput for the current container.- Parameters:
throughputProperties
- the throughput properties.- Returns:
- the throughput response.
-
readThroughput
Gets the throughput for the current container.- Returns:
- the throughput response.
-
createItem
Creates a new item synchronously and returns its respective Cosmos item response.- Type Parameters:
T
- the type parameter- Parameters:
item
- the item- Returns:
- the Cosmos item response
-
createItem
public <T> CosmosItemResponse<T> createItem(T item, PartitionKey partitionKey, CosmosItemRequestOptions options) Creates a new item synchronously and returns its respective Cosmos item response while specifying additional options.- Type Parameters:
T
- the type parameter.- Parameters:
item
- the item.partitionKey
- the partition key.options
- the options.- Returns:
- the Cosmos item response.
-
createItem
Creates a new item synchronously and returns its respective Cosmos item response while specifying additional options.The partition key value will be automatically extracted from the item's content.
- Type Parameters:
T
- the type parameter.- Parameters:
item
- the item.options
- the options.- Returns:
- the cosmos item response.
-
upsertItem
Upserts an Cosmos item in the current container.- Type Parameters:
T
- the type parameter.- Parameters:
item
- the item.- Returns:
- the Cosmos item response.
-
upsertItem
Upserts a item Cosmos sync item while specifying additional options.- Type Parameters:
T
- the type parameter.- Parameters:
item
- the item.options
- the options.- Returns:
- the Cosmos item response.
-
upsertItem
public <T> CosmosItemResponse<T> upsertItem(T item, PartitionKey partitionKey, CosmosItemRequestOptions options) Upserts a item Cosmos sync item while specifying additional options.- Type Parameters:
T
- the type parameter.- Parameters:
item
- the item.partitionKey
- the partitionKey.options
- the options.- Returns:
- the Cosmos item response.
-
queryItems
public <T> CosmosPagedIterable<T> queryItems(String query, CosmosQueryRequestOptions options, Class<T> classType) Query items in the current container returning the results asCosmosPagedIterable
.- Type Parameters:
T
- the type parameter.- Parameters:
query
- the query.options
- the options.classType
- the class type.- Returns:
- the
CosmosPagedIterable
.
-
queryItems
public <T> CosmosPagedIterable<T> queryItems(SqlQuerySpec querySpec, CosmosQueryRequestOptions options, Class<T> classType) Query items in the current container returning the results asCosmosPagedIterable
.- Type Parameters:
T
- the type parameter.- Parameters:
querySpec
- the query spec.options
- the options.classType
- the class type.- Returns:
- the
CosmosPagedIterable
.
-
queryChangeFeed
@Beta(value=V4_12_0, warningText="Preview API - subject to change in non-backwards compatible way") public <T> CosmosPagedIterable<T> queryChangeFeed(CosmosChangeFeedRequestOptions options, Class<T> classType) Query for items in the change feed of the current container using theCosmosChangeFeedRequestOptions
.The next page can be retrieved by calling queryChangeFeed again with a new instance of
CosmosChangeFeedRequestOptions
created from the continuation token of the previously returnedFeedResponse
instance.- Type Parameters:
T
- the type parameter.- Parameters:
options
- the change feed request options.classType
- the class type.- Returns:
- a
CosmosPagedFlux
containing one feed response page
-
readMany
Reads many documents.- Type Parameters:
T
- the type parameter- Parameters:
itemIdentityList
- CosmosItem id and partition key tuple of items that that needs to be readclassType
- class type- Returns:
- a Mono with feed response of cosmos items
-
readMany
public <T> FeedResponse<T> readMany(List<CosmosItemIdentity> itemIdentityList, String sessionToken, Class<T> classType) Reads many documents.- Type Parameters:
T
- the type parameter- Parameters:
itemIdentityList
- CosmosItem id and partition key tuple of items that that needs to be readsessionToken
- the optional Session token - null if the read can be made without specific session tokenclassType
- class type- Returns:
- a Mono with feed response of cosmos items
-
readAllItems
Reads all the items of a logical partition returning the results asCosmosPagedIterable
.- Type Parameters:
T
- the type parameter.- Parameters:
partitionKey
- the partition key value of the documents that need to be readclassType
- the class type.- Returns:
- the
CosmosPagedIterable
.
-
readAllItems
public <T> CosmosPagedIterable<T> readAllItems(PartitionKey partitionKey, CosmosQueryRequestOptions options, Class<T> classType) Reads all the items of a logical partition returning the results asCosmosPagedIterable
.- Type Parameters:
T
- the type parameter.- Parameters:
partitionKey
- the partition key value of the documents that need to be readoptions
- the feed options.classType
- the class type.- Returns:
- the
CosmosPagedIterable
.
-
readItem
public <T> CosmosItemResponse<T> readItem(String itemId, PartitionKey partitionKey, Class<T> itemType) Reads an item in the current container.- Type Parameters:
T
- the type parameter.- Parameters:
itemId
- the item id.partitionKey
- the partition key.itemType
- the class type of item.- Returns:
- the Cosmos item response.
-
readItem
public <T> CosmosItemResponse<T> readItem(String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options, Class<T> itemType) Reads an item in the current container while specifying additional options.- Type Parameters:
T
- the type parameter.- Parameters:
itemId
- the item id.partitionKey
- the partition key.options
- the options.itemType
- the class type of item.- Returns:
- the Cosmos item response.
-
replaceItem
public <T> CosmosItemResponse<T> replaceItem(T item, String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options) Replaces an item in the current container.- Type Parameters:
T
- the type parameter.- Parameters:
item
- the item.itemId
- the item id.partitionKey
- the partition key.options
- the options.- Returns:
- the Cosmos item response.
-
patchItem
public <T> CosmosItemResponse<T> patchItem(String itemId, PartitionKey partitionKey, CosmosPatchOperations cosmosPatchOperations, Class<T> itemType) Run patch operations on an Item.- Type Parameters:
T
- the type parameter.- Parameters:
itemId
- the item id.partitionKey
- the partition key.cosmosPatchOperations
- Represents a container having list of operations to be sequentially applied to the referred Cosmos item.itemType
- the item type.- Returns:
- the Cosmos item resource response with the patched item or an exception.
-
patchItem
public <T> CosmosItemResponse<T> patchItem(String itemId, PartitionKey partitionKey, CosmosPatchOperations cosmosPatchOperations, CosmosPatchItemRequestOptions options, Class<T> itemType) Run patch operations on an Item.- Type Parameters:
T
- the type parameter.- Parameters:
itemId
- the item id.partitionKey
- the partition key.cosmosPatchOperations
- Represents a container having list of operations to be sequentially applied to the referred Cosmos item.options
- the request options.itemType
- the item type.- Returns:
- the Cosmos item resource response with the patched item or an exception.
-
deleteItem
public CosmosItemResponse<Object> deleteItem(String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options) Deletes an item in the current container.- Parameters:
itemId
- the item id.partitionKey
- the partition key.options
- the options.- Returns:
- the Cosmos item response.
-
deleteAllItemsByPartitionKey
@Beta(value=V4_19_0, warningText="Preview API - subject to change in non-backwards compatible way") public CosmosItemResponse<Object> deleteAllItemsByPartitionKey(PartitionKey partitionKey, CosmosItemRequestOptions options) Deletes all items in the Container with the specified partitionKey value. Starts an asynchronous Cosmos DB background operation which deletes all items in the Container with the specified value. The asynchronous Cosmos DB background operation runs using a percentage of user RUs.- Parameters:
partitionKey
- the partition key.options
- the options.- Returns:
- the Cosmos item response
-
deleteItem
Deletes an item in the current container.- Type Parameters:
T
- the type parameter.- Parameters:
item
- the item to be deleted.options
- the options.- Returns:
- the Cosmos item response.
-
executeCosmosBatch
Executes the transactional batch.- Parameters:
cosmosBatch
- Batch having list of operation and partition key which will be executed by this container.- Returns:
- A TransactionalBatchResponse which contains details of execution of the transactional batch.
If the transactional batch executes successfully, the value returned by
CosmosBatchResponse.getStatusCode()
on the response returned will be set to 200}.If an operation within the transactional batch fails during execution, no changes from the batch will be committed and the status of the failing operation is made available by
CosmosBatchResponse.getStatusCode()
or by the exception. To obtain information about the operations that failed in case of some user error like conflict, not found etc, the response can be enumerated. This returnsCosmosBatchOperationResult
instances corresponding to each operation in the transactional batch in the order they were added to the transactional batch. For a result corresponding to an operation within the transactional batch, useCosmosBatchOperationResult.getStatusCode()
to access the status of the operation. If the operation was not executed or it was aborted due to the failure of another operation within the transactional batch, the value of this field will be 424; for the operation that caused the batch to abort, the value of this field will indicate the cause of failure.If there are issues such as request timeouts, Gone, session not available, network failure or if the service somehow returns 5xx then this will throw an exception instead of returning a CosmosBatchResponse.
Use
CosmosBatchResponse.isSuccessStatusCode()
on the response returned to ensure that the transactional batch succeeded.
-
executeCosmosBatch
public CosmosBatchResponse executeCosmosBatch(CosmosBatch cosmosBatch, CosmosBatchRequestOptions requestOptions) Executes the transactional batch.- Parameters:
cosmosBatch
- Batch having list of operation and partition key which will be executed by this container.requestOptions
- Options that apply specifically to batch request.- Returns:
- A CosmosBatchResponse which contains details of execution of the transactional batch.
If the transactional batch executes successfully, the value returned by
CosmosBatchResponse.getStatusCode()
on the response returned will be set to 200}.If an operation within the transactional batch fails during execution, no changes from the batch will be committed and the status of the failing operation is made available by
CosmosBatchResponse.getStatusCode()
or by the exception. To obtain information about the operations that failed in case of some user error like conflict, not found etc, the response can be enumerated. This returnsCosmosBatchOperationResult
instances corresponding to each operation in the transactional batch in the order they were added to the transactional batch. For a result corresponding to an operation within the transactional batch, useCosmosBatchOperationResult.getStatusCode()
to access the status of the operation. If the operation was not executed or it was aborted due to the failure of another operation within the transactional batch, the value of this field will be 424; for the operation that caused the batch to abort, the value of this field will indicate the cause of failure.If there are issues such as request timeouts, Gone, session not available, network failure or if the service somehow returns 5xx then this will throw an exception instead of returning a CosmosBatchResponse.
Use
CosmosBatchResponse.isSuccessStatusCode()
on the response returned to ensure that the transactional batch succeeded.
-
executeBulkOperations
public <TContext> Iterable<CosmosBulkOperationResponse<TContext>> executeBulkOperations(Iterable<CosmosItemOperation> operations) Executes list of operations in Bulk.- Type Parameters:
TContext
- The context for the bulk processing.- Parameters:
operations
- list of operation which will be executed by this container.- Returns:
- An Iterable of
CosmosBulkOperationResponse
which contains operation and it's response or exception.To create a operation which can be executed here, use
CosmosBulkOperations
. For eg. for a upsert operation useCosmosBulkOperations.getUpsertItemOperation(Object, PartitionKey)
We can get the corresponding operation using
To check if the operation had any exception, useCosmosBulkOperationResponse.getOperation()
and it's response usingCosmosBulkOperationResponse.getResponse()
. If the operation was executed successfully, the value returned byCosmosBulkItemResponse.isSuccessStatusCode()
will be true. To get actual status useCosmosBulkItemResponse.getStatusCode()
.CosmosBulkOperationResponse.getException()
to get the exception.
-
executeBulkOperations
public <TContext> Iterable<CosmosBulkOperationResponse<TContext>> executeBulkOperations(Iterable<CosmosItemOperation> operations, CosmosBulkExecutionOptions bulkOptions) Executes list of operations in Bulk.- Type Parameters:
TContext
- The context for the bulk processing.- Parameters:
operations
- list of operation which will be executed by this container.bulkOptions
- Options that apply for this Bulk request which specifies options regarding execution like concurrency, batching size, interval and context.- Returns:
- An Iterable of
CosmosBulkOperationResponse
which contains operation and it's response or exception.To create a operation which can be executed here, use
CosmosBulkOperations
. For eg. for a upsert operation useCosmosBulkOperations.getUpsertItemOperation(Object, PartitionKey)
We can get the corresponding operation using
To check if the operation had any exception, useCosmosBulkOperationResponse.getOperation()
and it's response usingCosmosBulkOperationResponse.getResponse()
. If the operation was executed successfully, the value returned byCosmosBulkItemResponse.isSuccessStatusCode()
will be true. To get actual status useCosmosBulkItemResponse.getStatusCode()
.CosmosBulkOperationResponse.getException()
to get the exception.
-
getScripts
Gets the Cosmos scripts using the current container as context.- Returns:
- the Cosmos sync scripts.
-
getFeedRanges
@Beta(value=V4_9_0, warningText="Preview API - subject to change in non-backwards compatible way") public List<FeedRange> getFeedRanges()Obtains a list ofFeedRange
that can be used to parallelize Feed operations.- Returns:
- An unmodifiable list of
FeedRange
-
enableLocalThroughputControlGroup
@Beta(value=V4_13_0, warningText="Preview API - subject to change in non-backwards compatible way") public void enableLocalThroughputControlGroup(ThroughputControlGroupConfig groupConfig) Enable the throughput control group with local control mode.ThroughputControlGroupConfig groupConfig = new ThroughputControlGroupConfigBuilder() .groupName("localControlGroup") .targetThroughputThreshold(0.1) .build(); container.enableLocalThroughputControlGroup(groupConfig);
- Parameters:
groupConfig
- AGlobalThroughputControlConfig
.
-
enableGlobalThroughputControlGroup
@Beta(value=V4_13_0, warningText="Preview API - subject to change in non-backwards compatible way") public void enableGlobalThroughputControlGroup(ThroughputControlGroupConfig groupConfig, GlobalThroughputControlConfig globalControlConfig) Enable the throughput control group with global control mode. The defined throughput limit will be shared across different clients.ThroughputControlGroupConfig groupConfig = new ThroughputControlGroupConfigBuilder() .groupName("localControlGroup") .targetThroughputThreshold(0.1) .build(); GlobalThroughputControlConfig globalControlConfig = this.client.createGlobalThroughputControlConfigBuilder(database.getId(), container.getId()) .setControlItemRenewInterval(Duration.ofSeconds(5)) .setControlItemExpireInterval(Duration.ofSeconds(10)) .build(); container.enableGlobalThroughputControlGroup(groupConfig, globalControlConfig);
- Parameters:
groupConfig
- The throughput control group configuration, seeGlobalThroughputControlGroup
.globalControlConfig
- The global throughput control configuration, seeGlobalThroughputControlConfig
.
-
openConnectionsAndInitCaches
@Beta(value=V4_14_0, warningText="Preview API - subject to change in non-backwards compatible way") public void openConnectionsAndInitCaches()Initializes the container by warming up the caches and connections for the current read region.
NOTE: This API ideally should be called only once during application initialization before any workload.
In case of any transient error, caller should consume the error and continue the regular workload.
-