Inserting Entities into Azure Storage Tables
This document details how to insert entities into an Azure Storage table using various SDKs and REST APIs. Azure Table Storage is a NoSQL key-attribute store that allows you to store large amounts of structured, non-relational data.
Understanding Entities and Operations
An entity in Azure Table Storage is a set of properties that can be treated as a single unit. Each entity is uniquely identified by its PartitionKey and RowKey. Inserting an entity means adding a new record to a table.
Common Insertion Methods
1. Using the Azure Storage SDK for .NET
The .NET SDK provides convenient methods for interacting with Azure Storage. The CloudTableClient and TableOperation objects are central to performing operations.
using Microsoft.Azure.Cosmos.Table;
using System;
using System.Threading.Tasks;
// Assuming 'cloudTable' is an initialized CloudTable object
// Create an entity
var customer = new DynamicTableEntity("Partition1", "Cust1")
{
Properties = new Dictionary()
{
{ "FirstName", new EntityProperty("John") },
{ "LastName", new EntityProperty("Doe") },
{ "Email", new EntityProperty("john.doe@example.com") },
{ "IsActive", new EntityProperty(true) }
}
};
// Create an insert operation
var insertOperation = TableOperation.Insert(customer);
// Execute the operation
var result = await cloudTable.ExecuteAsync(insertOperation);
if (result.HttpStatusCode >= 200 && result.HttpStatusCode < 300)
{
Console.WriteLine("Entity inserted successfully.");
}
else
{
Console.WriteLine($"Error inserting entity: {result.HttpStatusCode}");
}
Note: The DynamicTableEntity class is flexible, allowing you to define properties dynamically. For strongly-typed entities, you can inherit from TableEntity.
2. Using the Azure Storage SDK for Python
The Python SDK offers a similar, intuitive approach.
from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity
# Replace with your actual storage account connection string
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
table_service = TableService(connection_string=connection_string)
table_name = "Customers"
# Create an entity
entity = Entity()
entity.PartitionKey = "Partition1"
entity.RowKey = "Cust2"
entity.FirstName = "Jane"
entity.LastName = "Smith"
entity.Email = "jane.smith@example.com"
entity.IsActive = True
try:
table_service.insert_entity(table_name, entity)
print("Entity inserted successfully.")
except Exception as e:
print(f"Error inserting entity: {e}")
3. Using the Azure Storage REST API
You can also interact with Azure Table Storage directly using HTTP requests.
To insert an entity, you would typically send a POST request to the table's URI with an application/json payload.
Request URL:
https://<your-storage-account>.table.core.windows.net/<table-name>
Request Headers:
Content-Type: application/json
Authorization: SharedKey <your-storage-account>:<signature>
Date: <formatted-date>
Accept: application/json;odata=fullmetal
Request Body (JSON):
{
"PartitionKey": {"String": "Partition1"},
"RowKey": {"String": "Cust3"},
"FirstName": {"String": "Peter"},
"LastName": {"String": "Jones"},
"Email": {"String": "peter.jones@example.com"},
"IsActive": {"Boolean": true}
}
Important: When using the REST API, you must correctly construct the authorization signature. Refer to the Azure Storage authentication documentation for details.
Batch Insertions
For improved performance, especially when inserting multiple entities, consider using batch operations. Azure Table Storage supports batch operations for up to 100 entities. Note that batch operations must all belong to the same partition.
.NET Example for Batch Insert
var batchOperations = new List<TableOperation>();
batchOperations.Add(TableOperation.Insert(entity1));
batchOperations.Add(TableOperation.Insert(entity2));
// ... up to 100 operations
var batchResult = await cloudTable.ExecuteBatchAsync(batchOperations);
foreach (var result in batchResult)
{
if (result.HttpStatusCode < 200 || result.HttpStatusCode >= 300)
{
Console.WriteLine($"Error in batch: {result.HttpStatusCode}");
}
}
Considerations for Insertion
- PartitionKey and RowKey: Choose these keys wisely for efficient querying and distribution.
- Data Types: Ensure properties are correctly typed when inserting.
- Concurrency: For concurrent updates or inserts, consider using
InsertOrReplaceorInsertOrMergeoperations, or implement optimistic concurrency control. - Performance: Batch operations are recommended for inserting multiple entities within the same partition.
Tip: For simple inserts of single entities, Insert is suitable. If the entity might already exist and you want to overwrite it, use InsertOrReplace. To merge properties of an existing entity, use InsertOrMerge.