Build a Table API with Python and Azure Cosmos DB

Azure Cosmos DB

Azure Cosmos DB is Microsoft’s globally distributed multi-model database service.

Table API

Azure Cosmos DB provides Table API for applications that use Azure Table storage and need premium capabilities such as worldwide dedicated throughput, single-digit millisecond latency, high availability, automatic secondary indexing, and global distribution.

The Table API client SDKs are available for .NET, .NET Core, Java, Python, and Node.js.

Prerequisites

  • Azure Subscription
  • Python 3.7

Step 1. Log in to the Azure portal here(portral.azure.com).

Step 2. Create a Database account

Click Create a Resource ->Databases ->Azure Cosmos DB.

 Azure Cosmos DB

Step 3. You can fill in your basic Details.

Azure Table

Click Review+create

After Creating

Quick Start

Step 4. Add a Table

Select Data Explorer-> New Table.

Type throughput is - 400

Data Explorer 

Click Ok.

Step 5. Data Explorer displays the new database and table.

Database

Step 6. Add a Sample Data

In Data Explorer, expand the sample, select Entities, and then select Add Entity.

Add Entity

Step 7. Now add data to the PartitionKey value box and RowKey value box, and select Add Entity.

RowKey value

Step 8. Clone the sample application from GitHub

Open the Command Prompt, and run the following code.

md "C:\git-samples"

then, run the code.

cd "C:\git-samples"

Finally, run the code.

git clone https://github.com/Azure-Samples/storage-python-getting-started.git

Then, open the source file in Visual Studio.

Step 9. Update the Connection String

In the Azure portal -> Select Connection String.

Connection String

  1. Copy the ACCOUNT NAME.
  2. Open the config.py file, and paste the ACCOUNT NAME from the portal into the STORAGE_ACCOUNT_NAME value on line 19.
  3. Go back to the portal and copy the PRIMARY KEY.
  4. Paste the PRIMARY KEY from the portal into the STORAGE_ACCOUNT_KEY value on line 20.
  5. Save the config.py file.

Step 10. 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 enter azure-storage-table
  3. Run F5 to run the application. Your app displays in your browser.

Conclusion

That's all. We learned about how to use Python and Azure Cosmos DB Table API to build an app by cloning an example from Github. I hope you understand this concept of Cosmos DB.