Skip to content

Commit 960cf63

Browse files
Merge pull request #220135 from vmagelo/python-howto
Create how-to get-started article for python and nosql.
2 parents 5f07530 + ce709b1 commit 960cf63

File tree

6 files changed

+577
-0
lines changed

6 files changed

+577
-0
lines changed

articles/cosmos-db/nosql/TOC.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,18 @@
428428
href: how-to-dotnet-read-item.md
429429
- name: Query items
430430
href: how-to-dotnet-query-items.md
431+
- name: Python
432+
items:
433+
- name: Get started
434+
href: how-to-python-get-started.md
435+
- name: Work with databases
436+
items:
437+
- name: Create a database
438+
href: how-to-python-create-database.md
439+
- name: Work with containers
440+
items:
441+
- name: Create a container
442+
href: how-to-python-create-container.md
431443
- name: Designing resilient applications
432444
href: conceptual-resilient-sdk-applications.md
433445
- name: Connectivity modes
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Create a container in Azure Cosmos DB for NoSQL using Python
3+
description: Learn how to create a container in your Azure Cosmos DB for NoSQL database using the Python SDK.
4+
author: seesharprun
5+
ms.author: sidandrews
6+
ms.service: cosmos-db
7+
ms.subservice: nosql
8+
ms.devlang: python
9+
ms.topic: how-to
10+
ms.date: 12/06/2022
11+
ms.custom: devx-track-python, devguide-python, cosmos-db-dev-journey
12+
---
13+
14+
# Create a container in Azure Cosmos DB for NoSQL using Python
15+
16+
[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)]
17+
18+
Containers in Azure Cosmos DB store sets of items. Before you can create, query, or manage items, you must first create a container.
19+
20+
## Name a container
21+
22+
In Azure Cosmos DB, a container is analogous to a table in a relational database. When you create a container, the container name forms a segment of the URI used to access the container resource and any child items.
23+
24+
Here are some quick rules when naming a container:
25+
26+
- Keep container names between 3 and 63 characters long
27+
- Container names can only contain lowercase letters, numbers, or the dash (-) character.
28+
- Container names must start with a lowercase letter or number.
29+
30+
Once created, the URI for a container is in this format:
31+
32+
`https://<cosmos-account-name>.documents.azure.com/dbs/<database-name>/colls/<container-name>`
33+
34+
## Create a container
35+
36+
To create a container, call one of the following methods:
37+
38+
- [CreateContainerAsync](#create-a-container)
39+
- [CreateContainerIfNotExistsAsync](#create-a-container-if-it-doesnt-already-exist)
40+
- [Create a container asynchronously](#create-a-container-asynchronously)
41+
42+
### Create a container
43+
44+
The following example creates a container with the [``DatabaseProxy.create_container``](/python/api/azure-cosmos/azure.cosmos.databaseproxy#azure-cosmos-databaseproxy-create-container) method. This method throws an exception if the container with the same name already exists.
45+
46+
:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app.py" id="create_container":::
47+
48+
### Create a container if it doesn't already exist
49+
50+
The following example creates a container with the [``DatabaseProxy.create_container_if_not_exists``](/python/api/azure-cosmos/azure.cosmos.databaseproxy#azure-cosmos-databaseproxy-create-container-if-not-exist) method. Compared to the previous create method, this method doesn't throw an exception if the database already exists. This method is useful for avoiding errors if you run the same code multiple times.
51+
52+
:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app_exists.py" id="create_container":::
53+
54+
### Create a container asynchronously
55+
56+
You can also create a database asynchronously using similar object and methods in the [azure.cosmos.aio](/python/api/azure-cosmos/azure.cosmos.aio) namespace. For example, use the [`DatabaseProxy.create_database`](/python/api/azure-cosmos/azure.cosmos.aio.databaseproxy#azure-cosmos-aio-databaseproxy-create-container) method or the ['CosmoClient.create_database_if_not_exists](/python/api/azure-cosmos/azure.cosmos.aio.databaseproxy#azure-cosmos-aio-databaseproxy-create-container-if-not-exists) method.
57+
58+
Working asynchronously is useful when you want to perform multiple operations in parallel. For more information, see [Using the asynchronous client](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/cosmos/azure-cosmos#using-the-asynchronous-client).
59+
60+
## Parsing the response
61+
62+
In the examples above, the response from the requests is a [``ContainerProxy``](/python/api/azure-cosmos/azure.cosmos.containerproxy), which is an interface to interact with a DB Container. From the proxy, you can access methods to perform operations on the container.
63+
64+
The following example shows the **create_container_if_not_exists** method returning a **container** object.
65+
66+
:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app_exists.py" id="parse_response":::
67+
68+
## Next steps
69+
70+
Now that you've create a container, use the next guide to create items.
71+
72+
> [!div class="nextstepaction"]
73+
> [Examples for Azure Cosmos DB for NoSQL SDK for Python](samples-python.md)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Create a database in Azure Cosmos DB for NoSQL using Python
3+
description: Learn how to create a database in your Azure Cosmos DB for NoSQL account using the Python SDK.
4+
author: seesharprun
5+
ms.author: sidandrews
6+
ms.service: cosmos-db
7+
ms.subservice: nosql
8+
ms.devlang: python
9+
ms.topic: how-to
10+
ms.date: 12/06/2022
11+
ms.custom: devx-track-python, devguide-python, cosmos-db-dev-journey
12+
---
13+
14+
# Create a database in Azure Cosmos DB for NoSQL using Python
15+
16+
[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)]
17+
18+
Databases in Azure Cosmos DB are units of management for one or more containers. Before you can create or manage containers, you must first create a database.
19+
20+
## Name a database
21+
22+
In Azure Cosmos DB, a database is analogous to a namespace. When you create a database, the database name forms a segment of the URI used to access the database resource and any child resources.
23+
24+
Here are some quick rules when naming a database:
25+
26+
- Keep database names between 3 and 63 characters long
27+
- Database names can only contain lowercase letters, numbers, or the dash (-) character.
28+
- Database names must start with a lowercase letter or number.
29+
30+
Once created, the URI for a database is in this format:
31+
32+
`https://<cosmos-account-name>.documents.azure.com/dbs/<database-name>`
33+
34+
## Create a database
35+
36+
To create a database, call one of the following methods:
37+
38+
- [CreateDatabaseAsync](#create-a-database)
39+
- [CreateDatabaseIfNotExistsAsync](#create-a-database-if-it-doesnt-already-exist)
40+
- [Create a database asynchronously](#create-a-database-asynchronously)
41+
42+
### Create a database
43+
44+
The following example creates a database with the [`CosmosClient.create_database`](/python/api/azure-cosmos/azure.cosmos.cosmosclient#azure-cosmos-cosmosclient-create-database) method. This method throws an exception if a database with the same name exists.
45+
46+
:::code language="python" source="~/cosmos-db-nosql-python-samples/004-create-db/app.py" id="create_database":::
47+
48+
### Create a database if it doesn't already exist
49+
50+
The following example creates a database with the [`CosmosClient.create_database_if_not_exists`](/python/api/azure-cosmos/azure.cosmos.cosmosclient#azure-cosmos-cosmosclient-create-database-if-not-exists) method. If the database exists, this method returns the database settings. Compared to the previous create method, this method doesn't throw an exception if the database already exists. This method is useful for avoiding errors if you run the same code multiple times.
51+
52+
:::code language="python" source="~/cosmos-db-nosql-python-samples/004-create-db/app_exists.py" id="create_database":::
53+
54+
### Create a database asynchronously
55+
56+
You can also create a database asynchronously using similar object and methods in the [azure.cosmos.aio](/python/api/azure-cosmos/azure.cosmos.aio) namespace. For example, use the [`CosmosClient.create_database`](/python/api/azure-cosmos/azure.cosmos.aio.cosmosclient#azure-cosmos-aio-cosmosclient-create-database) method or the ['CosmoClient.create_database_if_not_exists](/python/api/azure-cosmos/azure.cosmos.aio.cosmosclient#azure-cosmos-aio-cosmosclient-create-database-if-not-exists) method.
57+
58+
Working asynchronously is useful when you want to perform multiple operations in parallel. For more information, see [Using the asynchronous client](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/cosmos/azure-cosmos#using-the-asynchronous-client).
59+
60+
## Parsing the response
61+
62+
In the examples above, the response from the requests is a [``DatabaseProxy``](/python/api/azure-cosmos/azure.cosmos.databaseproxy), which is an interface to interact with a specific database. From the proxy, you can access methods to perform operations on the database.
63+
64+
The following example shows the **create_database_if_not_exists** method returning a **database** object.
65+
66+
:::code language="python" source="~/cosmos-db-nosql-python-samples/004-create-db/app_exists.py" id="parse_response":::
67+
68+
## Next steps
69+
70+
Now that you've created a database, use the next guide to create containers.
71+
72+
> [!div class="nextstepaction"]
73+
> [Create a container](how-to-python-create-container.md)

0 commit comments

Comments
 (0)