Azure Documentation

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

Note: For optimal performance, design your PartitionKey and RowKey values to distribute your data across partitions.

Common Use Cases

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}")
            
Tip: Use 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}")
            
Important: Always strive to filter queries by PartitionKey to ensure performance.

Learn More

Explore the following resources for in-depth information: