Creating Entities
This article explains how to create entities in Azure Storage Tables using various methods, including the Azure SDKs for .NET, Java, Python, and Node.js.
Overview
Entities in Azure Table Storage are represented as collections of properties. Each entity must have at least two properties: PartitionKey and RowKey. These two properties together form the entity's unique identifier (its primary key).
- PartitionKey: Used to partition entities for scalability and performance. Entities with the same
PartitionKeyare typically stored on the same storage node. - RowKey: A unique identifier for an entity within a given partition.
Other properties can be of various primitive data types, including strings, integers, dates, booleans, GUIDs, and doubles. Complex types are not directly supported and need to be serialized.
Methods for Creating Entities
1. Using the Azure SDKs
The most common and recommended way to interact with Azure Table Storage is by using the official Azure SDKs. Here are examples for common languages.
.NET Example
The following code snippet demonstrates creating an entity using the Azure.Data.Tables NuGet package.
using Azure;
using Azure.Data.Tables;
// Replace with your connection string
string connectionString = "";
string tableName = "MyProducts";
// Create a TableServiceClient using the connection string
TableServiceClient tableServiceClient = new TableServiceClient(connectionString);
// Get a reference to the table, creating it if it doesn't exist
TableClient tableClient = await tableServiceClient.CreateTableIfNotExistsAsync(tableName);
// Define a dynamic entity
var product = new
{
PartitionKey = "Electronics",
RowKey = "1001",
Name = "Laptop",
Price = 1200.50,
Stock = 50,
Description = "A high-performance laptop."
};
// Add the entity to the table
Response<Response> result = await tableClient.AddEntityAsync(product);
Console.WriteLine($"Entity created successfully. Status: {result.GetRawResponse().Status}");
Python Example
Using the Azure Table Storage SDK for Python.
from azure.data.tables import TableServiceClient
# Replace with your connection string
connection_string = ""
table_name = "MyProducts"
# Create a TableServiceClient
table_service_client = TableServiceClient.from_connection_string(connection_string)
# Get a table client, creating it if it doesn't exist
table_client = table_service_client.get_table_client(table_name)
table_client.create_table() # This will raise an exception if table already exists
# Define the entity as a dictionary
entity = {
"PartitionKey": "Electronics",
"RowKey": "1002",
"Name": "Tablet",
"Price": 300.75,
"Stock": 120,
"Description": "A versatile tablet device."
}
# Add the entity to the table
table_client.upsert_entity(entity)
print("Entity created successfully.")
Node.js Example
Using the Azure Table Storage SDK for Node.js.
const { TableServiceClient } = require("@azure/data-tables");
// Replace with your connection string
const connectionString = "";
const tableName = "MyProducts";
async function createEntity() {
const tableServiceClient = TableServiceClient.fromConnectionString(connectionString);
const tableClient = tableServiceClient.getTableClient(tableName);
// Create the table if it doesn't exist
await tableClient.createTable();
const entity = {
partitionKey: "Electronics",
rowKey: "1003",
name: "Smartwatch",
price: 199.99,
stock: 80,
description: "A modern smartwatch with fitness tracking."
};
await tableClient.createEntity(entity);
console.log("Entity created successfully.");
}
createEntity().catch(error => {
console.error("Error creating entity:", error);
});
2. Using the Azure Portal
You can also create entities directly through the Azure Portal:
- Navigate to your Storage Account in the Azure Portal.
- Under "Data storage", select "Tables".
- Select the table where you want to add an entity.
- Click "Add entity".
- Fill in the
PartitionKey,RowKey, and any other desired properties with their respective values and data types. - Click "OK" to save the entity.
3. Using Azure Storage Emulator (for development)
For local development, you can use the Azure Storage Emulator to test your code without incurring cloud costs. The SDKs typically support connecting to the emulator by configuring the connection string appropriately (often using UseDevelopmentStorage=true).
Entity Properties Constraints
PartitionKeyandRowKeyare required and serve as the primary key.- Property names are case-sensitive.
- Property names cannot start with a system-reserved character (like
sys). - Property names have a maximum length of 255 characters.
- The total size of an entity cannot exceed 1 MB.
- Supported data types include String, Boolean, Byte array, DateTime, Double, GUID, Int32, Int64, and EntityProperty.
PartitionKey and RowKey values is crucial for the performance and scalability of your table.
Next Steps
After creating entities, you will typically want to query or modify them. Refer to the following articles: