Quickstart: Build a Table API app with Python and Azure Cosmos DB

This quickstart shows how to use Python and the Azure Cosmos DB Table API to build an app by cloning an example from GitHub. This quickstart also shows you how to create an Azure Cosmos DB account and how to use Data Explorer to create tables and entities in the web-based Azure portal.

Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. You can quickly create and query document, key/value, wide-column, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.

Prerequisites

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.

In addition:

  • If you don’t already have Visual Studio 2019 installed, you can download and use the free Visual Studio 2019 Community Edition. Make sure that you select the Azure development and Python development workloads during the Visual Studio setup.
  • Also select the Python 2 option in the Python development workload, or download Python 2.7 from python.org.

Create a database account

Important

You need to create a new Table API account to work with the generally available Table API SDKs. Table API accounts created during preview are not supported by the generally available SDKs.

  1. In a new browser window, sign in to the Azure portal.

  2. In the left navigation pane, select Create a resource. Select Databases and then select Azure Cosmos DB.

    Screenshot of the Azure portal, highlighting More Services, and Azure Cosmos DB

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

    Setting Value Description
    Subscription Your subscription Select the Azure subscription that you want to use for this Azure Cosmos DB account.
    Resource Group Create new

    Then enter the same unique name as provided in ID
    Select Create new. Then enter a new resource group name for your account. For simplicity, use the same name as your ID.
    Account Name Enter a unique name Enter a unique name to identify your Azure Cosmos DB account.

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

    Select Azure Table because in this quickstart you're creating a table that works with the Table API.

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

    You can leave the Geo-Redundancy and Multi-region Writes options at their default values (Disable) to avoid additional RU charges. You can skip the Network and Tags sections.

  4. Select Review+Create. After the validation is complete, select Create to create the account.

    The new account page for Azure Cosmos DB

  5. It takes a few minutes to create the account. You'll see a message that states Your deployment is underway. Wait for the deployment to finish and then select Go to resource.

    The Azure portal notifications pane

Add a table

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

  1. Click Data Explorer > New Table.

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

    Data Explorer in the Azure portal

  2. In the Add Table page, enter the settings for the new table.

    Setting Suggested value Description
    Table Id sample-table The ID for your new table. Table names have the same character requirements as database ids. Database names must be between 1 and 255 characters, and cannot contain / \ # ? or a trailing space.
    Throughput 400 RUs Change the throughput to 400 request units per second (RU/s). If you want to reduce latency, you can scale up the throughput later.
  3. Click OK.

  4. Data Explorer displays the new database and table.

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

Add sample data

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

  1. In Data Explorer, expand sample-table, click Entities, and then click Add Entity.

    Create new entities in Data Explorer in the Azure portal

  2. Now add data to the PartitionKey value box and RowKey value box, and click Add Entity.

    Set the Partition Key and Row Key for a new entity

    You can now add more entities to your table, edit your entities, or query your data in Data Explorer. Data Explorer is also where you can scale your throughput and add stored procedures, user-defined functions, and triggers to your table.

Clone the sample application

Now let's clone a Table app from GitHub, set the connection string, and run it. You'll see how easy it is to work with data programmatically.

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

    md "C:\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 "C:\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/storage-python-getting-started.git
    
  4. Then open the solution file in Visual Studio.

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app. This enables your app to communicate with your hosted database.

  1. In the Azure portal, click Connection String.

    View and copy the CONNECTION STRING in the Connection String pane

  2. Copy the ACCOUNT NAME using the button on the right side.

  3. Open the config.py file, and paste the ACCOUNT NAME from the portal into the STORAGE_ACCOUNT_NAME value on line 19.

  4. Go back to the portal and copy the PRIMARY KEY.

  5. Paste the PRIMARY KEY from the portal into the STORAGE_ACCOUNT_KEY value on line 20.

  6. Save the config.py file.

Run the app

  1. In Visual Studio, right-click on the project in Solution Explorer, select the current Python environment, then right click.

  2. Select Install Python Package, then type in azure-storage-table

  3. Run F5 to run the application. Your app displays in your browser.

You can now go back to Data Explorer and see query, modify, and work with this new data.

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 table using the Data Explorer, and run an app. Now you can query your data using the Table API.