다음을 통해 공유


CosmosAsyncContainer Class

  • java.lang.Object
    • com.azure.cosmos.CosmosAsyncContainer

public class CosmosAsyncContainer

Provides methods for reading, deleting, and replacing existing Containers. Provides methods for interacting with child resources (Items, Scripts, Conflicts)

Method Summary

Modifier and Type Method and Description
Mono<CosmosItemResponse<T>> createItem(T item)

Creates an item.

Mono<CosmosItemResponse<T>> createItem(T item, CosmosItemRequestOptions options)

Creates a Cosmos item.

Mono<CosmosItemResponse<T>> createItem(T item, PartitionKey partitionKey, CosmosItemRequestOptions options)

Creates an item.

Mono<CosmosItemResponse<Object>> deleteItem(T item, CosmosItemRequestOptions options)

Deletes the item.

Mono<CosmosItemResponse<T>> patchItem(String itemId, PartitionKey partitionKey, CosmosPatchOperations cosmosPatchOperations, CosmosPatchItemRequestOptions options, Class<T> itemType)

Run partial update that modifies specific properties or fields of the item without replacing the entire item.

Mono<CosmosItemResponse<T>> patchItem(String itemId, PartitionKey partitionKey, CosmosPatchOperations cosmosPatchOperations, Class<T> itemType)

Run partial update that modifies specific properties or fields of the item without replacing the entire item.

CosmosPagedFlux<T> queryChangeFeed(CosmosChangeFeedRequestOptions options, Class<T> classType)

Query for items in the change feed of the current container using the CosmosChangeFeedRequestOptions.

CosmosPagedFlux<T> queryItems(SqlQuerySpec querySpec, CosmosQueryRequestOptions options, Class<T> classType)

Query for items in the current container using a SqlQuerySpec and CosmosQueryRequestOptions.

CosmosPagedFlux<T> queryItems(SqlQuerySpec querySpec, Class<T> classType)

Query for items in the current container using a SqlQuerySpec.

CosmosPagedFlux<T> queryItems(String query, CosmosQueryRequestOptions options, Class<T> classType)

Query for items in the current container using a string.

CosmosPagedFlux<T> queryItems(String query, Class<T> classType)

Query for items in the current container.

CosmosPagedFlux<T> readAllItems(PartitionKey partitionKey, CosmosQueryRequestOptions options, Class<T> classType)

Reads all the items of a logical partition

CosmosPagedFlux<T> readAllItems(PartitionKey partitionKey, Class<T> classType)

Reads all the items of a logical partition

Mono<CosmosItemResponse<T>> readItem(String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options, Class<T> itemType)

Reads an item by itemId using a configured CosmosItemRequestOptions.

Mono<CosmosItemResponse<T>> readItem(String itemId, PartitionKey partitionKey, Class<T> itemType)

Reads an item by itemId.

Mono<FeedResponse<T>> readMany(List<CosmosItemIdentity> itemIdentityList, CosmosReadManyRequestOptions requestOptions, Class<T> classType)

Reads many documents.

Mono<FeedResponse<T>> readMany(List<CosmosItemIdentity> itemIdentityList, Class<T> classType)

Reads many documents.

Mono<FeedResponse<T>> readMany(List<CosmosItemIdentity> itemIdentityList, String sessionToken, Class<T> classType)

Reads many documents.

Mono<CosmosItemResponse<T>> replaceItem(T item, String itemId, PartitionKey partitionKey)

Replaces an existing item in a container with a new item.

Mono<CosmosItemResponse<T>> replaceItem(T item, String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options)

Replaces an existing item in a container with a new item.

Mono<CosmosItemResponse<T>> upsertItem(T item)

Upserts an item.

Mono<CosmosItemResponse<T>> upsertItem(T item, CosmosItemRequestOptions options)

Upserts an item.

Mono<CosmosItemResponse<T>> upsertItem(T item, PartitionKey partitionKey, CosmosItemRequestOptions options)

Upserts an item.

