Azure Data Tables Client Library
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
- breaking Major API changes exist when migrating from older Azure Table Storage SDKs (e.g., `azure-storage-table` v1.x or `azure-cosmosdb-table`) to `azure-data-tables` (v12.x). This is a complete re-architecture following Azure SDK 'track 2' guidelines.
- gotcha Azure Table Storage/Cosmos DB for Table API require every entity to have a `PartitionKey` and `RowKey`. These must be string types and together form the unique identifier for an entity within a table.
- gotcha Azure Table Storage operations (especially queries) are eventually consistent. A write operation might not be immediately visible to subsequent read operations across all nodes.
- gotcha Entity Group Transactions (EGTs) are limited. All operations within an EGT (batch operation) must operate on entities that share the *same PartitionKey*.
Install
-
pip install azure-data-tables
Imports
- TableServiceClient
from azure.data.tables import TableServiceClient
- TableClient
from azure.data.tables import TableClient
- ResourceExistsError
from azure.core.exceptions import ResourceExistsError
Quickstart
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}")