大网数据广告活动
Azure 网站与应用

Azure Functions 的 Azure 表存储输入绑定

· 52CE 我爱测

了解如何在 Azure Functions 中使用 Azure 表存储输入绑定。

使用 Azure 表存储输入绑定读取 Azure 存储帐户中的表。

示例

C#

一个实体

以下示例演示读取单个表行的 C# 函数。 对于发送到队列的每个消息,将触发该函数。

行键值“{queueTrigger}”指示行键来自队列消息字符串。

public class TableStorage
{
    public class MyPoco
    {
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public string Text { get; set; }
    }

    [FunctionName("TableInput")]
    public static void TableInput(
        [QueueTrigger("table-items")] string input, 
        [Table("MyTable", "MyPartition", "{queueTrigger}")] MyPoco poco, 
        ILogger log)
    {
        log.LogInformation($"PK={poco.PartitionKey}, RK={poco.RowKey}, Text={poco.Text}");
    }
}

CloudTable

CloudTable 仅在 Functions v2 及更高版本的运行时中受支持。

使用 CloudTable 方法参数通过 Azure 存储 SDK 来读取表。 下面是一个查询 Azure Functions 日志表的函数示例:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Cosmos.Table;
using System;
using System.Threading.Tasks;

namespace FunctionAppCloudTable2
{
    public class LogEntity : TableEntity
    {
        public string OriginalName { get; set; }
    }
    public static class CloudTableDemo
    {
        [FunctionName("CloudTableDemo")]
        public static async Task Run(
            [TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, 
            [Table("AzureWebJobsHostLogscommon")] CloudTable cloudTable,
            ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            TableQuery<LogEntity> rangeQuery = new TableQuery<LogEntity>().Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, 
                        "FD2"),
                    TableOperators.And,
                    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThan, 
                        "t")));

            // Execute the query and loop through the results
            foreach (LogEntity entity in 
                await cloudTable.ExecuteQuerySegmentedAsync(rangeQuery, null))
            {
                log.LogInformation(
                    $"{entity.PartitionKey}\t{entity.RowKey}\t{entity.Timestamp}\t{entity.OriginalName}");
            }
        }
    }
}

有关如何使用 CloudTable 的详细信息,请参阅 Azure 表存储入门。

如果在尝试绑定到 CloudTable 时出现错误消息,请确保引用正确的存储 SDK 版本。

IQueryable

IQueryable 仅在 Functions v1 运行时中受支持。

以下示例显示了一个 C# 函数,它从 TableEntity 中读取其中派生了 MyPoco 类的多个表行。

public class TableStorage
{
    public class MyPoco : TableEntity
    {
        public string Text { get; set; }
    }

    [FunctionName("TableInput")]
    public static void TableInput(
        [QueueTrigger("table-items")] string input, 
        [Table("MyTable", "MyPartition")] IQueryable<MyPoco> pocos, 
        ILogger log)
    {
        foreach (MyPoco poco in pocos)
        {
            log.LogInformation($"PK={poco.PartitionKey}, RK={poco.RowKey}, Text={poco.Text}");
        }
    }
}

C# 脚本

一个实体

以下示例演示 function.json 文件中的一个表输入绑定以及使用该绑定的 C# 脚本代码。 该函数使用队列触发器来读取单个表行。

function.json 文件指定 partitionKey 和 rowKey。 rowKey 值“{queueTrigger}”指示行键来自队列消息字符串。

{
  "bindings": [
    {
      "queueName": "myqueue-items",
      "connection": "MyStorageConnectionAppSetting",
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in"
    },
    {
      "name": "personEntity",
      "type": "table",
      "tableName": "Person",
      "partitionKey": "Test",
      "rowKey": "{queueTrigger}",
      "connection": "MyStorageConnectionAppSetting",
      "direction": "in"
    }
  ],
  "disabled": false
}

配置部分解释了这些属性。

C# 脚本代码如下所示:

public static void Run(string myQueueItem, Person personEntity, ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    log.LogInformation($"Name in Person entity: {personEntity.Name}");
}

public class Person
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Name { get; set; }
}

CloudTable

IQueryable 在版本 2.x 及更高版本的 Functions 运行时中不受支持。 一种替代方法是使用 CloudTable 方法参数通过 Azure 存储 SDK 来读取表。 下面是一个查询 Azure Functions 日志表的函数示例:

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */1 * * * *"
    },
    {
      "name": "cloudTable",
      "type": "table",
      "connection": "AzureWebJobsStorage",
      "tableName": "AzureWebJobsHostLogscommon",
      "direction": "in"
    }
  ],
  "disabled": false
}
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

public static async Task Run(TimerInfo myTimer, CloudTable cloudTable, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

    TableQuery<LogEntity> rangeQuery = new TableQuery<LogEntity>().Where(
    TableQuery.CombineFilters(
        TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, 
            "FD2"),
        TableOperators.And,
        TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThan, 
            "a")));

    // Execute the query and loop through the results
    foreach (LogEntity entity in 
    await cloudTable.ExecuteQuerySegmentedAsync(rangeQuery, null))
    {
        log.LogInformation(
            $"{entity.PartitionKey}\t{entity.RowKey}\t{entity.Timestamp}\t{entity.OriginalName}");
    }
}

public class LogEntity : TableEntity
{
    public string OriginalName { get; set; }
}

有关如何使用 CloudTable 的详细信息,请参阅 Azure 表存储入门。

如果在尝试绑定到 CloudTable 时出现错误消息,请确保引用正确的存储 SDK 版本。

IQueryable

以下示例演示 function.json 文件中的一个表输入绑定以及使用该绑定的 C# 脚本代码。 该函数读取队列消息中指定的分区键的实体。

function.json 文件如下所示:

{
  "bindings": [
    {
      "queueName": "myqueue-items",
      "connection": "MyStorageConnectionAppSetting",
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in"
    },
    {
      "name": "tableBinding",
      "type": "table",
      "connection": "MyStorageConnectionAppSetting",
      "tableName": "Person",
      "direction": "in"
    }
  ],
  "disabled": false
}

配置部分解释了这些属性。

C# 脚本代码添加对 Azure 存储 SDK 的引用,以便实体类型可以从 TableEntity 派生。

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.Extensions.Logging;

public static void Run(string myQueueItem, IQueryable<Person> tableBinding, ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    foreach (Person person in tableBinding.Where(p => p.PartitionKey == myQueueItem).ToList())
    {
        log.LogInformation($"Name: {person.Name}");
    }
}

public class Person : TableEntity
{
    public string Name { get; set; }
}

Java

以下示例显示了一个 HTTP 触发函数,该函数返回表存储中指定分区中的 person 对象列表。 在此示例中,分区键将从 http 路由中提取,表名和连接将从函数设置中提取。

public class Person {
    private String PartitionKey;
    private String RowKey;
    private String Name;

    public String getPartitionKey() { return this.PartitionKey; }
    public void setPartitionKey(String key) { this.PartitionKey = key; }
    public String getRowKey() { return this.RowKey; }
    public void setRowKey(String key) { this.RowKey = key; }
    public String getName() { return this.Name; }
    public void setName(String name) { this.Name = name; }
}

@FunctionName("getPersonsByPartitionKey")
public Person[] get(
        @HttpTrigger(name = "getPersons", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.FUNCTION, route="persons/{partitionKey}") HttpRequestMessage<Optional<String>> request,
        @BindingName("partitionKey") String partitionKey,
        @TableInput(name="persons", partitionKey="{partitionKey}", tableName="%MyTableName%", connection="MyConnectionString") Person[] persons,
        final ExecutionContext context) {

    context.getLogger().info("Got query for person related to persons with partition key: " + partitionKey);

    return persons;
}

TableInput 注释还可以从请求的 json 正文中提取绑定,如下面的示例所示。

@FunctionName("GetPersonsByKeysFromRequest")
public HttpResponseMessage get(
        @HttpTrigger(name = "getPerson", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.FUNCTION, route="query") HttpRequestMessage<Optional<String>> request,
        @TableInput(name="persons", partitionKey="{partitionKey}", rowKey = "{rowKey}", tableName="%MyTableName%", connection="MyConnectionString") Person person,
        final ExecutionContext context) {

    if (person == null) {
        return request.createResponseBuilder(HttpStatus.NOT_FOUND)
                    .body("Person not found.")
                    .build();
    }

    return request.createResponseBuilder(HttpStatus.OK)
                    .header("Content-Type", "application/json")
                    .body(person)
                    .build();
}

下面的示例使用筛选器在 Azure 表中查询具有特定名称的人员,并将可能的匹配项数目限制为10个结果。

@FunctionName("getPersonsByName")
public Person[] get(
        @HttpTrigger(name = "getPersons", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.FUNCTION, route="filter/{name}") HttpRequestMessage<Optional<String>> request,
        @BindingName("name") String name,
        @TableInput(name="persons", filter="Name eq '{name}'", take = "10", tableName="%MyTableName%", connection="MyConnectionString") Person[] persons,
        final ExecutionContext context) {

    context.getLogger().info("Got query for person related to persons with name: " + name);

    return persons;
}

JavaScript

以下示例演示 function.json 文件中的一个表输入绑定以及使用该绑定的 JavaScript 脚本代码。 该函数使用队列触发器来读取单个表行。

function.json 文件指定 partitionKey 和 rowKey。 rowKey 值“{queueTrigger}”指示行键来自队列消息字符串。