Flux<CosmosBulkOperationResponse<TContext>> executeBulkOperations(Flux<CosmosItemOperation> operations)

Executes flux of operations in Bulk.

Flux<CosmosBulkOperationResponse<TContext>> executeBulkOperations(Flux<CosmosItemOperation> operations, CosmosBulkExecutionOptions bulkOptions)

Executes flux of operations in Bulk.

Mono<CosmosContainerResponse> delete()

Deletes the current container.

Mono<CosmosContainerResponse> delete(CosmosContainerRequestOptions options)

Deletes the container

Mono<CosmosItemResponse<Object>> deleteAllItemsByPartitionKey(PartitionKey partitionKey, CosmosItemRequestOptions options)

Deletes all items in the Container with the specified partitionKey value.

Mono<CosmosItemResponse<Object>> deleteItem(String itemId, PartitionKey partitionKey)

Deletes an item.

Mono<CosmosItemResponse<Object>> deleteItem(String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options)

Deletes the item.

void enableGlobalThroughputControlGroup(ThroughputControlGroupConfig groupConfig, GlobalThroughputControlConfig globalControlConfig)

Enable the throughput control group with global control mode.

void enableLocalThroughputControlGroup(ThroughputControlGroupConfig groupConfig)

Enable the throughput control group with local control mode.

Mono<CosmosBatchResponse> executeCosmosBatch(CosmosBatch cosmosBatch)

Executes the transactional batch.

Mono<CosmosBatchResponse> executeCosmosBatch(CosmosBatch cosmosBatch, CosmosBatchRequestOptions requestOptions)

Executes the transactional batch.

CosmosAsyncConflict getConflict(String id)

Gets a CosmosAsyncConflict object using current container for context.

CosmosAsyncDatabase getDatabase()

Gets the parent CosmosAsyncDatabase for the current container.

Mono<List<FeedRange>> getFeedRanges()

Obtains a list of FeedRange that can be used to parallelize Feed operations.

String getId()

Get the id of the CosmosAsyncContainer.

CosmosAsyncScripts getScripts()

Gets a CosmosAsyncScripts using the current container as context.

Mono<Void> openConnectionsAndInitCaches()

Deprecated

Best effort to initialize the container by warming up the caches and connections for the current read region.

Mono<Void> openConnectionsAndInitCaches(int numProactiveConnectionRegions)

Deprecated

Best effort to initialize the container by warming up the caches and connections to a specified no.

CosmosPagedFlux<CosmosConflictProperties> queryConflicts(String query)

Queries all the conflicts in the current container.

CosmosPagedFlux<CosmosConflictProperties> queryConflicts(String query, CosmosQueryRequestOptions options)

Queries all the conflicts in the current container.

Mono<CosmosContainerResponse> read()

Reads the current container.

Mono<CosmosContainerResponse> read(CosmosContainerRequestOptions options)

Reads the current container while specifying additional options such as If-Match.

CosmosPagedFlux<CosmosConflictProperties> readAllConflicts(CosmosQueryRequestOptions options)

Lists all the conflicts in the current container.

Mono<ThroughputResponse> readThroughput()

Read the throughput provisioned for the current container.

Mono<CosmosContainerResponse> replace(CosmosContainerProperties containerProperties)

Replaces the current container's properties.

Mono<CosmosContainerResponse> replace(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options)

Replaces the current container properties while using non-default request options.

Mono<ThroughputResponse> replaceThroughput(ThroughputProperties throughputProperties)

Replace the throughput.

Methods inherited from java.lang.Object

Method Details

createItem

public Mono> createItem(T item)

Creates an item.

After subscription the operation will be performed. The Mono upon successful completion will contain a single resource response with the created Cosmos item. In case of failure the Mono will error.

Parameters:

item - the Cosmos item represented as a POJO or Cosmos item object.

Returns:

an Mono containing the single resource response with the created Cosmos item or an error.

createItem

public Mono> createItem(T item, CosmosItemRequestOptions options)

Creates a Cosmos item.

Parameters:

item - the item.
options - the item request options.

Returns:

an Mono containing the single resource response with the created Cosmos item or an error.

createItem

public Mono> createItem(T item, PartitionKey partitionKey, CosmosItemRequestOptions options)

Creates an item.

After subscription the operation will be performed. The Mono upon successful completion will contain a single resource response with the created Cosmos item. In case of failure the Mono will error.

Parameters:

item - the Cosmos item represented as a POJO or Cosmos item object.
partitionKey - the partition key.
options - the request options.

Returns:

an Mono containing the single resource response with the created Cosmos item or an error.

deleteItem

public Mono> deleteItem(T item, CosmosItemRequestOptions options)

Deletes the item.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos item response for the deleted item.

Parameters:

item - item to be deleted.
options - the request options.

Returns:

an Mono containing the Cosmos item resource response.

patchItem

public Mono> patchItem(String itemId, PartitionKey partitionKey, CosmosPatchOperations cosmosPatchOperations, CosmosPatchItemRequestOptions options, Class itemType)

Run partial update that modifies specific properties or fields of the item without replacing the entire item.

CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();

 cosmosPatchOperations
     .add("/departure", "SEA")
     .increment("/trips", 1);

 cosmosAsyncContainer.patchItem(
         passenger.getId(),
         new PartitionKey(passenger.getId()),
         cosmosPatchOperations,
         Passenger.class)
     .subscribe(response -> {
         System.out.println(response);
     }, throwable -> {
         throwable.printStackTrace();
     });

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos item response with the patched item.

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:

an Mono containing the Cosmos item resource response with the patched item or an error.

patchItem

public Mono> patchItem(String itemId, PartitionKey partitionKey, CosmosPatchOperations cosmosPatchOperations, Class itemType)

Run partial update that modifies specific properties or fields of the item without replacing the entire item.

CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();

 cosmosPatchOperations
     .add("/departure", "SEA")
     .increment("/trips", 1);

 cosmosAsyncContainer.patchItem(
         passenger.getId(),
         new PartitionKey(passenger.getId()),
         cosmosPatchOperations,
         Passenger.class)
     .subscribe(response -> {
         System.out.println(response);
     }, throwable -> {
         throwable.printStackTrace();
     });

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos item response with the patched item.

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:

an Mono containing the Cosmos item resource response with the patched item or an error.

queryChangeFeed

public CosmosPagedFlux queryChangeFeed(CosmosChangeFeedRequestOptions options, Class classType)

Query for items in the change feed of the current container using the CosmosChangeFeedRequestOptions.

CosmosChangeFeedRequestOptions options = CosmosChangeFeedRequestOptions
     .createForProcessingFromNow(FeedRange.forFullRange())
     .allVersionsAndDeletes();

 cosmosAsyncContainer.queryChangeFeed(options, Passenger.class)
     .byPage()
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Flux.empty();
     })
     .subscribe();

After subscription the operation will be performed. The Flux will contain one or several feed response of the obtained items. In case of failure the CosmosPagedFlux<T> will error.

Parameters:

options - the change feed request options.
classType - the class type.

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the obtained items or an error.

queryItems

public CosmosPagedFlux queryItems(SqlQuerySpec querySpec, CosmosQueryRequestOptions options, Class classType)

Query for items in the current container using a SqlQuerySpec and CosmosQueryRequestOptions.

After subscription the operation will be performed. The Flux will contain one or several feed response of the obtained items. In case of failure the CosmosPagedFlux<T> will error.

Parameters:

querySpec - the SQL query specification.
options - the query request options.
classType - the class type.

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the obtained items or an error.

queryItems

public CosmosPagedFlux queryItems(SqlQuerySpec querySpec, Class classType)

Query for items in the current container using a SqlQuerySpec.

CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();

 String query = "SELECT * FROM Passenger p WHERE (p.departure = @departure)";
 List<SqlParameter> parameters = Collections.singletonList(new SqlParameter("@departure", "SEA"));
 SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(query, parameters);

 cosmosAsyncContainer.queryItems(sqlQuerySpec, options, Passenger.class)
     .byPage()
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Flux.empty();
     })
     .subscribe();

After subscription the operation will be performed. The CosmosPagedFlux<T> will contain one or several feed response of the obtained items. In case of failure the CosmosPagedFlux<T> will error.

Parameters:

querySpec - the SQL query specification.
classType - the class type.

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the obtained items or an error.

queryItems

public CosmosPagedFlux queryItems(String query, CosmosQueryRequestOptions options, Class classType)

Query for items in the current container using a string.

CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
 String query = "SELECT * FROM Passenger WHERE Passenger.departure IN ('SEA', 'IND')";
 cosmosAsyncContainer.queryItems(query, options, Passenger.class)
     .byPage()
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Flux.empty();
     })
     .subscribe();

After subscription the operation will be performed. The CosmosPagedFlux<T> will contain one or several feed response of the obtained items. In case of failure the CosmosPagedFlux<T> will error.

Parameters:

query - the query.
options - the query request options.
classType - the class type.

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the obtained items or an error.

queryItems

public CosmosPagedFlux queryItems(String query, Class classType)

Query for items in the current container.

CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
 String query = "SELECT * FROM Passenger WHERE Passenger.departure IN ('SEA', 'IND')";
 cosmosAsyncContainer.queryItems(query, options, Passenger.class)
     .byPage()
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Flux.empty();
     })
     .subscribe();

After subscription the operation will be performed. The CosmosPagedFlux<T> will contain one or several feed response of the obtained items. In case of failure the CosmosPagedFlux<T> will error.

Parameters:

query - the query.
classType - the class type.

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the obtained items or an error.

readAllItems

public CosmosPagedFlux readAllItems(PartitionKey partitionKey, CosmosQueryRequestOptions options, Class classType)

Reads all the items of a logical partition

cosmosAsyncContainer
     .readAllItems(new PartitionKey(partitionKey), Passenger.class)
     .byPage(100)
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Flux.empty();
     })
     .subscribe();

After subscription the operation will be performed. The CosmosPagedFlux<T> will contain one or several feed responses of the read Cosmos items. In case of failure the CosmosPagedFlux<T> will error.

Parameters:

partitionKey - the partition key value of the documents that need to be read
options - the feed options (Optional).
classType - the class type.

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the read Cosmos items or an error.

readAllItems

public CosmosPagedFlux readAllItems(PartitionKey partitionKey, Class classType)

Reads all the items of a logical partition

cosmosAsyncContainer
     .readAllItems(new PartitionKey(partitionKey), Passenger.class)
     .byPage(100)
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Flux.empty();
     })
     .subscribe();

After subscription the operation will be performed. The CosmosPagedFlux<T> will contain one or several feed responses of the read Cosmos items. In case of failure the CosmosPagedFlux<T> will error.

Parameters:

partitionKey - the partition key value of the documents that need to be read
classType - the class type.

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the read Cosmos items or an error.

readItem

public Mono> readItem(String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options, Class itemType)

Reads an item by itemId using a configured CosmosItemRequestOptions. This operation is used to retrieve a single item from a container based on its unique identifier (ID) and partition key. The readItem operation provides direct access to a specific item using its unique identifier, which consists of the item's ID and the partition key value. This operation is efficient for retrieving a known item by its ID and partition key without the need for complex querying.

After subscription the operation will be performed. The Mono upon successful completion will contain a Cosmos item response with the read item.

Parameters:

itemId - the item id.
partitionKey - the partition key.
options - the request (Optional) CosmosItemRequestOptions.
itemType - the item type.

Returns:

an Mono containing the Cosmos item response with the read item or an error.

readItem

public Mono> readItem(String itemId, PartitionKey partitionKey, Class itemType)

Reads an item by itemId. This operation is used to retrieve a single item from a container based on its unique identifier (ID) and partition key. The readItem operation provides direct access to a specific item using its unique identifier, which consists of the item's ID and the partition key value. This operation is efficient for retrieving a known item by its ID and partition key without the need for complex querying.

After subscription the operation will be performed. The Mono upon successful completion will contain an item response with the read item.

// Read an item
 cosmosAsyncContainer.readItem(passenger.getId(), new PartitionKey(passenger.getId()), Passenger.class)
     .flatMap(response -> Mono.just(response.getItem()))
     .subscribe(passengerItem -> System.out.println(passengerItem), throwable -> {
         CosmosException cosmosException = (CosmosException) throwable;
         cosmosException.printStackTrace();
     });
 // ...

Parameters:

itemId - the item id.
partitionKey - the partition key.
itemType - the item type.

Returns:

an Mono containing the Cosmos item response with the read item or an error.

readMany

public Mono> readMany(List itemIdentityList, CosmosReadManyRequestOptions requestOptions, Class classType)

Reads many documents. Useful for reading many documents with a particular id and partition key in a single request. If any document from the list is missing, no exception will be thrown.

List<CosmosItemIdentity> itemIdentityList = new ArrayList<>();
 itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger1Id), passenger1Id));
 itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger2Id), passenger2Id));

 cosmosAsyncContainer.readMany(itemIdentityList, Passenger.class)
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Mono.empty();
     })
     .subscribe();

Parameters:

itemIdentityList - CosmosItem id and partition key tuple of items that that needs to be read
requestOptions - the optional request option
classType - class type

Returns:

a Mono with feed response of cosmos items or error

readMany

public Mono> readMany(List itemIdentityList, Class classType)

Reads many documents. Useful for reading many documents with a particular id and partition key in a single request. If any document from the list is missing, no exception will be thrown.

List<CosmosItemIdentity> itemIdentityList = new ArrayList<>();
 itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger1Id), passenger1Id));
 itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger2Id), passenger2Id));

 cosmosAsyncContainer.readMany(itemIdentityList, Passenger.class)
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Mono.empty();
     })
     .subscribe();

Parameters:

itemIdentityList - CosmosItem id and partition key tuple of items that that needs to be read
classType - class type

Returns:

a Mono with feed response of cosmos items

readMany

public Mono> readMany(List itemIdentityList, String sessionToken, Class classType)

Reads many documents. Useful for reading many documents with a particular id and partition key in a single request. If any document from the list is missing, no exception will be thrown.

List<CosmosItemIdentity> itemIdentityList = new ArrayList<>();
 itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger1Id), passenger1Id));
 itemIdentityList.add(new CosmosItemIdentity(new PartitionKey(passenger2Id), passenger2Id));

 cosmosAsyncContainer.readMany(itemIdentityList, Passenger.class)
     .flatMap(passengerFeedResponse -> {
         for (Passenger passenger : passengerFeedResponse.getResults()) {
             System.out.println(passenger);
         }
         return Mono.empty();
     })
     .subscribe();

Parameters:

itemIdentityList - CosmosItem id and partition key tuple of items that that needs to be read
sessionToken - the optional Session token - null if the read can be made without specific session token
classType - class type

Returns:

a Mono with feed response of cosmos items or error

replaceItem

public Mono> replaceItem(T item, String itemId, PartitionKey partitionKey)

Replaces an existing item in a container with a new item. It performs a complete replacement of the item, replacing all its properties with the properties of the new item

cosmosAsyncContainer.replaceItem(
         newPassenger,
         oldPassenger.getId(),
         new PartitionKey(oldPassenger.getId()),
         new CosmosItemRequestOptions())
     .subscribe(response -> {
         System.out.println(response);
     }, throwable -> {
         throwable.printStackTrace();
     });

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos item response with the replaced item.

Parameters:

item - the item to replace (containing the item id).
itemId - the item id.
partitionKey - the partition key.

Returns:

an Mono containing the Cosmos item resource response with the replaced item or an error.

