A Dummies guide to working with Azure Table Storage
Azure Table storage is a simple yet powerful NoSQL database service offered by Microsoft Azure as part of the storage offering.It provides a key-value pair storage mechanism that can scale to terabytes of data and a queryable API with OData.
When I say database service , if it invokes the thoughts of a Database server , tables , roles , rights , User IDs , sharding , backups and all those technical jargons might haunt you. Well then good news for you !! Azure Table storage is a Database Platform as a Service offering ( DB PAAS ).
This is a step-by-step hands-on tutorial on how to go about working with Azure table storage using Python.
- Installing requirements
pip3 install azure-data-tables
2. Grab the connection string for your Azure storage account.
3. Let’s create table
import os
from azure.data.tables import TableServiceClient
from dotenv import load_dotenv
load_dotenv()
connection_string = os.getenv("connection_string")
# table operations
table_name = "mydemotable"
service_client = TableServiceClient.from_connection_string(
connection_string)
#create a table
try:
# Create the table if it does not already exist
tc = service_client.create_table_if_not_exists(table_name)
print(f"Hello, Table {table_name}has been created succesfully .")
except Exception as e:
print(f"An exception occured {e}")
4. Let’s delete the table
import os
from azure.data.tables import TableServiceClient
from dotenv import load_dotenv
load_dotenv()
connection_string = os.getenv("connection_string")
# table operations
table_name = "mydemotable"
service_client = TableServiceClient.from_connection_string(
connection_string)
#delete a table
try:
# Create the table if it does not already exist
tc = service_client.delete_table(table_name)
print(f"Hello, Table {table_name}has been deleted succesfully .")
except Exception as e:
print(f"An exception occured {e}")
5. Let’s insert some data ( please re-create the table and proceed )
Now this is where things get a bit trickier.
Every Azure storage table should have two main properties namely
a. Partition Key
b. Row Key
Imagine you are storing the names of the historical landmarks across major cities in all countries across the world. Then the following makes perfect sense.
Partition Key : Name of the Country
Row Key : Name of the City
A partition is a consecutive range of entities possessing the same partition key value. By keeping the partition key to the name of the country we ensure that all data about a particular country is stored consecutively in storage for retrival and replication.
import os
from azure.data.tables import TableServiceClient
from dotenv import load_dotenv
load_dotenv()
connection_string = os.getenv("connection_string")
my_entity = {
u'PartitionKey': 'Germany',
u'RowKey': 'Berlin',
u'Landmark': 'Berlin Wall',
u'Type': 'Historical Monument',
u'Status': True
}
# table operations
table_name = "mydemotable"
#define the table service client
table_service_client = TableServiceClient.from_connection_string(
conn_str=connection_string)
#define the table client from the table service client
table_client = table_service_client.get_table_client(
table_name=table_name)
#store the data
try:
entity = table_client.create_entity(entity=my_entity)
print(f"Printing entity created {entity}")
except Exception as e:
print(f"An exception occured {e}")
And there you go . The record is inserted
6. Let’s update the data
Observe that we are adding an attribute called “Relevance” to the entity created.
Observe that the Parition Key and Row Key are supplied in the update operation.
Partition and Row Keys are mandatory for all CRUD operations in table storage.
import os
from azure.data.tables import TableServiceClient, UpdateMode
from dotenv import load_dotenv
load_dotenv()
connection_string = os.getenv("connection_string")
my_entity_to_be_updated = {
u'PartitionKey': 'Germany',
u'RowKey': 'Berlin',
u'Landmark': 'Berlin Wall',
u'Relevance': 'World War II',
u'Type': 'Historical Monument',
u'Status': True
}
table_service_client = TableServiceClient.from_connection_string(
conn_str=connection_string)
table_client = table_service_client.get_table_client(
table_name=table_name)
try:
updatedentity = table_client.update_entity(
mode=UpdateMode.MERGE, entity=my_entity_to_be_updated)
print(f"Printing entity updated {entity}")
except Exception as e:
print(f"An exception occured {e}")
7. Let’s delete the data
import os
from azure.data.tables import TableServiceClient
from dotenv import load_dotenv
load_dotenv()
connection_string = os.getenv("connection_string")
table_name = "mydemotable"
partitionkey = 'Germany'
rowkey = 'Berlin'
table_service_client = TableServiceClient.from_connection_string(
conn_str=connection_string)
table_client = table_service_client.get_table_client(
table_name=table_name)
try:
# delete the entity
table_client.delete_entity(partitionkey, rowkey)
print (f"Entry deleted succesfully")
except Exception as e:
print(f"An exception occured {e}")
Parting thougts:
- Azure Table storage is an extremely simple to setup and a powerful , scalable database solution.
- One of the best database service for rapid proto-typing and scaling up without huge efforts in development with minimal complexities.
Real-life sample with Table Storage:
I have developed an Azure Function App as part of our database toolkit for MVP / Rapid prototyping stage .Created a simple API that will allow our us to create / delete tables and perform CRUD operations.
When to choose Azure Table Storage:
A simple comparison between Azure Table Storage and Azure Cosmos DB and how to choose the right product.
An excellent video on why should you choose Azure Table Storage
Thanks for reading !
Follow me on Medium on dummies guides to most things Cloud !!