Question

Difficulty: MediumPerform Container and Item Operations in Azure Cosmos DB using SDK

You are developing a logistics monitoring solution in C# that tracks cargo container shipments. The container data is stored in Azure Cosmos DB. You need to write a method using the Azure Cosmos DB .NET SDK v3 that configures the connection, accesses the database and container, and performs a point read of a shipment item. In which order should you execute the steps to initialize the client and perform the point read?

  1. 1Create and configure an instance of CosmosClientOptions with the preferred deployment region.
  2. 2Instantiate the CosmosClient using the connection endpoint and the configured client options.
  3. 3Retrieve a reference to the Database object by calling GetDatabase on the initialized client.
  4. 4Retrieve a reference to the Container object by calling GetContainer on the database object.
  5. 5Call ReadItemAsync on the container, passing the shipment ID and the partition key value.

Answer

To perform a point read using the Azure Cosmos DB .NET SDK v3, you must first create and configure the CosmosClientOptions, instantiate the CosmosClient with those options, obtain a Database reference, obtain a Container reference, and finally call ReadItemAsync on the container specifying the item ID and its partition key.
The correct sequence begins with configuring CosmosClientOptions. Next, you instantiate the CosmosClient passing these options. Once the client is active, you navigate down the resource hierarchy by first retrieving the Database object via GetDatabase and then the Container object via GetContainer. Finally, you execute the point read on the Container object using ReadItemAsync with the item ID and partition key.

Step-by-Step Solution

1
Configure client options.
A CosmosClientOptions object is initialized with configurations like preferred regions.
This object is required during the client initialization step if custom settings are needed.
2
Instantiate the CosmosClient.
A thread-safe CosmosClient instance is created to manage connection pooling.
The client is the root object used to interact with the Azure Cosmos DB service.
3
Obtain Database reference.
A Database object is returned.
You must reference the database containing the target container.
4
Obtain Container reference.
A Container object is returned.
Item operations are executed against a specific container, so a container reference is necessary.
5
Perform the point read.
An ItemResponse is returned containing the deserialized shipment item.
ReadItemAsync executes the point read, which requires the item ID and the partition key.

Key Concept

Initializing the Cosmos DB SDK v3 client hierarchy and executing point reads with the Container class.
Rate this question