replaceItem

public Mono> replaceItem(T item, String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options)

Replaces an existing item in a container with a new item. It performs a complete replacement of the item, replacing all its properties with the properties of the new item

cosmosAsyncContainer.replaceItem(
         newPassenger,
         oldPassenger.getId(),
         new PartitionKey(oldPassenger.getId()),
         new CosmosItemRequestOptions())
     .subscribe(response -> {
         System.out.println(response);
     }, throwable -> {
         throwable.printStackTrace();
     });

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos item response with the replaced item.

Parameters:

item - the item to replace (containing the item id).
itemId - the item id.
partitionKey - the partition key.
options - the request comosItemRequestOptions (Optional).

Returns:

an Mono containing the Cosmos item resource response with the replaced item or an error.

upsertItem

public Mono> upsertItem(T item)

Upserts an item.

After subscription the operation will be performed. The Mono upon successful completion will contain a single resource response with the upserted item. In case of failure the Mono will error.

Parameters:

item - the item represented as a POJO or Item object to upsert.

Returns:

an Mono containing the single resource response with the upserted item or an error.

upsertItem

public Mono> upsertItem(T item, CosmosItemRequestOptions options)

Upserts an item.

After subscription the operation will be performed. The Mono upon successful completion will contain a single resource response with the upserted item. In case of failure the Mono will error.

Parameters:

item - the item represented as a POJO or Item object to upsert.
options - the request options.

Returns:

an Mono containing the single resource response with the upserted item or an error.

upsertItem

public Mono> upsertItem(T item, PartitionKey partitionKey, CosmosItemRequestOptions options)

Upserts an item.

After subscription the operation will be performed. The Mono upon successful completion will contain a single resource response with the upserted item. In case of failure the Mono will error.

Parameters:

item - the item represented as a POJO or Item object to upsert.
partitionKey - the partition key.
options - the request options.

Returns:

an Mono containing the single resource response with the upserted item or an error.

executeBulkOperations

public Flux> executeBulkOperations(Flux operations)

Executes flux of operations in Bulk.

Parameters:

operations - Flux of operation which will be executed by this container.

Returns:

A Flux of CosmosBulkOperationResponse<TContext> 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 use com.azure.cosmos.models.CosmosBulkOperations#getUpsertItemOperation(Object, PartitionKey) We can get the corresponding operation using getOperation() and it's response using getResponse(). If the operation was executed successfully, the value returned by isSuccessStatusCode() will be true. To get actual status use getStatusCode(). To check if the operation had any exception, use getException() to get the exception.

executeBulkOperations

public Flux> executeBulkOperations(Flux operations, CosmosBulkExecutionOptions bulkOptions)

Executes flux of operations in Bulk.

Parameters:

operations - Flux 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:

A Flux of CosmosBulkOperationResponse<TContext> 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 use com.azure.cosmos.models.CosmosBulkOperations#getUpsertItemOperation(Object, PartitionKey) We can get the corresponding operation using getOperation() and it's response using getResponse(). If the operation was executed successfully, the value returned by isSuccessStatusCode() will be true. To get actual status use getStatusCode(). To check if the operation had any exception, use getException() to get the exception.

delete

public Mono delete()

Deletes the current container.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos container response for the deleted container. In case of failure the Mono will error.

Returns:

an Mono containing the single Cosmos container response for the deleted container or an error.

delete

public Mono delete(CosmosContainerRequestOptions options)

Deletes the container

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos container response for the deleted database. In case of failure the Mono will error.

Parameters:

options - the request options.

Returns:

an Mono containing the single Cosmos container response for the deleted database or an error.

deleteAllItemsByPartitionKey

public Mono> 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.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos item response for all the deleted items.

Parameters:

partitionKey - partitionKey of the item.
options - the request options.

Returns:

an Mono containing the Cosmos item resource response.

deleteItem

public Mono> deleteItem(String itemId, PartitionKey partitionKey)

Deletes an item.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos item response for the deleted item.

