Using Python script to fetch and write data to CosmosDB container

This blog post guides you through the process of using Python to fetch and write data to a Cosmos DB container, a critical task for building dynamic and data-driven applications.

Prerequisites

Azure Account

Follow this article to setup your cosmosDB.

Steps We Will Cover

To successfully interact with a Cosmos DB container using Python, we’ll follow these high-level steps:

  1. Establish a Connection to Cosmos DB:

    • Authenticate and establish a secure connection to the Cosmos DB account using the provided credentials.
  2. Fetch Data from the Cosmos DB Container:

    • Query the container using Python to retrieve existing data.

    • Parse and process the retrieved data as needed for your application.

  3. Write Data to the Cosmos DB Container:

    • Write the data to the specified container.
  4. Test and Validate the Operations:

    • Run tests to verify data retrieval and insertion.

    • Use the Azure portal or tools like Data Explorer to confirm data updates in the container.

Fetch script in Python

We will create a Python script that will fetch data from the container.

You need to add your Cosmosdb URI and Primary key. To get these details, go to your cosmosdb account and click on Settings > Keys.

Capture your URI and Primary Key and add them to this script.

from azure.cosmos import CosmosClient, exceptions

# Cosmos DB Configuration
URI = "<YOUR_COSMOS_DB_URI>"
PRIMARY_KEY = "<YOUR_PRIMARY_KEY>"
DATABASE_NAME = "cosmicdb"
CONTAINER_NAME = "products"

# Initialize Cosmos Client
client = CosmosClient(URI, PRIMARY_KEY)

# Access database and container
database = client.get_database_client(DATABASE_NAME)
container = database.get_container_client(CONTAINER_NAME)

# Fetch Data
def fetch_data():
    try:
        print("Fetching data from Cosmos DB...")
        query = "SELECT * FROM c"
        items = list(container.query_items(query=query, enable_cross_partition_query=True))
        print(f"Fetched {len(items)} items.")
        for item in items:
            print(f"ID: {item['id']}, Name: {item['name']}, Price: {item['price']}")
            print(f"  Company: {item['company']['name']} (Category: {item['company']['category']})")
    except exceptions.CosmosHttpResponseError as e:
        print(f"Error fetching data: {e}")

if __name__ == "__main__":
    fetch_data()

Run this script:

pip3 install azure.cosmos 
python3 fetch.py

Write script in Python

Create a file write.py and copy the Python script from below. Add URI and Primary Key to your script.

from azure.cosmos import CosmosClient, exceptions
import uuid

# Cosmos DB Configuration
URI = "<YOUR_COSMOS_DB_URI>"
PRIMARY_KEY = "<YOUR_PRIMARY_KEY>"
DATABASE_NAME = "cosmicdb"
CONTAINER_NAME = "products"

# Initialize Cosmos Client
client = CosmosClient(URI, PRIMARY_KEY)

# Access database and container
database = client.get_database_client(DATABASE_NAME)
container = database.get_container_client(CONTAINER_NAME)

# Write Data
def write_data():
    try:
        print("Writing data to Cosmos DB...")
        unique_id = f"juice-{str(uuid.uuid4())[:8]}"
        data = {
            "id": unique_id,
            "name": "orange",
            "company": {
                "category": "beverage",
                "name": "globaldrinks"
            },
            "price": 60  # You can customize this
        }
        container.create_item(body=data)
        print(f"Inserted item: {data}")
    except exceptions.CosmosHttpResponseError as e:
        print(f"Error writing data: {e}")

if __name__ == "__main__":
    write_data()

Run this script:

python3 write.py

You can again run the fetch script to check if the data has been inserted correctly.

python3 fetch.py

You can validate the same from the Azure data explorer section:

Conclusion

Integrating Python with Azure Cosmos DB allows you to leverage Python's flexibility and Cosmos DB's scalability for efficient data management. By following the outlined steps, you can fetch and write data to your Cosmos DB container, enabling dynamic interaction with your database. This integration forms the backbone of data-driven solutions, enhancing your application's responsiveness and scalability. The synergy of Python and Cosmos DB opens up possibilities for building robust, future-proof applications tailored to meet the demands of modern enterprises.