Azure Table Storage Documentation
Azure Table Storage is a NoSQL key-attribute store that accepts unstructured, semi-structured, and structured data. It's ideal for applications that need a flexible schema, fast queries, and a highly scalable storage solution for large amounts of data.
What is Azure Table Storage?
Azure Table Storage is part of Azure Cosmos DB, a globally distributed, multi-model database service. Table Storage provides a key-value store that is optimized for applications that require a schema-less design and efficient querying by its primary key. Each table can store entities, and each entity can have a variable number of properties.
Key Concepts
- Table: A collection of entities. Tables are schemaless, meaning a table can contain entities with different sets of properties.
- Entity: A record within a table. An entity is analogous to a row in a relational database.
- Property: A name-value pair within an entity. Entities can have up to 252 properties, plus the two required system properties (PartitionKey and RowKey).
- PartitionKey: Used to group entities together. Entities with the same PartitionKey are stored together. This is crucial for performance and scalability.
- RowKey: Uniquely identifies an entity within a partition. The combination of PartitionKey and RowKey provides a unique identifier for each entity.
Common Use Cases
- Storing user data for web applications.
- Storing metadata for unstructured data (e.g., blobs).
- Storing logs and diagnostic information.
- Caching frequently accessed data.
- Managing application settings and configurations.
Getting Started
You can interact with Azure Table Storage using various SDKs for languages like .NET, Java, Node.js, Python, and Go, or via the REST API. You can also manage tables and entities using the Azure portal or Azure Storage Explorer.
Creating a Table
To create a table, you typically initialize a TableServiceClient and then call the `create_table` method (or equivalent in your chosen SDK).
from azure.data.tables import TableServiceClient
connection_string = "YOUR_CONNECTION_STRING"
table_name = "my-new-table"
try:
table_service_client = TableServiceClient.from_connection_string(connection_string)
table = table_service_client.create_table(table_name)
print(f"Table '{table_name}' created successfully.")
except Exception as e:
print(f"An error occurred: {e}")
Inserting an Entity
Entities are represented as dictionaries or custom objects. You need to specify the PartitionKey and RowKey.
from azure.data.tables import TableServiceClient, Entity
connection_string = "YOUR_CONNECTION_STRING"
table_name = "my-new-table"
# Sample entity data
entity_data = {
'PartitionKey': 'users',
'RowKey': 'user123',
'Name': 'Alice Smith',
'Email': 'alice.smith@example.com',
'Age': 30
}
try:
table_service_client = TableServiceClient.from_connection_string(connection_string)
table_client = table_service_client.get_table_client(table_name=table_name)
entity = Entity()
entity.update(entity_data)
created_entity = table_client.upsert_entity(entity)
print(f"Entity inserted successfully: {created_entity}")
except Exception as e:
print(f"An error occurred: {e}")
upsert_entity to insert or update an entity in a single operation.
Querying Data
Queries are highly efficient when filtering by PartitionKey and RowKey. You can also perform table-wide queries, but these can be more expensive for large tables.
Querying by PartitionKey and RowKey
from azure.data.tables import TableServiceClient
connection_string = "YOUR_CONNECTION_STRING"
table_name = "my-new-table"
try:
table_service_client = TableServiceClient.from_connection_string(connection_string)
table_client = table_service_client.get_table_client(table_name=table_name)
partition_key = 'users'
row_key = 'user123'
entity = table_client.get_entity(partition_key, row_key)
print(f"Retrieved entity: {entity}")
except Exception as e:
print(f"An error occurred: {e}")
Querying Entities within a Partition
from azure.data.tables import TableServiceClient
connection_string = "YOUR_CONNECTION_STRING"
table_name = "my-new-table"
try:
table_service_client = TableServiceClient.from_connection_string(connection_string)
table_client = table_service_client.get_table_client(table_name=table_name)
partition_key = 'users'
filter_query = f"PartitionKey eq '{partition_key}'"
entities = table_client.list_entities(filter=filter_query)
print(f"Entities in partition '{partition_key}':")
for entity in entities:
print(entity)
except Exception as e:
print(f"An error occurred: {e}")
Learn More
Explore the following resources for in-depth information: