This document outlines the methods for inserting entities into Azure Storage Tables using various SDKs and REST APIs.
An entity in Azure Table Storage is a collection of properties. Each entity must have at least two properties: PartitionKey and RowKey. These two properties together form the unique identifier for an entity within a table. The Timestamp property is automatically managed by the service.
Azure Table Storage supports several operations for inserting entities:
PartitionKey.This is the most common operation for adding new data. You can use the Azure Storage SDKs for your preferred language or the REST API.
Here's an example using the Azure SDK for Python to insert a single entity:
from azure.data.tables import TableServiceClient
from azure.core.exceptions import ResourceExistsError
# Replace with your actual connection string
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
table_name = "MySampleTable"
try:
table_service_client = TableServiceClient.from_connection_string(connection_string)
table_client = table_service_client.get_table_client(table_name=table_name)
entity = {
"PartitionKey": "Customers",
"RowKey": "user123",
"Name": "Alice Wonderland",
"Email": "alice@example.com",
"Age": 30
}
# Insert the entity
created_entity = table_client.create_entity(entity)
print(f"Entity inserted successfully: {created_entity}")
except ResourceExistsError:
print("Entity with the same PartitionKey and RowKey already exists.")
except Exception as e:
print(f"An error occurred: {e}")
Here's an example using the Azure SDK for .NET:
using Azure.Data.Tables;
using System;
// Replace with your actual connection string
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
string tableName = "MySampleTable";
try
{
var tableServiceClient = new TableServiceClient(connectionString);
TableClient tableClient = tableServiceClient.GetTableClient(tableName);
var entity = new Dictionary<string, object>
{
{ "PartitionKey", "Customers" },
{ "RowKey", "user456" },
{ "Name", "Bob The Builder" },
{ "Email", "bob@example.com" },
{ "Age", 42 }
};
// Insert the entity
var response = await tableClient.UpsertEntityAsync(entity, TableUpdateMode.Replace); // UpsertEntity can also be used with TableUpdateMode.Merge
Console.WriteLine($"Entity inserted/updated successfully. Status: {response.Status}");
}
catch (RequestFailedException ex) when (ex.Status == 409)
{
Console.WriteLine("Entity with the same PartitionKey and RowKey already exists.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
Batch operations allow you to send multiple insert, update, or delete operations for entities that share the same PartitionKey in a single HTTP request. This is more efficient than performing individual operations.
PartitionKey.
Here's an example using the Azure SDK for Java to perform a batch insert:
import com.azure.data.tables.TableClient;
import com.azure.data.tables.TableServiceClient;
import com.azure.data.tables.TableServiceClientBuilder;
import com.azure.data.tables.models.TableEntity;
import com.azure.data.tables.models.TableTransactionAction;
import com.azure.data.tables.models.TableTransactionActionType;
import com.azure.core.util.Context;
import com.azure.core.exception. bất kỳ exception; // Assuming a generic exception for simplicity
// Replace with your actual connection string
String connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
String tableName = "MySampleTable";
try {
TableServiceClient tableServiceClient = new TableServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
TableClient tableClient = tableServiceClient.getTableClient(tableName);
// Create entities for the same PartitionKey
TableEntity entity1 = new TableEntity("Products", "sku001")
.addProperty("Name", "Laptop")
.addProperty("Price", 1200.00);
TableEntity entity2 = new TableEntity("Products", "sku002")
.addProperty("Name", "Mouse")
.addProperty("Price", 25.50);
// Create transaction actions for inserts
TableTransactionAction insertAction1 = new TableTransactionAction(TableTransactionActionType.CREATE, entity1);
TableTransactionAction insertAction2 = new TableTransactionAction(TableTransactionActionType.CREATE, entity2);
// Submit the batch
var response = tableClient.submitTransaction(Arrays.asList(insertAction1, insertAction2), Context.NONE);
System.out.println("Batch insert completed. Status codes: " + response.stream()
.map(transactionResponse -> Integer.toString(transactionResponse.getStatusCode()))
.collect(Collectors.joining(", ")));
} catch ( bất kỳ exception e) {
System.err.println("An error occurred during batch insert: " + e.getMessage());
e.printStackTrace();
}
You can add any number of custom properties to an entity. Each property consists of a name (string) and a value. The value can be of various supported data types, including String, Int32, Int64, Double, Boolean, DateTime, Guid, Binary, Double, etc.
PartitionKey and RowKey carefully. A well-designed partitioning strategy is crucial for efficient querying and scaling.
You can also insert entities directly using the Azure Table Storage REST API.
Method: POST
URI: https://{accountName}.table.core.windows.net/{tableName}
Headers:
| Header | Description |
|---|---|
Authorization |
SharedKey or SAS token for authentication. |
Accept |
application/json;odata=minimalmetadata |
Content-Type |
application/json;odata=minimalmetadata |
Request Body (JSON):
{
"PartitionKey": { "String": "Sensors" },
"RowKey": { "String": "sensor-101" },
"Temperature": { "Double": 25.5 },
"Humidity": { "Int32": 60 },
"ReadingTime": { "DateTime": "2023-10-27T10:30:00Z" }
}
Inserting entities is a fundamental operation when working with Azure Table Storage. By understanding the different insertion methods and the structure of entities, you can efficiently manage your data.
For more detailed information, please refer to the official Azure Table Storage REST API documentation.