Azure Data Tables Client Library

12.7.0 · active · verified Thu Apr 09

The `azure-data-tables` library is the Python SDK for Azure Table Storage and Azure Cosmos DB for Table API. It provides synchronous and asynchronous clients for managing tables, entities, and performing queries. This library is part of the 'track 2' Azure SDK, offering a modern, consistent, and Pythonic interface. The current stable version is 12.7.0, with releases typically aligning with broader Azure SDK updates for bug fixes and minor features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `TableServiceClient` using a connection string, create a table, add an entity, query for it, and then delete it. For production, consider using `azure-identity` with `DefaultAzureCredential` and an endpoint URL for authentication.

import os
from azure.data.tables import TableServiceClient, TableEntity
from azure.core.exceptions import ResourceExistsError

# --- Configuration ---
# Replace with your storage account connection string or set as an environment variable
# Example: 'DefaultEndpointsProtocol=https;AccountName=<account>;AccountKey=<key>;EndpointSuffix=core.windows.net'
connection_string = os.environ.get('AZURE_TABLES_CONNECTION_STRING', 'YOUR_CONNECTION_STRING_HERE')

# --- Client Initialization ---
# For AAD auth, use TableServiceClient(endpoint=os.environ.get('AZURE_TABLES_ENDPOINT'), credential=DefaultAzureCredential())
# Ensure 'AZURE_TABLES_ENDPOINT' is set (e.g., 'https://<accountname>.table.core.windows.net/')

try:
    table_service_client = TableServiceClient.from_connection_string(conn_str=connection_string)
    table_name = "MyTestTable"
    table_client = table_service_client.get_table_client(table_name=table_name)

    # --- Create Table (if it doesn't exist) ---
    try:
        print(f"Creating table '{table_name}'...")
        table_client.create_table()
        print(f"Table '{table_name}' created.")
    except ResourceExistsError:
        print(f"Table '{table_name}' already exists.")

    # --- Add an Entity ---
    print("Adding entity...")
    entity = TableEntity(PartitionKey="pk1", RowKey="rk1", name="Alice", age=30)
    table_client.upsert_entity(entity)
    print("Entity added/updated.")

    # --- Query Entity ---
    print("Querying entity...")
    queried_entity = table_client.get_entity(partition_key="pk1", row_key="rk1")
    print(f"Queried entity: {queried_entity['name']}, {queried_entity['age']}")

    # --- List Entities (simple query) ---
    print("Listing entities...")
    entities = table_client.query_entities(filter="PartitionKey eq 'pk1'")
    for ent in entities:
        print(f" - PK: {ent['PartitionKey']}, RK: {ent['RowKey']}, Name: {ent.get('name')}")

    # --- Delete Entity ---
    print("Deleting entity...")
    table_client.delete_entity(partition_key="pk1", row_key="rk1")
    print("Entity deleted.")

    # --- Delete Table (cleanup) ---
    # print(f"Deleting table '{table_name}'...")
    # table_client.delete_table()
    # print(f"Table '{table_name}' deleted.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →