Creating Entities in Azure Table Storage
This document guides you through the process of creating entities within your Azure Table Storage tables. Entities are the fundamental units of data stored in Azure Tables. Each entity is a set of properties, represented as key-value pairs.
Prerequisites
Before you begin, ensure you have:
- An Azure Storage account.
- A table created within your storage account.
Understanding Entity Properties
Every entity in Azure Table Storage must include two system properties:
- PartitionKey: A string that determines the entity's partition. Entities with the same
PartitionKeyare grouped together in the same physical partition. - RowKey: A string that uniquely identifies an entity within a partition.
In addition to these system properties, you can define custom properties. Property names are case-insensitive strings and can contain up to 255 characters. Property values can be of various Azure Table Storage data types.
Methods for Creating Entities
You can create entities using various methods, including the Azure SDKs, Azure CLI, Azure PowerShell, or REST API.
Using Azure SDKs (Example: .NET)
The Azure SDKs provide convenient ways to interact with Table Storage. Here's a C# example using the Azure SDK for .NET:
using Azure;
using Azure.Data.Tables;
// Replace with your connection string and table name
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string tableName = "Tasks";
// Authenticate to Azure Table Storage
TableServiceClient tableServiceClient = new TableServiceClient(connectionString);
TableClient tableClient = tableServiceClient.GetTableClient(tableName);
// Create a new entity
var task = new
{
PartitionKey = "TasksToDo",
RowKey = Guid.NewGuid().ToString(), // Unique RowKey for each entity
Description = "Buy groceries",
IsCompleted = false,
DueDate = DateTimeOffset.UtcNow.AddDays(1)
};
try
{
tableClient.UpsertEntity(BinaryData.FromObjectAsJson(task), TableUpdateMode.Replace);
Console.WriteLine("Entity created successfully.");
}
catch (RequestFailedException e)
{
Console.WriteLine($"Error creating entity: {e.Message}");
}
Using Azure CLI
You can also create entities using the Azure Command-Line Interface (CLI).
Create an Entity using `az storage table entity insert`
To insert an entity, you'll typically use a JSON file containing the entity's properties.
Example JSON file (entity.json)
{
"PartitionKey": "TasksToDo",
"RowKey": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
"Description": "Schedule dentist appointment",
"IsCompleted": false,
"DueDate": "2023-12-31T10:00:00Z"
}
Azure CLI command
az storage table entity insert \
--connection-string "YOUR_AZURE_STORAGE_CONNECTION_STRING" \
--table-name Tasks \
--entity @entity.json
Important Considerations
When creating entities, keep the following in mind:
- Property Limits: Property names are limited to 255 characters. The total size of an entity is limited to 1 MB.
- Data Types: Azure Table Storage supports a variety of data types, including strings, numbers, booleans, dates, GUIDs, and binary data.
- Concurrency: Use the `ETag` property and appropriate update modes (like optimistic concurrency) to handle concurrent updates gracefully.
RowKey. For sequential or related entities, you might use timestamps or other identifiers.
Next Steps
Once you have created entities, you will likely want to query them. Refer to the Querying Entities document for more information.