Skip to content

Commit 17e7b63

Browse files
committed
Revert quickstart-python
1 parent c26ef64 commit 17e7b63

File tree

1 file changed

+198
-16
lines changed

1 file changed

+198
-16
lines changed
Lines changed: 198 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
---
2-
title: Quickstart - Azure Cosmos DB for NoSQL client library for Python
3-
description: Learn how to build a .NET app to manage Azure Cosmos DB for NoSQL account resources and data in this quickstart.
4-
author: seesharprun
5-
ms.author: sidandrews
2+
title: 'Quickstart: Build a Python app using Azure Cosmos DB for NoSQL account'
3+
description: Presents a Python code sample you can use to connect to and query the Azure Cosmos DB for NoSQL
4+
author: Rodrigossz
65
ms.service: cosmos-db
76
ms.subservice: nosql
87
ms.devlang: python
98
ms.topic: quickstart
10-
ms.date: 11/03/2022
11-
ms.custom: seodec18, seo-javascript-september2019, seo-python-october2019, devx-track-python, mode-api, ignite-2022, cosmos-dev-refresh, cosmos-dev-dotnet-path
9+
ms.date: 08/25/2022
10+
ms.author: rosouz
11+
ms.custom: seodec18, seo-javascript-september2019, seo-python-october2019, devx-track-python, mode-api, ignite-2022
1212
---
1313

14-
# Quickstart: Azure Cosmos DB for NoSQL client library for Python
15-
14+
# Quickstart: Build a Python application using an Azure Cosmos DB for NoSQL account
1615
[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)]
1716

1817
> [!div class="op_single_selector"]
@@ -26,16 +25,199 @@ ms.custom: seodec18, seo-javascript-september2019, seo-python-october2019, devx-
2625
> * [Go](quickstart-go.md)
2726
>
2827
29-
Get started with the Azure Cosmos DB client library for Python to create databases, containers, and items within your account. Follow these steps to install the package and try out example code for basic tasks.
28+
In this quickstart, you create and manage an Azure Cosmos DB for NoSQL account from the Azure portal, and from Visual Studio Code with a Python app cloned from GitHub. Azure Cosmos DB is a multi-model database service that lets you quickly create and query document, table, key-value, and graph databases with global distribution and horizontal scale capabilities.
3029

