Skill 2.1.1
Connect to Azure Cosmos DB for NoSQL by using the SDK and run queries
Key Concepts
A CosmosClient connects to the account endpoint, preferably with DefaultAzureCredential and a data plane role assignment such as Cosmos DB Built-in Data Contributor; data plane roles are assigned with az cosmosdb sql role assignment create, not with ordinary Azure role assignments. The client is created once and reused, because it caches metadata and connections. From the client the code obtains a database proxy and a container proxy, and calls create_item, upsert_item, read_item, replace_item, patch_item and delete_item.
A point read with read_item(item=id, partition_key=pk) is the cheapest way to fetch one item. query_items runs SQL-like queries such as SELECT c.id, c.total FROM c WHERE c.customerId = @cid with parameters; a query that supplies the partition key runs against one partition, while enable_cross_partition_query=True fans out to all of them at higher cost. Results are returned in pages with continuation tokens, and the x-ms-request-charge response header reports the RUs consumed.