Question

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

You are developing a C# console application that processes IoT telemetry using the Azure Cosmos DB .NET SDK v3.

The application must connect to a database named `TelemetryDb` and a container named `DeviceData`. The container's partition key path is set to `/deviceId`.

You need to write code to retrieve a single telemetry reading document with an ID of `device-reading-101` and a partition key value of `device-id-55`.

Arrange the following steps in the correct order to configure the SDK, execute the point read operation, and retrieve the deserialized telemetry data.

  1. 1Instantiate the `CosmosClient` class by passing the connection string, configuring it to be reused as a singleton.
  2. 2Call `GetDatabase` on the `CosmosClient` instance, passing the database name `TelemetryDb` to retrieve a `Database` object reference.
  3. 3Call `GetContainer` on the `Database` instance, passing the container name `DeviceData` to retrieve a `Container` object reference.
  4. 4Call `ReadItemAsync<DeviceReading>` on the `Container` instance, passing the item ID `device-reading-101` and a new `PartitionKey` initialized with `device-id-55`.
  5. 5Access the `Resource` property of the returned `ItemResponse<DeviceReading>` object to obtain the deserialized telemetry data payload.

Answer

To retrieve the telemetry data, you must first initialize a CosmosClient. Then, call GetDatabase to get a Database reference, and call GetContainer on that database to get a Container reference. Execute the point read by calling ReadItemAsync on the container with the item ID and the PartitionKey. Finally, retrieve the deserialized object by accessing the Resource property of the response.
The correct order follows the logical hierarchy of the Azure Cosmos DB SDK v3: first, a CosmosClient is initialized to manage connections; then, a Database reference is retrieved; next, a Container reference is obtained; after that, ReadItemAsync is called on the container using the document's ID and its partition key; finally, the Resource property of the response is accessed to get the deserialized entity.

Step-by-Step Solution

1
Initialize CosmosClient
A thread-safe, singleton client instance connected to Azure Cosmos DB
The CosmosClient acts as the entry point to the Azure Cosmos DB service and maintains connection pools.
2
Obtain Database reference
A Database object representing TelemetryDb
You must navigate the hierarchy from client to database before referencing a container.
3
Obtain Container reference
A Container object representing DeviceData
Item operations are executed against a specific Container object, which is a child of the database.
4
Call ReadItemAsync
An ItemResponse containing the status and payload of the read
The point read must be executed by passing both the item ID and the partition key value as a PartitionKey object.
5
Access Resource property
The deserialized DeviceReading object
The ItemResponse contains metadata and headers; the actual document payload is accessed via the Resource property.

Key Concept

Point read operations using the Azure Cosmos DB .NET SDK v3 hierarchy
Estimated Time:2m 0s
Rate this question