Skip to content

Latest commit

 

History

History
73 lines (45 loc) · 4.04 KB

how-to-python-create-container.md

File metadata and controls

73 lines (45 loc) · 4.04 KB
title description author ms.author ms.service ms.subservice ms.devlang ms.topic ms.date ms.custom
Create a container in Azure Cosmos DB for NoSQL using Python
Learn how to create a container in your Azure Cosmos DB for NoSQL database using the Python SDK.
seesharprun
sidandrews
cosmos-db
nosql
python
how-to
12/06/2022
devx-track-python, devguide-python, cosmos-db-dev-journey

Create a container in Azure Cosmos DB for NoSQL using Python

[!INCLUDENoSQL]

Containers in Azure Cosmos DB store sets of items. Before you can create, query, or manage items, you must first create a container.

Name a container

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.

Here are some quick rules when naming a container:

  • Keep container names between 3 and 63 characters long
  • Container names can only contain lowercase letters, numbers, or the dash (-) character.
  • Container names must start with a lowercase letter or number.

Once created, the URI for a container is in this format:

https://<cosmos-account-name>.documents.azure.com/dbs/<database-name>/colls/<container-name>

Create a container

To create a container, call one of the following methods:

Create a container

The following example creates a container with the DatabaseProxy.create_container method. This method throws an exception if the container with the same name already exists.

:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app.py" id="create_container":::

Create a container if it doesn't already exist

The following example creates a container with the DatabaseProxy.create_container_if_not_exists 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.

:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app_exists.py" id="create_container":::

Create a container asynchronously

You can also create a database asynchronously using similar object and methods in the azure.cosmos.aio namespace. For example, use the DatabaseProxy.create_database method or the CosmoClient.create_database_if_not_exists method.

Working asynchronously is useful when you want to perform multiple operations in parallel. For more information, see Using the asynchronous client.

Parsing the response

In the examples above, the response from the requests is a ContainerProxy, which is an interface to interact with a DB Container. From the proxy, you can access methods to perform operations on the container.

The following example shows the create_container_if_not_exists method returning a container object.

:::code language="python" source="~/cosmos-db-nosql-python-samples/005-create-container/app_exists.py" id="parse_response":::

Next steps

Now that you've create a container, use the next guide to create items.

[!div class="nextstepaction"] Examples for Azure Cosmos DB for NoSQL SDK for Python