31-
> [!NOTE]
32-
> The [example code snippets](https://github.com/Azure-Samples/azure-cosmos-db-dotnet-quickstart) are available on GitHub as a .NET project.
30+
## Prerequisites
3331

34-
[API reference documentation](/dotnet/api/microsoft.azure.cosmos) | [Library source code](https://github.com/Azure/azure-cosmos-dotnet-v3) | [Package (NuGet)](https://www.nuget.org/packages/Microsoft.Azure.Cosmos) | [Samples](samples-dotnet.md)
32+
- An Azure Cosmos DB Account. You options are:
33+
* Within an Azure active subscription:
34+
* [Create an Azure free Account](https://azure.microsoft.com/free) or use your existing subscription
35+
* [Visual Studio Monthly Credits](https://azure.microsoft.com/pricing/member-offers/credit-for-visual-studio-subscribers)
36+
* [Azure Cosmos DB Free Tier](../optimize-dev-test.md#azure-cosmos-db-free-tier)
37+
* Without an Azure active subscription:
38+
* [Try Azure Cosmos DB for free](../try-free.md), a tests environment that lasts for 30 days.
39+
* [Azure Cosmos DB Emulator](https://aka.ms/cosmosdb-emulator)
40+
- [Python 3.7+](https://www.python.org/downloads/), with the `python` executable in your `PATH`.
41+
- [Visual Studio Code](https://code.visualstudio.com/).
42+
- The [Python extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python#overview).
43+
- [Git](https://www.git-scm.com/downloads).
44+
- [Azure Cosmos DB for NoSQL SDK for Python](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/cosmos/azure-cosmos)
3545

36-
## Prerequisites
46+
## Important update on Python 2.x Support
47+
48+
New releases of this SDK won't support Python 2.x starting January 1st, 2022. Please check the [CHANGELOG](./sdk-python.md) for more information.
49+
50+
## Create a database account
51+
52+
[!INCLUDE [cosmos-db-create-dbaccount](../includes/cosmos-db-create-dbaccount.md)]
53+
54+
## Add a container
55+
56+
[!INCLUDE [cosmos-db-create-collection](../includes/cosmos-db-create-collection.md)]
57+
58+
## Add sample data
59+
60+
[!INCLUDE [cosmos-db-create-sql-api-add-sample-data](../includes/cosmos-db-create-sql-api-add-sample-data.md)]
61+
62+
## Query your data
63+
64+
[!INCLUDE [cosmos-db-create-sql-api-query-data](../includes/cosmos-db-create-sql-api-query-data.md)]
65+
66+
## Clone the sample application
67+
68+
Now let's clone a API for NoSQL app from GitHub, set the connection string, and run it. This quickstart uses version 4 of the [Python SDK](https://pypi.org/project/azure-cosmos/#history).
69+
70+
1. Open a command prompt, create a new folder named git-samples, then close the command prompt.
71+
72+
```cmd
73+
md git-samples
74+
```
75+
76+
If you are using a bash prompt, you should instead use the following command:
77+
78+
```bash
79+
mkdir "git-samples"
80+
```
81+
82+
2. Open a git terminal window, such as git bash, and use the `cd` command to change to the new folder to install the sample app.
83+
84+
```bash
85+
cd "git-samples"
86+
```
87+
88+
3. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.
89+
90+
```bash
91+
git clone https://github.com/Azure-Samples/azure-cosmos-db-python-getting-started.git
92+
```
93+
94+
## Update your connection string
95+
96+
Now go back to the Azure portal to get your connection string information and copy it into the app.
97+
98+
1. In your Azure Cosmos DB account in the [Azure portal](https://portal.azure.com/), select **Keys** from the left navigation. Use the copy buttons on the right side of the screen to copy the **URI** and **Primary Key** into the *cosmos_get_started.py* file in the next step.
99+
100+
:::image type="content" source="./media/quickstart-python/access-key-and-uri-in-keys-settings-in-the-azure-portal.png" alt-text="Get an access key and URI in the Keys settings in the Azure portal":::
101+
102+
2. In Visual Studio Code, open the *cosmos_get_started.py* file in *\git-samples\azure-cosmos-db-python-getting-started*.
103+
104+
3. Copy your **URI** value from the portal (using the copy button) and make it the value of the **endpoint** variable in *cosmos_get_started.py*.
105+
106+
`endpoint = 'https://FILLME.documents.azure.com',`
107+
108+
4. Then copy your **PRIMARY KEY** value from the portal and make it the value of the **key** in *cosmos_get_started.py*. You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.
109+
110+
`key = 'FILLME'`
111+
112+
5. Save the *cosmos_get_started.py* file.
113+
114+
## Review the code
115+
116+
This step is optional. Learn about the database resources created in code, or skip ahead to [Update your connection string](#update-your-connection-string).
117+
118+
The following snippets are all taken from the [cosmos_get_started.py](https://github.com/Azure-Samples/azure-cosmos-db-python-getting-started/blob/main/cosmos_get_started.py) file.
119+
120+
* The CosmosClient is initialized. Make sure to update the "endpoint" and "key" values as described in the [Update your connection string](#update-your-connection-string) section.
121+
122+
[!code-python[](~/azure-cosmos-db-python-getting-started/cosmos_get_started.py?name=create_cosmos_client)]
123+
124+
* A new database is created.
125+
126+
[!code-python[](~/azure-cosmos-db-python-getting-started/cosmos_get_started.py?name=create_database_if_not_exists)]
127+
128+
* A new container is created, with 400 RU/s of [provisioned throughput](../request-units.md). We choose `lastName` as the [partition key](../partitioning-overview.md#choose-partitionkey), which allows us to do efficient queries that filter on this property.
129+
130+
[!code-python[](~/azure-cosmos-db-python-getting-started/cosmos_get_started.py?name=create_container_if_not_exists)]
131+
132+
* Some items are added to the container. Containers are a collection of items (JSON documents) that can have varied schema. The helper methods ```get_[name]_family_item``` return representations of a family that are stored in Azure Cosmos DB as JSON documents.
133+
134+
[!code-python[](~/azure-cosmos-db-python-getting-started/cosmos_get_started.py?name=create_item)]
135+
136+
* Point reads (key value lookups) are performed using the `read_item` method. We print out the [RU charge](../request-units.md) of each operation.
137+
138+
[!code-python[](~/azure-cosmos-db-python-getting-started/cosmos_get_started.py?name=read_item)]
139+
140+
* A query is performed using SQL query syntax. Because we're using partition key values of ```lastName``` in the WHERE clause, Azure Cosmos DB will efficiently route this query to the relevant partitions, improving performance.
141+
142+
[!code-python[](~/azure-cosmos-db-python-getting-started/cosmos_get_started.py?name=query_items)]
143+
144+
## Run the app
145+
146+
1. In Visual Studio Code, select **View** > **Command Palette**.
147+
148+
2. At the prompt, enter **Python: Select Interpreter** and then select the version of Python to use.
149+
150+
The Footer in Visual Studio Code is updated to indicate the interpreter selected.
151+
152+
3. Select **View** > **Integrated Terminal** to open the Visual Studio Code integrated terminal.
153+
154+
4. In the integrated terminal window, ensure you are in the *azure-cosmos-db-python-getting-started* folder. If not, run the following command to switch to the sample folder.
155+
156+
```cmd
157+
cd "\git-samples\azure-cosmos-db-python-getting-started"`
158+
```
159+
160+
5. Run the following command to install the azure-cosmos package.
161+
162+
```python
163+
pip install azure-cosmos aiohttp
164+
```
165+
166+
If you get an error about access being denied when attempting to install azure-cosmos, you'll need to [run VS Code as an administrator](https://stackoverflow.com/questions/37700536/visual-studio-code-terminal-how-to-run-a-command-with-administrator-rights).
167+
168+
6. Run the following command to run the sample and create and store new documents in Azure Cosmos DB.
169+
170+
```python
171+
python cosmos_get_started.py
172+
```
173+
174+
7. To confirm the new items were created and saved, in the Azure portal, select **Data Explorer** > **AzureSampleFamilyDatabase** > **Items**. View the items that were created. For example, here is a sample JSON document for the Andersen family:
175+
176+
```json
177+
{
178+
"id": "Andersen-1569479288379",
179+
"lastName": "Andersen",
180+
"district": "WA5",
181+
"parents": [
182+
{
183+
"familyName": null,
184+
"firstName": "Thomas"
185+
},
186+
{
187+
"familyName": null,
188+
"firstName": "Mary Kay"
189+
}
190+
],
191+
"children": null,
192+
"address": {
193+
"state": "WA",
194+
"county": "King",
195+
"city": "Seattle"
196+
},
197+
"registered": true,
198+
"_rid": "8K5qAIYtZXeBhB4AAAAAAA==",
199+
"_self": "dbs/8K5qAA==/colls/8K5qAIYtZXc=/docs/8K5qAIYtZXeBhB4AAAAAAA==/",
200+
"_etag": "\"a3004d78-0000-0800-0000-5d8c5a780000\"",
201+
"_attachments": "attachments/",
202+
"_ts": 1569479288
203+
}
204+
```
205+
206+
## Review SLAs in the Azure portal
207+
208+
[!INCLUDE [cosmosdb-tutorial-review-slas](../includes/cosmos-db-tutorial-review-slas.md)]
209+
210+
## Clean up resources
211+
212+
[!INCLUDE [cosmosdb-delete-resource-group](../includes/cosmos-db-delete-resource-group.md)]
213+
214+
## Next steps
215+
216+
In this quickstart, you've learned how to create an Azure Cosmos DB account, create a container using the Data Explorer, and run a Python app in Visual Studio Code. You can now import additional data to your Azure Cosmos DB account.
37217
38-
* An Azure account with an active subscription. [Create an account for free](https://aka.ms/trycosmosdb).
39-
* [.NET 6.0 or later](https://dotnet.microsoft.com/download)
40-
* [Azure Command-Line Interface (CLI)](/cli/azure/) or [Azure PowerShell](/powershell/azure/)
218+
Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
219+
* If all you know is the number of vcores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md)
220+
* If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-with-capacity-planner.md)
41221
222+
> [!div class="nextstepaction"]
223+
> [Import data into Azure Cosmos DB for the API for NoSQL](../import-data.md)

0 commit comments

Comments
 (0)