cosmosAsyncContainer.deleteItem(
     passenger.getId(),
     new PartitionKey(passenger.getId())
 ).subscribe(response -> {
     System.out.println(response);
 }, throwable -> {
     CosmosException cosmosException = (CosmosException) throwable;
     cosmosException.printStackTrace();
 });

Parameters:

itemId - the item id.
partitionKey - the partition key.

Returns:

an Mono containing the Cosmos item resource response.

deleteItem

public Mono> deleteItem(String itemId, PartitionKey partitionKey, CosmosItemRequestOptions options)

Deletes the item.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos item response for the deleted item.

Parameters:

itemId - id of the item.
partitionKey - partitionKey of the item.
options - the request options (Optional).

Returns:

an Mono containing the Cosmos item resource response.

enableGlobalThroughputControlGroup

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, see GlobalThroughputControlGroup.
globalControlConfig - The global throughput control configuration, see GlobalThroughputControlConfig.

enableLocalThroughputControlGroup

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 - A ThroughputControlGroupConfig.

executeCosmosBatch

public Mono executeCosmosBatch(CosmosBatch cosmosBatch)

Executes the transactional batch.

Parameters:

cosmosBatch - Batch having list of operation and partition key which will be executed by this container.

Returns:

A Mono response 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 returns CosmosBatchOperationResult 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, use CosmosBatchOperationResult#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 the Mono will return error instead of CosmosBatchResponse.

Use CosmosBatchResponse#isSuccessStatusCode on the response returned to ensure that the transactional batch succeeded.

executeCosmosBatch

public Mono 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 Mono response 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 returns CosmosBatchOperationResult 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, use CosmosBatchOperationResult#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 the Mono will return error instead of CosmosBatchResponse.

Use CosmosBatchResponse#isSuccessStatusCode on the response returned to ensure that the transactional batch succeeded.

getConflict

public CosmosAsyncConflict getConflict(String id)

Gets a CosmosAsyncConflict object using current container for context.

Parameters:

id - the id of the Cosmos conflict.

Returns:

a Cosmos conflict.

getDatabase

public CosmosAsyncDatabase getDatabase()

Gets the parent CosmosAsyncDatabase for the current container.

Returns:

getFeedRanges

public Mono> getFeedRanges()

Obtains a list of FeedRange that can be used to parallelize Feed operations.

cosmosAsyncContainer.getFeedRanges()
     .subscribe(feedRanges -> {
         for (FeedRange feedRange : feedRanges) {
             System.out.println("Feed range: " + feedRange);
         }
     });

Returns:

An unmodifiable list of FeedRange

getId

public String getId()

Get the id of the CosmosAsyncContainer.

Returns:

the id of the CosmosAsyncContainer.

getScripts

public CosmosAsyncScripts getScripts()

Gets a CosmosAsyncScripts using the current container as context.

This can be further used to perform various operations on Cosmos scripts.

Returns:

openConnectionsAndInitCaches

@Deprecated
public Mono openConnectionsAndInitCaches()

Deprecated

Best effort to initialize the container by warming up the caches and connections for the current read region.

Depending on how many partitions the container has, the total time needed will also change. But generally you can use the following formula to get an estimated time: If it took 200ms to establish a connection, and you have 100 partitions in your container then it will take around (100 * 4 / CPUCores) * 200ms to open all connections after get the address list

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.

Returns:

Mono of Void.

openConnectionsAndInitCaches

@Deprecated
public Mono openConnectionsAndInitCaches(int numProactiveConnectionRegions)

Deprecated

Best effort to initialize the container by warming up the caches and connections to a specified no. of regions from the preferred list of regions.

Depending on how many partitions the container has, the total time needed will also change. But generally you can use the following formula to get an estimated time: If it took 200ms to establish a connection, and you have 100 partitions in your container then it will take around (100 * 4 / (10 * CPUCores)) * 200ms * RegionsWithProactiveConnections to open all connections after get the address list

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.

