Quickstart: Build a Table API app with Python and Azure Cosmos DB
In this quickstart, you create and manage an Azure Cosmos DB Table API account from the Azure portal, and from Visual Studio with a Python app cloned from GitHub. Azure Cosmos DB is a multi-model database service that lets you quickly create and query document, table, key-value, and graph databases with global distribution and horizontal scale capabilities.
- An Azure account with an active subscription. Create one for free. Or try Azure Cosmos DB for free without an Azure subscription. You can also use the Azure Cosmos DB Emulator with a URI of
https://localhost:8081and the key
- Visual Studio 2019, with the Azure development and Python development workloads selected during setup.
Create a database account
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.
In a new browser window, sign in to the Azure portal.
In the left menu, select Create a resource.
On the New page, select Databases > Azure Cosmos DB.
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 Account Name Select Create new. Then enter a new resource group name for your account. For simplicity, use the same name as your Azure Cosmos DB account name. Account Name A unique name Enter a unique name to identify your Azure Cosmos DB account.
The account name can use only lowercase letters, numbers, and hyphens (-), and must be between 3 and 31 characters long.
API 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. You must create a separate account for each API.
Select Azure Table, because in this quickstart you are creating a table that works with the Table API.
Learn more about the Table API.
Location 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 the data.
You can leave the Geo-Redundancy and Multi-region Writes options at Disable to avoid additional charges, and skip the Network and Tags sections.
Select Review+Create. After the validation is complete, select Create to create the account.
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.
Add a table
You can now use the Data Explorer tool in the Azure portal to create a database and table.
Select Data Explorer > New Table.
The Add Table area is displayed on the far right, you may need to scroll right to see it.
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.
Data Explorer displays the new database and table.
Add sample data
You can now add data to your new table using Data Explorer.
In Data Explorer, expand sample-table, select Entities, and then select Add Entity.
Now add data to the PartitionKey value box and RowKey value box, and select Add 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.
Open a command prompt, create a new folder named git-samples, then close the command prompt.
Open a git terminal window, such as git bash, and use the
cdcommand to change to the new folder to install the sample app.
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
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.
In your Azure Cosmos DB account in the Azure portal, select Connection String.
Copy the ACCOUNT NAME using the button on the right side.
Open the config.py file, and paste the ACCOUNT NAME from the portal into the STORAGE_ACCOUNT_NAME value on line 19.
Go back to the portal and copy the PRIMARY KEY.
Paste the PRIMARY KEY from the portal into the STORAGE_ACCOUNT_KEY value on line 20.
Save the config.py file.
Run the app
In Visual Studio, right-click on the project in Solution Explorer.
Select the current Python environment, then right click.
Select Install Python Package, then enter azure-storage-table.
Press 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:
Select Metrics in your Cosmos DB account's navigation menu.
Select a tab such as Latency, and select a timeframe on the right. Compare the Actual and SLA lines on the charts.
Review the metrics on the other tabs.
Clean up resources
When you're done with your 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:
In the Azure portal Search bar, search for and select Resource groups.
From the list, select the resource group you created for this quickstart.
On the resource group Overview page, select Delete resource group.
In the next window, enter the name of the resource group to delete, and then select Delete.
In this quickstart, you've learned how to create an Azure Cosmos DB account, create a table using the Data Explorer, and run a Python app in Visual Studio to add table data. Now you can query your data using the Table API.