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:

Tutorial Steps

  1. 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.
  2. 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.Tables

    Or using pip for Python:

    pip install azure-data-tables
  3. 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)
  4. 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}")
  5. Insert an Entity:

    Entities are represented as dictionaries. You must include PartitionKey and RowKey.

    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}")
  6. 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']}")
  7. Update an Entity:

    Use upsert_entity to 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}")
  8. 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}")
  9. 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}")
Note: For production scenarios, consider using the Azure SDK for more robust error handling, batch operations, and advanced features like querying with indexes.

Next Steps