How to INSERT SELECT UPDATE DELETE data in Azure Cosmos DB using Python?
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In today’s blog post, we will discuss how to insert, select, update and delete data in Azure Cosmos DB using Python SDK for Cosmos core SQL.
How to INSERT SELECT UPDATE DELETE data in Azure Cosmos DB using Python?
Prerequisites:
Azure CLI installed and configured
Step 1: Install Azure Python SDK for Cosmos DB.
1 2 |
## Install Azure Python SDK for cosmosdb pip install azure-cosmos |
Step 2: Declare some environment variables.
1 2 3 4 5 6 |
## Declare some variables export RES_GROUP="AzureCosmosDBRG" export ACCT_NAME="cloudaffaire-azure-cosmosdb-account" export REGION="eastus" export DATABASE="AzureCosmosDB" export CONTAINER="AzureCosmosDBContainer" |
Step 3: Create a new resource group for Cosmos DB.
1 2 3 4 |
## Create a resource group az group create \ --name $RES_GROUP \ --location $REGION |
Step 4: Create an Azure Cosmos DB account using Azure CLI.
1 2 3 4 |
## Create a Cosmos account az cosmosdb create \ --name $ACCT_NAME \ --resource-group $RES_GROUP |
Step 5: Get and store Azure Cosmos DB connection string details into variables.
1 2 3 |
## Get cosmosdb endpoint and keys (used to connect with cosmosdb) export ACCOUNT_URI=$(az cosmosdb show --resource-group $RES_GROUP --name $ACCT_NAME --query documentEndpoint --output tsv) export ACCOUNT_KEY=$(az cosmosdb keys list --resource-group $RES_GROUP --name $ACCT_NAME --query primaryMasterKey --output tsv) |
Step 6: Create a Python script to create an Azure Cosmos DB database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## Python script to create a database cat << EOF > createDB.py from azure.cosmos import CosmosClient, exceptions import os url = os.environ['ACCOUNT_URI'] key = os.environ['ACCOUNT_KEY'] database_name = os.environ['DATABASE'] client = CosmosClient(url, credential=key) try: database = client.create_database(database_name) except exceptions.CosmosResourceExistsError: database = client.get_database_client(database_name) EOF |
Step 6: Create an Azure Cosmos DB database using Python.
1 2 |
## Create a cosmos db database python createDB.py |
Step 7: Create a Python script to create an Azure Cosmos DB container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
## Python script to create a container cat << EOF > createCONTAINER.py from azure.cosmos import CosmosClient, PartitionKey, exceptions import os url = os.environ['ACCOUNT_URI'] key = os.environ['ACCOUNT_KEY'] database_name = os.environ['DATABASE'] container_name = os.environ['CONTAINER'] client = CosmosClient(url, credential=key) database = client.get_database_client(database_name) try: container = database.create_container(id=container_name, partition_key=PartitionKey(path="/productName")) except exceptions.CosmosResourceExistsError: container = database.get_container_client(container_name) except exceptions.CosmosHttpResponseError: raise EOF |
Step 8: Create an Azure Cosmos DB container using Python.
1 2 |
## Create a container in Cosmos DB python createCONTAINER.py |
Step 9: Create a Python script to insert data into Azure Cosmos DB.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## Python script to insert data cat << EOF > insert.py from azure.cosmos import CosmosClient import os url = os.environ['ACCOUNT_URI'] key = os.environ['ACCOUNT_KEY'] database_name = os.environ['DATABASE'] container_name = os.environ['CONTAINER'] client = CosmosClient(url, credential=key) database = client.get_database_client(database_name) container = database.get_container_client(container_name) for i in range(1, 10): container.upsert_item({ 'id': 'item{0}'.format(i), 'productName': 'Widget', 'productModel': 'Model {0}'.format(i) } ) EOF |
Step 10: Insert data into Azure Cosmos DB using Python.
1 2 |
## Insert data in Azure Cosmos DB python insert.py |
Step 11: Create a Python script to select data from Azure Cosmos DB.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
## Python script to select data cat << EOF > select.py from azure.cosmos import CosmosClient import os import json url = os.environ['ACCOUNT_URI'] key = os.environ['ACCOUNT_KEY'] database_name = os.environ['DATABASE'] container_name = os.environ['CONTAINER'] client = CosmosClient(url, credential=key) database = client.get_database_client(database_name) container = database.get_container_client(container_name) # Enumerate the returned items for item in container.query_items( query='SELECT * FROM '+container_name+' r WHERE r.id="item3"', enable_cross_partition_query=True): print(json.dumps(item, indent=True)) EOF |
Step 12: Select data from Azure Cosmos DB using Python.
1 2 |
## Select data in Azure Cosmos DB python select.py |
Step 13: Create a Python script to delete data from Azure Cosmos DB.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
## Python script to delete data cat << EOF > delete.py from azure.cosmos import CosmosClient import os url = os.environ['ACCOUNT_URI'] key = os.environ['ACCOUNT_KEY'] database_name = os.environ['DATABASE'] container_name = os.environ['CONTAINER'] client = CosmosClient(url, credential=key) database = client.get_database_client(database_name) container = database.get_container_client(container_name) for item in container.query_items( query='SELECT * FROM '+container_name+' r WHERE r.id="item3"', enable_cross_partition_query=True): container.delete_item(item, partition_key='Widget') EOF |
Step 14: Delete data from Azure Cosmos DB using Python.
1 2 |
## Delete data from Azure Cosmos DB python delete.py |
Step 15: Clean up.
1 2 3 4 |
## Delete the resource group az group delete \ --name $RES_GROUP \ --yes |
Hope you have enjoyed this article. To get more details in Azure Cosmos DB, please refer the below documentation.
https://docs.microsoft.com/en-us/azure/cosmos-db/
Azure Cosmos DB CLI reference
https://docs.microsoft.com/en-us/cli/azure/cosmosdb?view=azure-cli-latest