Question

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

You are developing a C# console application that retrieves a specific product configuration document from an Azure Cosmos DB container by using the .NET SDK v3. You need to write the code that performs a point read of the item. Which sequence of actions should you perform to complete this operation? To answer, arrange the actions in the correct order.

  1. 1Instantiate a CosmosClient object using the Azure Cosmos DB account endpoint URI and authentication credentials.
  2. 2Call GetDatabase on the CosmosClient object to obtain a reference to the target database.
  3. 3Call GetContainer on the Database object to obtain a reference to the target container.
  4. 4Instantiate a PartitionKey structure using the partition key value of the target document.
  5. 5Call ReadItemAsync on the Container object, passing the item's unique identifier and the PartitionKey object.

Answer

To perform a point read, you must first instantiate a CosmosClient. Next, call GetDatabase on the CosmosClient to retrieve the Database reference. Then, call GetContainer on the Database to retrieve the Container reference. After that, initialize a PartitionKey structure with the partition key value. Finally, call ReadItemAsync on the Container, passing the unique identifier and the PartitionKey.
The correct sequence starts with instantiating the CosmosClient, which establishes the connection pool. You then call GetDatabase on the client and GetContainer on the database to navigate the SDK hierarchy. Before executing the read, you instantiate the PartitionKey with the target value. Finally, you execute the point read by calling ReadItemAsync on the container with the item ID and PartitionKey.

Step-by-Step Solution

1
Instantiate the CosmosClient object.
A CosmosClient instance is created, initiating the connection and client-side caching.
The client is the root object required to interact with any Azure Cosmos DB resources.
2
Call GetDatabase on the CosmosClient instance.
A Database object reference is returned.
You must navigate the resource hierarchy from the client down to the database before accessing containers.
3
Call GetContainer on the Database instance.
A Container object reference is returned.
All item-level operations are executed against a container reference.
4
Create a PartitionKey instance with the item's partition key value.
A PartitionKey structure is initialized.
The SDK v3 requires an explicit PartitionKey parameter for point operations to ensure efficient routing.
5
Call ReadItemAsync on the Container instance, passing the ID and PartitionKey.
An ItemResponse is returned, containing the document data.
This executing call performs the actual point read operation over the network.

Key Concept

Executing container and item operations using the Azure Cosmos DB .NET SDK v3 requires initializing the CosmosClient, obtaining Database and Container references, and providing a PartitionKey to the item operation method.
Rate this question