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 wordwide dedicated throughput, single-digit millisecodn 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

Build a Table API with Python and Azure Cosmos DB 

 
Step 1
 
Login to Azure portal here(portral.azure.com) 
 
Step 2
 
Create a Database account
 
Click Create a resource ->  Databases ->  Azure Cosmos DB.
 
Screenshot of the Azure portal, highlighting More Services, and Azure Cosmos DB
 
Step 3
 
You can fill your basis Details
 
The new account page for Azure Cosmos DB
 
Click Review+create
 
After Creating ,
 
The Azure portal notifications pane
 
Step 4
 
Add a Table
 
Select Data Explorer -> New Table.
 
Type throughput is - 400
 
Data Explorer in the Azure portal
 
Click Ok.
 
Step 5
 
Data Explorer displays the new database and table
 
The Azure portal Data Explorer, showing the new database and collection
 
Step 6
 
Add a Sample Data
 
In Data Explorer, expand sample-table, select Entities, and then select Add Entity. 
 
Create new entities in Data Explorer in the Azure portal
 
Step 7
 
Now add data to the PartitionKey value box and RowKey value box, and select Add Entity. 
 
Set the Partition Key and Row Key for a new entity
 
Step 8
 
Clone the sample application from Github
 
Open the Command Prompt ,run the following code
  1. md "C:\git-samples"
then, run the code
  1. cd "C:\git-samples"
Finally, run the code
  1. 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
 
View and copy the CONNECTION STRING in the Connection String settings
  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 understood this concept of  Cosmos DB.


Similar Articles