{
  "bindings": [
    {
      "queueName": "myqueue-items",
      "connection": "MyStorageConnectionAppSetting",
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in"
    },
    {
      "name": "personEntity",
      "type": "table",
      "tableName": "Person",
      "partitionKey": "Test",
      "rowKey": "{queueTrigger}",
      "connection": "MyStorageConnectionAppSetting",
      "direction": "in"
    }
  ],
  "disabled": false
}

配置部分解释了这些属性。

JavaScript 代码如下所示:

module.exports = function (context, myQueueItem) {
    context.log('Node.js queue trigger function processed work item', myQueueItem);
    context.log('Person entity name: ' + context.bindings.personEntity.Name);
    context.done();
};

PowerShell

下面的函数使用 queue 触发器来读取单个表行作为函数的输入。

在此示例中,绑定配置为表的显式值指定 partitionKey ,并使用表达式传递到 rowKey 。 rowKey表达式 {queueTrigger} 指示行键来自队列消息字符串。

function.js上的 绑定配置:

{
  "bindings": [
    {
      "queueName": "myqueue-items",
      "connection": "MyStorageConnectionAppSetting",
      "name": "MyQueueItem",
      "type": "queueTrigger",
      "direction": "in"
    },
    {
      "name": "PersonEntity",
      "type": "table",
      "tableName": "Person",
      "partitionKey": "Test",
      "rowKey": "{queueTrigger}",
      "connection": "MyStorageConnectionAppSetting",
      "direction": "in"
    }
  ],
  "disabled": false
}

run.ps1 中的 PowerShell 代码:

param($MyQueueItem, $PersonEntity, $TriggerMetadata)
Write-Host "PowerShell queue trigger function processed work item: $MyQueueItem"
Write-Host "Person entity name: $($PersonEntity.Name)"

Python

下面的函数使用 queue 触发器来读取单个表行作为函数的输入。

在此示例中,绑定配置为表的显式值指定 partitionKey ,并使用表达式传递到 rowKey 。 rowKey表达式 {id} 指示行键来自队列消息字符串。

文件 function.js上 的绑定配置:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "messageJSON",
      "type": "table",
      "tableName": "messages",
      "partitionKey": "message",
      "rowKey": "{id}",
      "connection": "AzureWebJobsStorage",
      "direction": "in"
    },
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ],
      "route": "messages/{id}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ],
  "disabled": false
}

_ _ _ _ Py 文件中的 Python 代码:

import json

import azure.functions as func

def main(req: func.HttpRequest, messageJSON) -> func.HttpResponse:

    message = json.loads(messageJSON)
    return func.HttpResponse(f"Table row: {messageJSON}")

特性和注释

C#

