Azure Cosmos DB: Build a Python application using Azure Cosmos DB SQL API account

Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. You can quickly create and query documents, key/value, wide column and graph databases. All of these operations benefit from the distribution and scale of Azure Cosmos DB.

This quickstart demonstrates how to create an Azure Cosmos DB SQL API account, document database, and container using the Azure portal. You then build and run a console app built with the Python SDK for SQL API. This quickstart uses version 3.0 of the Python SDK.

If you don't have an Azure subscription, create a free account before you begin. You can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments. Or, you can use the Azure Cosmos DB Emulator with a URI of https://localhost:8081. The Primary Key is provided in Authenticating requests.

Prerequisites

Create a database account

  1. Sign in to the Azure portal.

  2. Select Create a resource > Databases > Azure Cosmos DB.

    The Azure portal Databases pane

  3. On the Create Azure Cosmos DB Account page, enter the basic settings for the new Azure Cosmos account.

    Setting Value Description
    Subscription Subscription name Select the Azure subscription that you want to use for this Azure Cosmos account.
    Resource Group Resource group name Select a resource group, or select Create new, then enter a unique name for the new resource group.
    Account Name Enter a unique name Enter a name to identify your Azure Cosmos account. Because documents.azure.com is appended to the ID that you provide to create your URI, use a unique ID.

    The ID can only contain lowercase letters, numbers, and the hyphen (-) character. It must be between 3-31 characters in length.
    API Core (SQL) The API determines the type of account to create. Azure Cosmos DB provides five APIs: Core (SQL) and MongoDB for document data, Gremlin for graph data, Azure Table, and Cassandra. Currently, you must create a separate account for each API.

    Select Core (SQL) to create a document database and query by using SQL syntax.

    Learn more about the SQL API.
    Location Select the region closest to your users Select a geographic location to host your Azure Cosmos DB account. Use the location that is closest to your users to give them the fastest access to the data.

    The new account page for Azure Cosmos DB

  4. Select Review + create. You can skip the Network and Tags sections.

  5. Review the account settings, and then select Create. It takes a few minutes to create the account. Wait for the portal page to display Your deployment is complete.

    The Azure portal Notifications pane

  6. Select Go to resource to go to the Azure Cosmos DB account page.

    The Azure Cosmos DB account page

Add a collection

You can now use the Data Explorer tool in the Azure portal to create a database and collection.

  1. Click Data Explorer > New Collection.

    The Add Collection area is displayed on the far right, you may need to scroll right to see it.

    The Azure portal Data Explorer, Add Collection pane

  2. In the Add collection page, enter the settings for the new collection.

    Setting Suggested value Description
    Database id Tasks Enter Tasks as the name for the new database. Database names must contain from 1 through 255 characters, and they cannot contain /, \\, #, ?, or a trailing space.
    Collection id Items Enter Items as the name for your new collection. Collection ids have the same character requirements as database names.
    Partition key <Your partition key> Enter a partition key such as /userid.
    Throughput 400 RU Change the throughput to 400 request units per second (RU/s). If you want to reduce latency, you can scale up the throughput later.

    In addition to the preceding settings, you can optionally add Unique keys for the collection. Let's leave the field empty in this example. Unique keys provide developers with the ability to add a layer of data integrity to the database. By creating a unique key policy while creating a collection, you ensure the uniqueness of one or more values per partition key. To learn more, refer to the Unique keys in Azure Cosmos DB article.

    Click OK.

    Data Explorer displays the new database and collection.

    The Azure portal Data Explorer, showing the new database and collection

Add sample data

You can now add data to your new collection using Data Explorer.

  1. In Data Explorer, the new database appears in the Collections pane. Expand the Tasks database, expand the Items collection, click Documents, and then click New Documents.

    Create new documents in Data Explorer in the Azure portal

  2. Now add a document to the collection with the following structure.

    {
        "id": "1",
        "category": "personal",
        "name": "groceries",
        "description": "Pick up apples and strawberries.",
        "isComplete": false
    }
    
  3. Once you've added the json to the Documents tab, click Save.

    Copy in json data and click Save in Data Explorer in the Azure portal

  4. Create and save one more document where you insert a unique value for the id property, and change the other properties as you see fit. Your new documents can have any structure you want as Azure Cosmos DB doesn't impose any schema on your data.

Query your data

You can use queries in Data Explorer to retrieve and filter your data.

  1. At the top of the Documents tab in Data Explorer, review the default query SELECT * FROM c. This query retrieves and displays all documents in the collection in ID order.

    Default query in Data Explorer is SELECT * FROM c

  2. To change the query, select Edit Filter, replace the default query with ORDER BY c._ts DESC, and then select Apply Filter.

    Change the default query by adding ORDER BY c._ts DESC and clicking Apply Filter

    The modified query displays the documents in descending order based on their time stamp, so now your second document is listed first.

    Changed query to ORDER BY c._ts DESC and clicking Apply Filter

If you're familiar with SQL syntax, you can enter any supported SQL queries in the query predicate box. You can also use Data Explorer to create stored procedures, UDFs, and triggers for server-side business logic.

Data Explorer provides easy Azure portal access to all of the built-in programmatic data access features available in the APIs. You also use the portal to scale throughput, get keys and connection strings, and review metrics and SLAs for your Azure Cosmos DB account.

Clone the sample application

Now let's clone a SQL API app from GitHub, set the connection string, and run it.

  1. Open a command prompt, create a new folder named git-samples, then close the command prompt.

    md "git-samples"
    

    If you are using a bash prompt, you should instead use the following command:

    mkdir "git-samples"
    
  2. Open a git terminal window, such as git bash, and use the cd command to change to the new folder to install the sample app.

    cd "git-samples"
    
  3. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.

    git clone https://github.com/Azure-Samples/azure-cosmos-db-python-getting-started.git
    

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app.

  1. In the Azure portal, in your Azure Cosmos DB account, in the left navigation click Keys. You'll use the copy buttons on the right side of the screen to copy the URI and Primary Key into the CosmosGetStarted.py file in the next step.

    View and copy an access key in the Azure portal, Keys blade

  2. Open the CosmosGetStarted.py file in \git-samples\azure-cosmos-db-python-getting-started in Visual Studio Code.

  3. Copy your URI value from the portal (using the copy button) and make it the value of the endpoint key in CosmosGetStarted.py.

    'ENDPOINT': 'https://FILLME.documents.azure.com',

  4. Then copy your PRIMARY KEY value from the portal and make it the value of the config.PRIMARYKEY in CosmosGetStarted.py. You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.

    'PRIMARYKEY': 'FILLME',

  5. Save the CosmosGetStarted.py file.

Review the code

This step is optional. Learn about the database resources created in code, or skip ahead to Update your connection string.

Note, if you are familiar with the previous version of the Python SDK, you may be used to seeing the terms "collection" and "document." Because Azure Cosmos DB supports multiple API models, version 3.0+ of the Python SDK uses the generic terms "container,", which may be a collection, graph, or table and "item" to describe the content of the container.

The following snippets are all taken from the CosmosGetStarted.py file.

  • The CosmosClient is initialized. Make sure to update the "Endpoint" and "master key" values as described in the Update your connection string section.

    # Initialize the Cosmos client
    client = cosmos_client.CosmosClient(url_connection=config['ENDPOINT'], auth={'masterKey': config['MASTERKEY']})
    
  • A new database is created.

    # Create a database
    db = client.CreateDatabase({ 'id': config['DATABASE'] })
    
  • A new collection is created.

    # Create collection options
    options = {
        'offerThroughput': 400
    }
    
    # Create a container
    container = client.CreateContainer(db['_self'], container_definition, options)
    
  • Some items are added to the container.

    # Create and add some items to the container
    item1 = client.CreateItem(container['_self'], {
        'serverId': 'server1',
        'Web Site': 0,
        'Cloud Service': 0,
        'Virtual Machine': 0,
        'message': 'Hello World from Server 1!'
        }
    )
    
    item2 = client.CreateItem(container['_self'], {
        'serverId': 'server2',
        'Web Site': 1,
        'Cloud Service': 0,
        'Virtual Machine': 0,
        'message': 'Hello World from Server 2!'
        }
    )
    
  • A query is performed using SQL

    query = {'query': 'SELECT * FROM server s'}
    
    options = {}
    options['enableCrossPartitionQuery'] = True
    options['maxItemCount'] = 2
    
    result_iterable = client.QueryItems(container['_self'], query, options)
    for item in iter(result_iterable):
        print(item['message'])
    

Run the app

  1. In Visual Studio Code, select View>Command Palette.

  2. At the prompt, enter Python: Select Interpreter and then select the version of Python to use.

    The Footer in Visual Studio Code is updated to indicate the interpreter selected.

  3. Select View > Integrated Terminal to open the Visual Studio Code integrated terminal.

  4. In the integrated terminal window, ensure you are in the azure-cosmos-db-python-getting-started folder. If not, run the following command to switch to the sample folder.

    cd "\git-samples\azure-cosmos-db-python-getting-started"`
    
  5. Run the following command to install the azure-cosmos package.

    pip3 install azure-cosmos
    

    If you get an error about access being denied when attempting to install azure-cosmos, you'll need to run VS Code as an administrator.

  6. Run the following command to run the sample and create and store new documents in Azure Cosmos dB.

    python CosmosGetStarted.py
    
  7. To confirm the new items were created and saved, in the Azure portal, select Data Explorer, expand coll, expand Documents, and then select the server1 document. The server1 document contents match the content returned in the integrated terminal window.

    View the new documents in the Azure portal

Review SLAs in the Azure portal

The Azure portal monitors your Cosmos DB account throughput, storage, availability, latency, and consistency. Charts for metrics associated with an Azure Cosmos DB Service Level Agreement (SLA) show the SLA value compared to actual performance. This suite of metrics makes monitoring your SLAs transparent.

To review metrics and SLAs:

  1. Select Metrics in your Cosmos DB account's navigation menu.

  2. Select a tab such as Latency, and select a timeframe on the right. Compare the Actual and SLA lines on the charts.

    Azure Cosmos DB metrics suite

  3. Review the metrics on the other tabs.

Clean up resources

When you're done with your web app and Azure Cosmos DB account, you can delete the Azure resources you created so you don't incur more charges. To delete the resources:

  1. In the Azure portal, select Resource groups on the far left. If the left menu is collapsed, select Expand button to expand it.

  2. Select the resource group you created for this quickstart.

    Metrics in the Azure portal

  3. In the new window, select Delete resource group.

    Metrics in the Azure portal

  4. In the next window, type the name of the resource group to delete, and then select Delete.

Next steps

In this quickstart, you've learned how to create an Azure Cosmos DB account, create a collection using the Data Explorer, and run an app. You can now import additional data to your Cosmos DB account.