Microsoft Docs

Azure Functions Table Bindings

Azure Functions provide a powerful way to build serverless applications. Bindings are a declarative way to connect to data and services without having to write complex integration code. This document focuses on the Table bindings, which allow you to interact with Azure Table Storage.

Introduction to Azure Table Storage

Azure Table Storage is a NoSQL key-attribute store that stores large amounts of structured, non-relational data. It's a cost-effective service for storing data that requires rapid development and access.

Table Input Binding

An input binding for Azure Table Storage allows your function to read data from a table. You can retrieve a single entity or a collection of entities.

Reading a Single Entity

To read a single entity, you typically specify the partition key and row key.

{
  "bindings": [
    {
      "name": "inputTable",
      "type": "table",
      "direction": "in",
      "tableName": "MyTable",
      "partitionKey": "{partitionKey}",
      "rowKey": "{rowKey}"
    }
  ]
}

In your function code, you'll receive an object representing the entity:

// C# example
public static void Run(
    MyEntityType inputTable,
    ILogger log)
{
    if (inputTable != null)
    {
        log.LogInformation($"Found entity: {inputTable.Name}");
    }
    else
    {
        log.LogInformation("Entity not found.");
    }
}

public class MyEntityType
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Name { get; set; }
    // Other properties...
}

Reading Multiple Entities (Querying)

For more complex queries, you can use a query binding.

{
  "bindings": [
    {
      "name": "entities",
      "type": "table",
      "direction": "in",
      "tableName": "MyTable",
      "filter": "PartitionKey eq '{partitionKey}'"
    }
  ]
}

This would bind to a collection of entities in your function:

// C# example
public static void Run(
    IEnumerable<MyEntityType> entities,
    ILogger log)
{
    foreach (var entity in entities)
    {
        log.LogInformation($"Processing entity: {entity.Name}");
    }
}

Table Output Binding

An output binding allows your function to write data to an Azure Table Storage table. This is commonly used for inserting or updating entities.

Inserting or Updating an Entity

{
  "bindings": [
    {
      "name": "outputTable",
      "type": "table",
      "direction": "out",
      "tableName": "MyTable"
    }
  ]
}

In your function code, you'll assign data to the output binding:

// C# example
public static void Run(
    string inputData,
    out MyEntityType outputTable,
    ILogger log)
{
    outputTable = new MyEntityType
    {
        PartitionKey = "Customers",
        RowKey = Guid.NewGuid().ToString(),
        Name = "New Customer"
        // Set other properties from inputData
    };
    log.LogInformation($"Inserting entity with RowKey: {outputTable.RowKey}");
}

Important Considerations

When using output bindings to insert or update, ensure you provide both a PartitionKey and a RowKey for each entity.

Connection Strings and Configuration

Table bindings use connection strings defined in your Azure Functions application settings. The default setting name for Azure Table Storage is AzureWebJobsStorage. You can specify a different connection setting using the connection property in the binding configuration.

Example `function.json` with custom connection

{
  "bindings": [
    {
      "name": "inputTable",
      "type": "table",
      "direction": "in",
      "tableName": "MyTable",
      "connection": "MyTableStorageConnection"
    }
  ]
}

In your application's configuration (e.g., local.settings.json or Azure App Settings), you would define:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "MyTableStorageConnection": "DefaultEndpointsProtocol=https;AccountName=YOUR_ACCOUNT_NAME;AccountKey=YOUR_ACCOUNT_KEY;EndpointSuffix=core.windows.net"
  }
}

Common Scenarios

Note on Performance

For highly transactional workloads or complex querying needs, consider other Azure storage options like Azure Cosmos DB or Azure SQL Database.

Further Reading