在 C# 类库中,请使用以下属性来配置表输入绑定:

  • TableAttribute

    该特性的构造函数采用表名称、分区键和行键。 可对函数的 out 参数或返回值使用该特性,如以下示例中所示:

    [FunctionName("TableInput")]
    public static void Run(
        [QueueTrigger("table-items")] string input, 
        [Table("MyTable", "Http", "{queueTrigger}")] MyPoco poco, 
        ILogger log)
    {
        ...
    }
    

    可以设置 Connection 属性来指定要使用的存储帐户,如以下示例中所示:

    [FunctionName("TableInput")]
    public static void Run(
        [QueueTrigger("table-items")] string input, 
        [Table("MyTable", "Http", "{queueTrigger}", Connection = "StorageConnectionAppSetting")] MyPoco poco, 
        ILogger log)
    {
        ...
    }
    

    有关完整示例,请参阅 C# 示例。

  • StorageAccountAttribute

    提供另一种方式来指定要使用的存储帐户。 构造函数采用包含存储连接字符串的应用设置的名称。 可以在参数、方法或类级别应用该特性。 以下示例演示类级别和方法级别:

    [StorageAccount("ClassLevelStorageAppSetting")]
    public static class AzureFunctions
    {
        [FunctionName("TableInput")]
        [StorageAccount("FunctionLevelStorageAppSetting")]
        public static void Run( //...
    {
        ...
    }
    

要使用的存储帐户按以下顺序确定:

  • Table 特性的 Connection 属性。
  • 作为 Table 特性应用到同一参数的 StorageAccount 特性。
  • 应用到函数的 StorageAccount 特性。
  • 应用到类的 StorageAccount 特性。
  • 函数应用的默认存储帐户(“AzureWebJobsStorage”应用设置)。

C# 脚本

C# 脚本不支持特性。

Java

在 Java 函数运行时库中,对其值将来自表存储的参数使用 @TableInput 注释。 可以将此注释与本机 Java 类型、POJO 或使用了 Optional<T> 的可为 null 的值一起使用。

JavaScript

JavaScript 不支持特性。

PowerShell

PowerShell 不支持特性。

Python

Python 不支持特性。


配置

下表解释了在 function.json 文件和 Table 特性中设置的绑定配置属性。

function.json 属性 Attribute 属性 说明
type 不适用 必须设置为 table。 在 Azure 门户中创建绑定时,会自动设置此属性。
direction 不适用 必须设置为 in。 在 Azure 门户中创建绑定时,会自动设置此属性。
name 不适用 表示函数代码中的表或实体的变量的名称。
tableName TableName 表的名称。
partitionKey PartitionKey 可选。 要读取的表实体的分区键。 有关如何使用此属性的指导,请参阅用法部分。
rowKey RowKey 可选。 要读取的表实体的行键。 有关如何使用此属性的指导,请参阅用法部分。
take Take 可选。 要在 JavaScript 中读取的最大实体数。 有关如何使用此属性的指导,请参阅用法部分。
filter Filter 可选。 JavaScript 中的表输入的 OData 筛选表达式。 有关如何使用此属性的指导,请参阅用法部分。
连接 Connection 包含要用于此绑定的存储连接字符串的应用设置的名称。 设置可以是带有“AzureWebJobs”前缀的应用设置的名称,也可以是连接字符串的名称。 例如,如果设置名称是“AzureWebJobsMyStorage”,则可以在此处指定“MyStorage”。 Functions 运行时将自动查找名为“AzureWebJobsMyStorage”的应用设置。 如果将 connection 留空,函数运行时将使用名为 AzureWebJobsStorage 的应用设置中的默认存储连接字符串。

在本地进行开发时,应用设置将取 local.settings.json 文件的值。

使用情况

C#

  • 读取一行

    设置 partitionKey 和 rowKey。 使用方法参数 T <paramName> 访问表数据。 在 C# 脚本中,paramName 是在 function.json 的 name 属性中指定的值。 T 通常是实现 ITableEntity 或派生自 TableEntity 的类型。 此方案中不使用 filter 和 take 属性。

  • 读取一行或多行

    使用方法参数 IQueryable<T> <paramName> 访问表数据。 在 C# 脚本中,paramName 是在 function.json 的 name 属性中指定的值。 T 必须是实现 ITableEntity 或派生自 TableEntity 的类型。 可以使用 IQueryable 方法执行任何所需的筛选。 此方案中不使用 partitionKey、rowKey、filter 和 take 属性。

    NOTE Functions v2 运行时不支持 IQueryable。 一种替代方法是使用 CloudTable paramName 方法参数通过 Azure 存储 SDK 来读取表。 如果在尝试绑定到 CloudTable 时出现错误消息,请确保引用正确的存储 SDK 版本。

C# 脚本

  • 读取一行

    设置 partitionKey 和 rowKey。 使用方法参数 T <paramName> 访问表数据。 在 C# 脚本中,paramName 是在 function.json 的 name 属性中指定的值。 T 通常是实现 ITableEntity 或派生自 TableEntity 的类型。 此方案中不使用 filter 和 take 属性。

  • 读取一行或多行

    使用方法参数 IQueryable<T> <paramName> 访问表数据。 在 C# 脚本中,paramName 是在 function.json 的 name 属性中指定的值。 T 必须是实现 ITableEntity 或派生自 TableEntity 的类型。 可以使用 IQueryable 方法执行任何所需的筛选。 此方案中不使用 partitionKey、rowKey、filter 和 take 属性。

    NOTE Functions v2 运行时不支持 IQueryable。 一种替代方法是使用 CloudTable paramName 方法参数通过 Azure 存储 SDK 来读取表。 如果在尝试绑定到 CloudTable 时出现错误消息,请确保引用正确的存储 SDK 版本。

Java

使用 TableInput 特性可以访问触发了函数的表行。

JavaScript

设置 filter 和 take 属性。 不要设置 partitionKey 或 rowKey。 使用 context.bindings.<BINDING_NAME> 访问输入一个或多个输入表实体。 反序列化的对象具有 RowKey 和 PartitionKey 属性。

PowerShell

数据将传递到 name function.json 文件中的键指定的输入参数。 指定 partitionKey 和可 rowKey 筛选特定记录。 有关更多详细信息,请参阅 PowerShell 示例 。

Python

表数据作为 JSON 字符串传递给函数。 通过调用来反序列化消息, json.loads 如输入 示例中所示。


后续步骤

历史资料。此文对应 2022-08-12 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者(原始作者元数据 craigshoemaker)。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体、发布器占位标签和导航标记。

资料来源与适用范围

资料来源
Microsoft / Azure 中文运维文档历史快照
适用范围
历史中文快照 2022-08-12;详见原文产品版本;文档日期 09/03/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。