Get Started with Azure Table Storage
This tutorial guides you through the fundamental concepts and basic operations of Azure Table Storage. Table Storage is a NoSQL key-attribute store for custom NoSQL data. It's ideal for storing large amounts of structured, non-relational data that requires rapid access.
What is Azure Table Storage?
Azure Table Storage is a service that stores significant amounts of structured, non-relational data. It's a key-value store, meaning you store data as a collection of properties (attributes) within an entity. Each entity has a partition key and a row key, which together form its unique identifier.
Key Concepts:
- Tables: Collections of entities. Schemaless, so a table can contain entities with different property sets.
- Entities: A set of properties, similar to a row in a database. An entity can have up to 252 properties.
- Properties: Name-value pairs within an entity. Property names are strings, and values can be of various data types.
- Partition Key: A string value that helps partition the data logically. Entities with the same partition key are co-located.
- Row Key: A string value that uniquely identifies an entity within a partition.
Tutorial Steps
-
Create an Azure Storage Account:
First, you need an Azure Storage account. You can create one through the Azure portal, Azure CLI, or PowerShell. In the Azure portal, search for "Storage accounts", click "Create", and follow the prompts.
Tip: Choose the appropriate replication option (e.g., LRS, GRS) based on your data redundancy needs. -
Install Azure Table Storage Client Library:
Depending on your development language, install the relevant Azure SDK client library. For example, using NuGet for .NET:
dotnet add package Azure.Data.TablesOr using pip for Python:
pip install azure-data-tables -
Connect to Table Storage:
You'll need your storage account name and a connection string or shared access signature (SAS) to authenticate. You can find these in the "Access keys" section of your storage account in the Azure portal.
Example (Python):
from azure.data.tables import TableServiceClient connection_string = "YOUR_STORAGE_CONNECTION_STRING" table_name = "MySampleTable" table_service_client = TableServiceClient.from_connection_string(conn_str=connection_string) table_client = table_service_client.get_table_client(table_name=table_name) -
Create a Table:
If the table doesn't exist, create it. Table names must be ASCII, between 3 and 63 characters long, and can contain only lowercase letters, numbers, and hyphens. They cannot start or end with a hyphen.
Example (Python):
try: table_client.create_table() print(f"Table '{table_name}' created.") except Exception as e: print(f"Table '{table_name}' might already exist or an error occurred: {e}") -
Insert an Entity:
Entities are represented as dictionaries. You must include
PartitionKeyandRowKey.Example (Python):
from datetime import datetime entity = { u"PartitionKey": u"users", u"RowKey": u"user1", u"email": u"user1@example.com", u"age": 30, u"registeredOn": datetime.utcnow() } try: entity_client = table_client.upsert_entity(entity) print("Entity inserted successfully.") except Exception as e: print(f"Error inserting entity: {e}") -
Query Entities:
Retrieve entities from your table. You can filter by partition key or apply more complex OData filters.
Example (Python - retrieve all entities from "users" partition):
print("Retrieving entities:") for entity in table_client.list_entities(filter="PartitionKey eq 'users'"): print(f" PartitionKey: {entity['PartitionKey']}, RowKey: {entity['RowKey']}, Email: {entity['email']}") -
Update an Entity:
Use
upsert_entityto update an existing entity. If the entity exists, it's updated; otherwise, it's inserted.Example (Python - update age):
entity_to_update = { u"PartitionKey": u"users", u"RowKey": u"user1", u"age": 31 # Updated age } try: table_client.upsert_entity(entity_to_update, mode="merge") print("Entity updated successfully.") except Exception as e: print(f"Error updating entity: {e}") -
Delete an Entity:
Remove an entity using its partition key and row key.
Example (Python):
try: table_client.delete_entity(u"users", u"user1") print("Entity deleted successfully.") except Exception as e: print(f"Error deleting entity: {e}") -
Delete a Table:
Remove the entire table and all its data.
Example (Python):
try: table_client.delete_table() print(f"Table '{table_name}' deleted.") except Exception as e: print(f"Error deleting table: {e}")
Next Steps
- Explore advanced querying with OData filters.
- Learn about Table Storage performance best practices.
- Integrate Table Storage with other Azure services like Azure Functions or Web Apps.