Quickstart: Get started with Azure Table Storage
This quickstart guide walks you through the fundamental steps to get started with Azure Table Storage, a NoSQL key-attribute store. You'll learn how to create a table, insert entities, query data, and delete entities using the Azure SDK for Python.
Prerequisites
Before you begin, ensure you have the following:
- An Azure subscription. If you don't have one, create a free account.
- A storage account. You can create one through the Azure portal, Azure CLI, or Azure PowerShell.
- The Azure SDK for Python installed. If you haven't already, install it using pip:
pip install azure-cosmosdb-table
Step 1: Connect to your Storage Account
To interact with your Table Storage, you need your storage account name and its primary access key. You can find these in the Azure portal under your storage account's "Access keys" section.
Next, you'll instantiate a `TableServiceClient` object, which serves as the entry point for interacting with your tables. Replace <your_account_name> and <your_account_key> with your actual credentials.
from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import EntityProperty, TablePayloadFormat
# Replace with your storage account name and key
account_name = "<your_account_name>"
account_key = "<your_account_key>"
table_service = TableService(account_name=account_name, account_key=account_key)
Step 2: Create a Table
You can create a new table using the `create_table` method. If the table already exists, this operation will fail, but the `table_service` will still be usable for that table name.
table_name = "MyTaskTable"
try:
table_service.create_table(table_name)
print(f"Table '{table_name}' created successfully.")
except Exception as e:
print(f"Error creating table '{table_name}': {e}")
Step 3: Insert an Entity
An entity is a set of properties, similar to a row in a database. Each entity must have two required properties: PartitionKey and RowKey. These two properties together form the entity's primary key.
We'll define a task entity and insert it into our table.
task = {
'PartitionKey': 'tasks',
'RowKey': 'task1',
'description': 'Buy groceries',
'completed': False,
'priority': 1
}
try:
table_service.insert_entity(table_name, task)
print(f"Entity '{task['RowKey']}' inserted successfully.")
except Exception as e:
print(f"Error inserting entity: {e}")
Step 4: Query Entities
You can retrieve entities from a table using the `query_entities` method. You can filter entities by providing a query string.
Let's retrieve all entities with PartitionKey equal to 'tasks'.
print("\nQuerying entities...")
tasks = table_service.query_entities(table_name, "PartitionKey eq 'tasks'")
for task in tasks:
print(f" PartitionKey: {task.PartitionKey}, RowKey: {task.RowKey}, Description: {task.description}, Completed: {task.completed}, Priority: {task.priority}")
You can also retrieve a single entity by its PartitionKey and RowKey.
print("\nRetrieving a single entity...")
retrieved_task = table_service.get_entity(table_name, 'tasks', 'task1')
print(f" Retrieved: PartitionKey: {retrieved_task.PartitionKey}, RowKey: {retrieved_task.RowKey}, Description: {retrieved_task.description}")
Step 5: Update an Entity
To update an entity, you can use the `update_entity` method. This will overwrite the existing entity with the new data. If the entity doesn't exist, the operation will fail.
Let's mark our task as completed.
print("\nUpdating entity...")
task_to_update = {
'PartitionKey': 'tasks',
'RowKey': 'task1',
'description': 'Buy groceries',
'completed': True, # Mark as completed
'priority': 1
}
try:
table_service.update_entity(table_name, task_to_update)
print(f"Entity '{task_to_update['RowKey']}' updated successfully.")
except Exception as e:
print(f"Error updating entity: {e}")
Step 6: Delete an Entity
To delete an entity, use the `delete_entity` method, providing the PartitionKey and RowKey.
print("\nDeleting entity...")
try:
table_service.delete_entity(table_name, 'tasks', 'task1')
print("Entity 'task1' deleted successfully.")
except Exception as e:
print(f"Error deleting entity: {e}")
Step 7: Delete a Table
Finally, you can delete the entire table.
print("\nDeleting table...")
try:
table_service.delete_table(table_name)
print(f"Table '{table_name}' deleted successfully.")
except Exception as e:
print(f"Error deleting table '{table_name}': {e}")
Next Steps
Congratulations! You've successfully completed the Azure Table Storage quickstart. You've learned the basic operations for managing entities in Table Storage.
- Explore advanced querying options.
- Learn about Table Storage performance best practices.
- Discover how to use other Azure SDKs for different languages.