Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this quickstart, you deploy a basic Azure Cosmos DB for NoSQL application using the Azure SDK for Python. Azure Cosmos DB for NoSQL is a schemaless data store allowing applications to store unstructured data in the cloud. Query data in your containers and perform common operations on individual items using the Azure SDK for Python.
API reference documentation | Library source code | Package (PyPI) | Azure Developer CLI
If you don't have an Azure account, create a free account before you begin.
Use the Azure Developer CLI (azd
) to create an Azure Cosmos DB for NoSQL account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.
Open a terminal in an empty directory.
If you're not already authenticated, authenticate to the Azure Developer CLI using azd auth login
. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.
azd auth login
Use azd init
to initialize the project.
azd init --template cosmos-db-nosql-python-quickstart
During initialization, configure a unique environment name.
Deploy the Azure Cosmos DB account using azd up
. The Bicep templates also deploy a sample web application.
azd up
During the provisioning process, select your subscription, desired location, and target resource group. Wait for the provisioning process to complete. The process can take approximately five minutes.
Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.
Deploying services (azd deploy)
(✓) Done: Deploying service web
- Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io>
SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.
Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.
The client library is available through the Python Package Index, as the azure-cosmos
library.
Open a terminal and navigate to the /src
folder.
cd ./src
If not already installed, install the azure-cosmos
package using pip install
.
pip install azure-cosmos
Also, install the azure-identity
package if not already installed.
pip install azure-identity
Open and review the src/requirements.txt file to validate that the azure-cosmos
and azure-identity
entries both exist.
Name | Description |
---|---|
CosmosClient |
This class is the primary client class and is used to manage account-wide metadata or databases. |
DatabaseProxy |
This class represents a database within the account. |
ContainerProxy |
This class is primarily used to perform read, update, and delete operations on either the container or the items stored within the container. |
PartitionKey |
This class represents a logical partition key. This class is required for many common operations and queries. |
The sample code in the template uses a database named cosmicworks
and container named products
. The products
container contains details such as name, category, quantity, a unique identifier, and a sale flag for each product. The container uses the /category
property as a logical partition key.
This sample creates a new instance of the CosmosClient
type and authenticates using a DefaultAzureCredential
instance.
credential = DefaultAzureCredential()
client = CosmosClient(url="<azure-cosmos-db-nosql-account-endpoint>", credential=credential)
Use client.get_database_client
to retrieve the existing database named cosmicworks
.
database = client.get_database_client("cosmicworks")
Retrieve the existing products
container using database.get_container_client
.
container = database.get_container_client("products")
Build a new object with all of the members you want to serialize into JSON. In this example, the type has a unique identifier, and fields for category, name, quantity, price, and sale. Create an item in the container using container.upsert_item
. This method "upserts" the item effectively replacing the item if it already exists.
new_item = {
"id": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
"category": "gear-surf-surfboards",
"name": "Yamba Surfboard",
"quantity": 12,
"sale": False,
}
created_item = container.upsert_item(new_item)
Perform a point read operation by using both the unique identifier (id
) and partition key fields. Use container.read_item
to efficiently retrieve the specific item.
existing_item = container.read_item(
item="aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
partition_key="gear-surf-surfboards",
)
Perform a query over multiple items in a container using container.GetItemQueryIterator
. Find all items within a specified category using this parameterized query:
SELECT * FROM products p WHERE p.category = @category
queryText = "SELECT * FROM products p WHERE p.category = @category"
results = container.query_items(
query=queryText,
parameters=[
dict(
name="@category",
value="gear-surf-surfboards",
)
],
enable_cross_partition_query=False,
)
Loop through the results of the query.
items = [item for item in results]
output = json.dumps(items, indent=True)
Use the Visual Studio Code extension for Azure Cosmos DB to explore your NoSQL data. You can perform core database operations including, but not limited to:
For more information, see How-to use Visual Studio Code extension to explore Azure Cosmos DB for NoSQL data.
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
azd down
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Use the Azure Cosmos DB for NoSQL SDK - Training
Learn about the Microsoft.Azure.Cosmos library, and then download the library to use in a .NET application.
Certification
Microsoft Certified: Azure Cosmos DB Developer Specialty - Certifications
Write efficient queries, create indexing policies, manage, and provision resources in the SQL API and SDK with Microsoft Azure Cosmos DB.
Documentation
Examples for Azure Cosmos DB for NoSQL SDK for Python
Find Python examples on GitHub for common tasks in Azure Cosmos DB, including CRUD operations.
Azure Cosmos DB SQL API client library for Python
Azure Cosmos DB SQL Python API, SDK & resources
Learn all about the SQL Python API and SDK including release dates, retirement dates, and changes made between each version of the Azure Cosmos DB Python SDK.