Azure Functions Table Input Bindings
This document describes how to use the Azure Table storage input binding in Azure Functions.
The Azure Table storage input binding allows your function to read data from an Azure Table. You can retrieve a single entity, a collection of entities, or a query results set.
Prerequisites
- An Azure subscription.
- An Azure Storage account.
- An Azure Function App configured to use the Azure Tables binding extension.
Binding Configuration
The table input binding is configured in the function.json file for a specific function. Here's an example of a function.json with a table input binding:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "inputTable",
"type": "table",
"direction": "in",
"tableName": "MySampleTable",
"connection": "AzureWebJobsStorage",
"partitionKey": "{partitionKey}",
"rowKey": "{rowKey}"
},
{
"name": "msg",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
],
"authLevel": "function"
},
{
"name": "output",
"type": "http",
"direction": "out"
}
]
}
Binding Parameters
| Parameter | Description | Required |
|---|---|---|
name |
The name of the parameter in your function code that will receive the table data. | Yes |
type |
Must be set to table for a table input binding. |
Yes |
direction |
Must be set to in for an input binding. |
Yes |
tableName |
The name of the Azure Table to read from. | Yes |
connection |
The name of an app setting that contains the Azure Storage connection string. Defaults to AzureWebJobsStorage. |
No |
partitionKey |
The partition key of the entity to retrieve. Can be a literal value or a dynamic value from the trigger or other input. | No (depends on use case) |
rowKey |
The row key of the entity to retrieve. Can be a literal value or a dynamic value from the trigger or other input. | No (depends on use case) |
Function Code Examples
C# Example
This example shows how to use a table input binding to retrieve a single entity.
using Microsoft.Azure.WebJobs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace MyFunctionApp
{
public static class GetTableEntity
{
[FunctionName("GetTableEntity")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "entity/{partitionKey}/{rowKey}")] HttpRequest req,
string partitionKey,
string rowKey,
[Table("MySampleTable", "{partitionKey}", "{rowKey}", Connection = "AzureWebJobsStorage")] dynamic entity,
ILogger log)
{
log.LogInformation($"C# HTTP trigger function processed a request for entity: PartitionKey={partitionKey}, RowKey={rowKey}");
if (entity == null)
{
return new NotFoundResult();
}
return new OkObjectResult(entity);
}
}
}
JavaScript Example (Node.js)
This example shows how to use a table input binding to retrieve a single entity.
const { app } = require('@azure/functions');
app.http('getTableEntity', {
methods: ['GET', 'POST'],
authLevel: 'function',
route: 'entity/{partitionKey}/{rowKey}',
handler: async (request, context) => {
const partitionKey = context.bindingData.partitionKey;
const rowKey = context.bindingData.rowKey;
context.log(`JavaScript HTTP trigger function processed a request for entity: PartitionKey=${partitionKey}, RowKey=${rowKey}`);
const entity = context.bindings.inputTable; // 'inputTable' matches the 'name' in function.json
if (!entity) {
return { status: 404 };
}
return { body: entity };
}
});
Python Example
This example shows how to use a table input binding to retrieve a single entity.
import azure.functions as func
import logging
def main(req: func.HttpRequest, inputTable: func.Out[func.Document]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
partitionKey = req.route_params.get('partitionKey')
rowKey = req.route_params.get('rowKey')
logging.info(f"Retrieving entity: PartitionKey={partitionKey}, RowKey={rowKey}")
# The inputTable binding will automatically fetch the entity based on partitionKey and rowKey
# defined in function.json. If it's not found, it will be None.
if inputTable.get() is None:
return func.HttpResponse(
"Entity not found.",
status_code=404
)
else:
return func.HttpResponse(
body=inputTable.get().to_json(),
mimetype="application/json",
status_code=200
)
Retrieving Multiple Entities
To retrieve multiple entities, you can omit the partitionKey and rowKey in the binding configuration and then use a query within your function code. Alternatively, you can specify a partitionKey and retrieve all entities within that partition.
Example: Retrieving all entities from a partition (Python)
In function.json:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "inputTable",
"type": "table",
"direction": "in",
"tableName": "MySampleTable",
"connection": "AzureWebJobsStorage",
"partitionKey": "{partitionKey}"
},
{
"name": "msg",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get"
],
"route": "entities/{partitionKey}",
"authLevel": "function"
},
{
"name": "output",
"type": "http",
"direction": "out"
}
]
}
In __init__.py:
import azure.functions as func
import logging
def main(req: func.HttpRequest, inputTable: func.Out[func.Document]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
partitionKey = req.route_params.get('partitionKey')
logging.info(f"Retrieving all entities for PartitionKey={partitionKey}")
entities = inputTable.get_all()
if not entities:
return func.HttpResponse(
"No entities found for the specified partition.",
status_code=404
)
else:
return func.HttpResponse(
body=",\n".join([entity.to_json() for entity in entities]),
mimetype="application/json",
status_code=200
)
Key Concepts
- Partition Key: Groups entities together. Entities with the same partition key are co-located.
- Row Key: Uniquely identifies an entity within a partition.
- Connection String: Specifies how to connect to your Azure Storage account.
For more advanced querying, consider using the Azure Table storage SDK directly within your function.