Skip to content

Commit 00bfbb4

Browse files
committed
Add async code to NoSQL+Python quickstart
1 parent 29345e7 commit 00bfbb4

File tree

1 file changed

+97
-2
lines changed

1 file changed

+97
-2
lines changed

articles/cosmos-db/nosql/quickstart-python.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.service: cosmos-db
77
ms.subservice: nosql
88
ms.devlang: python
99
ms.topic: quickstart
10-
ms.date: 11/03/2022
10+
ms.date: 1/17/2023
1111
ms.custom: seodec18, seo-javascript-september2019, seo-python-october2019, devx-track-python, mode-api, ignite-2022, devguide-python, cosmos-db-dev-journey
1212
---
1313

@@ -89,6 +89,8 @@ For this sample code, the container will use the category as a logical partition
8989

9090
### Authenticate the client
9191

92+
#### [Sync](#tab/sync)
93+
9294
From the project directory, open the *app.py* file. In your editor, import the `os` and `json` modules. Then, import the `CosmosClient` and `PartitionKey` classes from the `azure.cosmos` module.
9395

9496
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="imports":::
@@ -101,14 +103,46 @@ Create a new client instance using the [`CosmosClient`](/python/api/azure-cosmos
101103

102104
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_client":::
103105

106+
#### [Async](#tab/async)
107+
108+
From the project directory, open the *app.py* file. In your editor, import the `os`, `json`, and `asyncio` modules. Then, import the `CosmosClient` and `PartitionKey` classes from the `azure.cosmos` module.
109+
110+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="imports":::
111+
112+
Create variables for the `COSMOS_ENDPOINT` and `COSMOS_KEY` environment variables using `os.environ`.
113+
114+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="environment_variables":::
115+
116+
Create constants for Database and Container names.
117+
118+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="constants":::
119+
120+
Define a coroutine function and create a new client instance using the [`CosmosClient`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient) class constructor and the environment variables you created as parameters.
121+
122+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="24-26":::
123+
124+
---
125+
104126
### Create a database
105127

128+
#### [Sync](#tab/sync)
129+
106130
Use the [`CosmosClient.create_database_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient#azure-cosmos-cosmos-client-cosmosclient-create-database-if-not-exists) method to create a new database if it doesn't already exist. This method will return a [`DatabaseProxy`](/python/api/azure-cosmos/azure.cosmos.databaseproxy) reference to the existing or newly created database.
107131

108132
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_database":::
109133

134+
#### [Async](#tab/async)
135+
136+
Use the [`CosmosClient.create_database_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient#azure-cosmos-cosmos-client-cosmosclient-create-database-if-not-exists) method to create a new database if it doesn't already exist. This method will return a [`DatabaseProxy`](/python/api/azure-cosmos/azure.cosmos.databaseproxy) reference to the existing or newly created database.
137+
138+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="28-29":::
139+
140+
---
141+
110142
### Create a container
111143

144+
#### [Sync](#tab/sync)
145+
112146
The [`PartitionKey`](/python/api/azure-cosmos/azure.cosmos.partitionkey) class defines a partition key path that you can use when creating a container.
113147

114148
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_partition_key":::
@@ -117,8 +151,22 @@ The [`Databaseproxy.create_container_if_not_exists`](/python/api/azure-cosmos/az
117151

118152
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_container":::
119153

154+
#### [Async](#tab/async)
155+
156+
The [`PartitionKey`](/python/api/azure-cosmos/azure.cosmos.partitionkey) class defines a partition key path that you can use when creating a container.
157+
158+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="31":::
159+
160+
The [`Databaseproxy.create_container_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.databaseproxy#azure-cosmos-databaseproxy-create-container-if-not-exists) method will create a new container if it doesn't already exist. This method will also return a [`ContainerProxy`](/python/api/azure-cosmos/azure.cosmos.containerproxy) reference to the container.
161+
162+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="33-36":::
163+
164+
---
165+
120166
### Create an item
121167

168+
#### [Sync](#tab/sync)
169+
122170
Create a new item in the container by first creating a new variable (`newItem`) with a sample item defined. In this example, the unique identifier of this item is `70b63682-b93a-4c77-aad2-65501347265f`. The partition key value is derived from the `/categoryId` path, so it would be `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
123171

124172
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="new_item":::
@@ -130,16 +178,41 @@ Create an item in the container by using the [`ContainerProxy.create_item`](/pyt
130178

131179
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_item":::
132180

181+
#### [Async](#tab/async)
182+
183+
Create a new item in the container by first creating a new variable (`newItem`) with a sample item defined. In this example, the unique identifier of this item is `70b63682-b93a-4c77-aad2-65501347265f`. The partition key value is derived from the `/categoryId` path, so it would be `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
184+
185+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="38-45":::
186+
187+
> [!TIP]
188+
> The remaining fields are flexible and you can define as many or as few as you want. You can even combine different item schemas in the same container.
189+
190+
Create an item in the container by using the [`ContainerProxy.create_item`](/python/api/azure-cosmos/azure.cosmos.containerproxy#azure-cosmos-containerproxy-create-item) method passing in the variable you already created.
191+
192+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="47":::
193+
194+
---
195+
133196
### Get an item
134197

135198
In Azure Cosmos DB, you can perform a point read operation by using both the unique identifier (``id``) and partition key fields. In the SDK, call [`ContainerProxy.read_item`](/python/api/azure-cosmos/azure.cosmos.containerproxy#azure-cosmos-containerproxy-read-item) passing in both values to return an item as a dictionary of strings and values (`dict[str, Any]`).
136199

200+
#### [Sync](#tab/sync)
201+
137202
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="read_item":::
138203

139-
In this example, the dictionary result is saved to a variable named `existingItem`.
204+
#### [Async](#tab/async)
205+
206+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="49-53":::
207+
208+
---
209+
210+
In this example, the dictionary result is saved to a variable named `existing_item`.
140211

141212
### Query items
142213

214+
#### [Sync](#tab/sync)
215+
143216
After you insert an item, you can run a query to get all items that match a specific filter. This example runs the SQL query: ``SELECT * FROM products p WHERE p.categoryId = "61dba35b-4f02-45c5-b648-c6badc0cbd79"``. This example uses query parameterization to construct the query. The query uses a string of the SQL query, and a dictionary of query parameters.
144217

145218
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="build_query":::
@@ -156,6 +229,28 @@ Finally, use a for loop to iterate over the results in each page and perform var
156229

157230
In this example, `json.dumps` is used to print the item to the console in a human-readable way.
158231

232+
#### [Async](#tab/async)
233+
234+
After you insert an item, you can run a query to get all items that match a specific filter. This example runs the SQL query: ``SELECT * FROM products p WHERE p.categoryId = "61dba35b-4f02-45c5-b648-c6badc0cbd79"``. This example uses query parameterization to construct the query. The query uses a string of the SQL query, and a dictionary of query parameters.
235+
236+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="55-57":::
237+
238+
This example dictionary included the `@categoryId` query parameter and the corresponding value `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
239+
240+
Once the query is defined, call [`ContainerProxy.query_items`](/python/api/azure-cosmos/azure.cosmos.containerproxy#azure-cosmos-containerproxy-query-items) to run the query and return the results as a paged set of items (`ItemPage[Dict[str, Any]]`).
241+
242+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="59-61":::
243+
244+
Finally, use a for loop to iterate over the results in each page and perform various actions.
245+
246+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="62-64":::
247+
248+
Run the async function.
249+
250+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="70":::
251+
252+
---
253+
159254
## Run the code
160255

161256
This app creates an API for NoSQL database and container. The example then creates an item and then reads the exact same item back. Finally, the example issues a query that should only return that single item. At the final step, the example outputs the final item to the console.

0 commit comments

Comments
 (0)