In order to minimize latencies associated with warming up caches and opening connections the no. of proactive connection regions cannot be more than CosmosContainerProactiveInitConfigBuilder#MAX_NO_OF_PROACTIVE_CONNECTION_REGIONS.

Parameters:

numProactiveConnectionRegions - the no of regions to proactively connect to

Returns:

Mono of Void.

queryConflicts

public CosmosPagedFlux queryConflicts(String query)

Queries all the conflicts in the current container.

try {
     cosmosAsyncContainer.queryConflicts(query).
         byPage(100)
         .subscribe(response -> {
             for (CosmosConflictProperties conflictProperties : response.getResults()) {
                 System.out.println(conflictProperties);
             }
         }, throwable -> {
             throwable.printStackTrace();
         });
 } catch (CosmosException ce) {
     ce.printStackTrace();
 } catch (Exception e) {
     e.printStackTrace();
 }

Parameters:

query - the query.

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the obtained conflicts or an error.

queryConflicts

public CosmosPagedFlux queryConflicts(String query, CosmosQueryRequestOptions options)

Queries all the conflicts in the current container.

Parameters:

query - the query.
options - the query request options (Optional).

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the obtained conflicts or an error.

read

public Mono read()

Reads the current container.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos container response with the read container. In case of failure the Mono will error.

Returns:

an Mono containing the single Cosmos container response with the read container or an error.

read

public Mono read(CosmosContainerRequestOptions options)

Reads the current container while specifying additional options such as If-Match.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos container response with the read container. In case of failure the Mono will error.

Parameters:

options - the Cosmos container request options.

Returns:

an Mono containing the single Cosmos container response with the read container or an error.

readAllConflicts

public CosmosPagedFlux readAllConflicts(CosmosQueryRequestOptions options)

Lists all the conflicts in the current container.

try {
     cosmosAsyncContainer.readAllConflicts(options).
         byPage(100)
         .subscribe(response -> {
             for (CosmosConflictProperties conflictProperties : response.getResults()) {
                 System.out.println(conflictProperties);
             }
         }, throwable -> {
             throwable.printStackTrace();
         });
 } catch (CosmosException ce) {
     ce.printStackTrace();
 } catch (Exception e) {
     e.printStackTrace();
 }

Parameters:

options - the query request options

Returns:

a CosmosPagedFlux<T> containing one or several feed response pages of the obtained conflicts or an error.

readThroughput

public Mono readThroughput()

Read the throughput provisioned for the current container.

Mono<ThroughputResponse> throughputResponseMono = cosmosAsyncContainer.readThroughput();
 throughputResponseMono.subscribe(throughputResponse -> {
     System.out.println(throughputResponse);
 }, throwable -> {
     throwable.printStackTrace();
 });

Returns:

the mono containing throughput response.

replace

public Mono replace(CosmosContainerProperties containerProperties)

Replaces the current container's properties.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos container response with the replaced container properties. In case of failure the Mono will error.

Parameters:

containerProperties - the container properties

Returns:

an Mono containing the single Cosmos container response with the replaced container properties or an error.

replace

public Mono replace(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options)

Replaces the current container properties while using non-default request options.

After subscription the operation will be performed. The Mono upon successful completion will contain a single Cosmos container response with the replaced container properties. In case of failure the Mono will error.

Parameters:

containerProperties - the container properties
options - the Cosmos container request options.

Returns:

an Mono containing the single Cosmos container response with the replaced container properties or an error.

replaceThroughput

public Mono replaceThroughput(ThroughputProperties throughputProperties)

Replace the throughput.

ThroughputProperties throughputProperties =
     ThroughputProperties.createAutoscaledThroughput(1000);

 cosmosAsyncContainer.replaceThroughput(throughputProperties)
     .subscribe(throughputResponse -> {
             System.out.println(throughputResponse);
         },
         throwable -> {
             throwable.printStackTrace();
         });

Parameters:

throughputProperties - the throughput properties.

Returns:

the mono containing throughput response.

Applies to