Skip to content

Commit c5bff91

Browse files
authored
Merge pull request #224406 from seesharprun/cosmos-nosql-python-quickstart
Cosmos DB | Add async code to NoSQL+Python quickstart
2 parents b5a6fb2 + 3bdbd2f commit c5bff91

File tree

1 file changed

+104
-12
lines changed

1 file changed

+104
-12
lines changed

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

Lines changed: 104 additions & 12 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

@@ -91,69 +91,156 @@ For this sample code, the container will use the category as a logical partition
9191

9292
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.
9393

94+
#### [Sync](#tab/sync)
95+
9496
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="imports":::
9597

96-
Create variables for the `COSMOS_ENDPOINT` and `COSMOS_KEY` environment variables using `os.environ`.
98+
#### [Async](#tab/async)
99+
100+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" id="imports":::
101+
102+
---
103+
104+
Create constants for the `COSMOS_ENDPOINT` and `COSMOS_KEY` environment variables using `os.environ`.
105+
106+
#### [Sync / Async](#tab/sync+async)
97107

98108
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="environment_variables":::
99109

110+
---
111+
112+
Create constants for the database and container names.
113+
114+
#### [Sync / Async](#tab/sync+async)
115+
116+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="constants":::
117+
118+
---
119+
100120
Create a new client instance using the [`CosmosClient`](/python/api/azure-cosmos/azure.cosmos.cosmos_client.cosmosclient) class constructor and the two variables you created as parameters.
101121

122+
#### [Sync](#tab/sync)
123+
102124
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_client":::
103125

126+
#### [Async](#tab/async)
127+
128+
> [!IMPORTANT]
129+
> Please the client instance in a coroutine function named `manage_cosmos`. Within the coroutine function, define the new client with the `async with` keywords. Outside of the coroutine function, use the `asyncio.run` function to execute the coroutine asynchronously.
130+
131+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="24-26":::
132+
133+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="70":::
134+
135+
---
136+
104137
### Create a database
105138

106139
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.
107140

141+
#### [Sync](#tab/sync)
142+
108143
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_database":::
109144

145+
#### [Async](#tab/async)
146+
147+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="28-29":::
148+
149+
---
150+
110151
### Create a container
111152

112153
The [`PartitionKey`](/python/api/azure-cosmos/azure.cosmos.partitionkey) class defines a partition key path that you can use when creating a container.
113154

114-
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_partition_key":::
155+
#### [Sync](#tab/sync)
115156

116-
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.
157+
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_partition_key":::
117158

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

161+
#### [Async](#tab/async)
162+
163+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="31":::
164+
165+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="33-36":::
166+
167+
---
168+
169+
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.
170+
120171
### Create an item
121172

122-
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`.
173+
Create a new item in the container by first creating a new variable (`new_item`) 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`.
174+
175+
#### [Sync / Async](#tab/sync+async)
123176

124177
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="new_item":::
125178

179+
---
180+
126181
> [!TIP]
127182
> 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.
128183
129184
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.
130185

186+
#### [Sync](#tab/sync)
187+
131188
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="create_item":::
132189

190+
#### [Async](#tab/async)
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

143214
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.
144215

216+
#### [Sync / Async](#tab/sync+async)
217+
145218
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="build_query":::
146219

220+
---
221+
147222
This example dictionary included the `@categoryId` query parameter and the corresponding value `61dba35b-4f02-45c5-b648-c6badc0cbd79`.
148223

149224
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]]`).
150225

226+
#### [Sync / Async](#tab/sync+async)
227+
151228
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="query_items":::
152229

230+
---
231+
153232
Finally, use a for loop to iterate over the results in each page and perform various actions.
154233

234+
#### [Sync](#tab/sync)
235+
155236
:::code language="python" source="~/cosmos-db-nosql-python-samples/001-quickstart/app.py" id="iterate_query_results":::
156237

238+
#### [Async](#tab/async)
239+
240+
:::code language="python" source="~/cosmos-db-nosql-python-samples/002-quickstart-async/app.py" range="62-64":::
241+
242+
---
243+
157244
In this example, `json.dumps` is used to print the item to the console in a human-readable way.
158245

159246
## Run the code
@@ -169,19 +256,24 @@ python app.py
169256
The output of the app should be similar to this example:
170257

171258
```output
172-
{
259+
Database cosmicworks
260+
Container products
261+
Point read Yamba Surfboard
262+
Result list [
263+
{
173264
"id": "70b63682-b93a-4c77-aad2-65501347265f",
174265
"categoryId": "61dba35b-4f02-45c5-b648-c6badc0cbd79",
175266
"categoryName": "gear-surf-surfboards",
176267
"name": "Yamba Surfboard",
177268
"quantity": 12,
178269
"sale": false,
179-
"_rid": "yzN6AIfJxe0BAAAAAAAAAA==",
180-
"_self": "dbs/yzN6AA==/colls/yzN6AIfJxe0=/docs/yzN6AIfJxe0BAAAAAAAAAA==/",
181-
"_etag": "\"2a00ccd4-0000-0200-0000-63650e420000\"",
270+
"_rid": "KSsMAPI2fH0BAAAAAAAAAA==",
271+
"_self": "dbs/KSsMAA==/colls/KSsMAPI2fH0=/docs/KSsMAPI2fH0BAAAAAAAAAA==/",
272+
"_etag": "\"48002b76-0000-0200-0000-63c85f9d0000\"",
182273
"_attachments": "attachments/",
183-
"_ts": 16457527130
184-
}
274+
"_ts": 1674076061
275+
}
276+
]
185277
```
186278

187279
> [!NOTE]

0 commit comments

Comments
 (0)