Azure Functions Table Output Bindings
Table output bindings allow your Azure Functions to write data to an Azure Table Storage table. This is a powerful way to persist structured data from your serverless code.
Overview
When you configure an output binding for Azure Table Storage, your function can return or write an object to the binding, and the Azure Functions runtime handles the creation of the entity in the specified table.
Configuration
You configure table output bindings in your function's function.json file. Here's a typical configuration:
{
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"post"
]
},
{
"type": "table",
"direction": "out",
"name": "outputTable",
"tableName": "myOutputTable",
"connection": "AzureWebJobsStorage"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Key properties:
type: Must be"table".direction: Must be"out".name: The name of the parameter in your function code that will receive the output.tableName: The name of the Azure Table Storage table to write to.connection: The name of the app setting that contains the Azure Table Storage connection string. Defaults to"AzureWebJobsStorage".
Writing Data
You can write data to the table output binding in several ways, depending on your language.
C# Example
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Threading.Tasks;
public static class AddTableEntry
{
[FunctionName("AddTableEntry")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
[Table("myOutputTable", Connection = "AzureWebJobsStorage")] out MyTableEntity outputEntity,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request to add a table entry.");
string requestBody = await new System.IO.StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
if (data?.partitionKey == null || data?.rowKey == null)
{
// Basic validation - in production, use more robust validation
outputEntity = null; // Ensure no entity is written if validation fails
return;
}
outputEntity = new MyTableEntity()
{
PartitionKey = data.partitionKey.ToString(),
RowKey = data.rowKey.ToString(),
Data = data.data?.ToString() ?? "No additional data"
};
log.LogInformation($"Entity written to table: PartitionKey={outputEntity.PartitionKey}, RowKey={outputEntity.RowKey}");
}
}
public class MyTableEntity : Microsoft.Azure.Cosmos.Table.TableEntity
{
public string Data { get; set; }
}
JavaScript Example (Node.js)
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request to add a table entry.');
const partitionKey = req.query.partitionKey || (req.body && req.body.partitionKey);
const rowKey = req.query.rowKey || (req.body && req.body.rowKey);
const data = req.query.data || (req.body && req.body.data);
if (partitionKey && rowKey) {
context.bindings.outputTable = {
partitionKey: partitionKey,
rowKey: rowKey,
data: data || 'No additional data'
};
context.res = {
status: 201,
body: "Successfully added entity to table."
};
} else {
context.res = {
status: 400,
body: "Please provide partitionKey and rowKey in the request."
};
}
};
Important Considerations
When writing to Azure Table Storage, remember that each entity requires a PartitionKey and a RowKey. These two properties together form the unique identifier for an entity.
Performance Tip
For bulk inserts or updates, consider using the TableBatchOperation class in .NET or similar mechanisms in other languages to group multiple operations into a single request, significantly improving performance and reducing costs.
Entity Structure
The object you bind to the output should have properties that map to the table entity's properties. Standard properties like PartitionKey and RowKey are automatically handled. You can include custom properties that will be stored as columns in the table.
For instance, the MyTableEntity class in the C# example defines a custom property Data.
Next Steps
Explore how to read data from Azure Table Storage using input bindings, and learn about other Azure Storage integrations for your functions.