使用 Azure 表存储输出绑定读取将实体写入 Azure 存储帐户中的表。Use an Azure Table storage output binding to write entities to a table in an Azure Storage account.
备注
此输出绑定不支持更新现有实体。This output binding does not support updating existing entities.请使用 Azure 存储 SDK 中的 TableOperation.Replace 操作来更新现有实体。Use the TableOperation.Replace operation from the Azure Storage SDK to update an existing entity.
以下示例演示使用 HTTP 触发器写入单个表行的 C# 函数。The following example shows a C# function that uses an HTTP trigger to write a single table row.
public class TableStorage
{
public class MyPoco
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Text { get; set; }
}
[FunctionName("TableOutput")]
[return: Table("MyTable")]
public static MyPoco TableOutput([HttpTrigger] dynamic input, ILogger log)
{
log.LogInformation($"C# http trigger function processed: {input.Text}");
return new MyPoco { PartitionKey = "Http", RowKey = Guid.NewGuid().ToString(), Text = input.Text };
}
}
以下示例演示 function.json 文件中的一个表输出绑定以及使用该绑定的 C# 脚本代码。The following example shows a table output binding in a function.json file and C# script code that uses the binding.该函数写入多个表实体。The function writes multiple table entities.
function.json 文件如下所示:Here's the function.json file:
配置部分解释了这些属性。The configuration section explains these properties.
C# 脚本代码如下所示:Here's the C# script code:
public static void Run(string input, ICollector<Person> tableBinding, ILogger log)
{
for (int i = 1; i < 10; i++)
{
log.LogInformation($"Adding Person entity {i}");
tableBinding.Add(
new Person() {
PartitionKey = "Test",
RowKey = i.ToString(),
Name = "Name" + i.ToString() }
);
}
}
public class Person
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Name { get; set; }
}
以下示例演示 function.json 文件中的一个表输出绑定以及使用该绑定的 JavaScript 函数。The following example shows a table output binding in a function.json file and a JavaScript function that uses the binding.该函数写入多个表实体。The function writes multiple table entities.
function.json 文件如下所示:Here's the function.json file:
该特性的构造函数采用表名称。The attribute's constructor takes the table name.可对函数的 out 参数或返回值使用该特性,如以下示例中所示:The attribute can be used on an out parameter or on the return value of the function, as shown in the following example:
有关完整示例,请参阅 C# 示例。For a complete example, see the C# example.
可以使用 StorageAccount 特性在类、方法或参数级别指定存储帐户。You can use the StorageAccount attribute to specify the storage account at class, method, or parameter level.有关详细信息,请参阅输入 - 特性。For more information, see Input - attributes.
C# 脚本不支持特性。Attributes are not supported by C# Script.
JavaScript 不支持特性。Attributes are not supported by JavaScript.
Python 不支持特性。Attributes are not supported by Python.
在 Java functions runtime library中,对参数使用 TableOutput 注释以将值写入到表存储中。In the Java functions runtime library, use the TableOutput annotation on parameters to write values into table storage.
下表解释了在 function.json 文件和 Table 特性中设置的绑定配置属性。The following table explains the binding configuration properties that you set in the function.json file and the Table attribute.
function.json 属性function.json property
Attribute 属性Attribute property
说明Description
typetype
不适用n/a
必须设置为 table。Must be set to table.在 Azure 门户中创建绑定时,会自动设置此属性。This property is set automatically when you create the binding in the Azure portal.
directiondirection
不适用n/a
必须设置为 out。Must be set to out.在 Azure 门户中创建绑定时,会自动设置此属性。This property is set automatically when you create the binding in the Azure portal.
namename
不适用n/a
在函数代码中使用的、表示表或实体的变量名称。The variable name used in function code that represents the table or entity.设置为 $return 可引用函数返回值。Set to $return to reference the function return value.
tableNametableName
TableNameTableName
表的名称。The name of the table.
partitionKeypartitionKey
PartitionKeyPartitionKey
要写入的表实体的分区键。The partition key of the table entity to write.有关如何使用此属性的指导,请参阅用法部分。See the usage section for guidance on how to use this property.
rowKeyrowKey
RowKeyRowKey
要写入的表实体的行键。The row key of the table entity to write.有关如何使用此属性的指导,请参阅用法部分。See the usage section for guidance on how to use this property.
连接 connection
ConnectionConnection
包含要用于此绑定的存储连接字符串的应用设置的名称。The name of an app setting that contains the Storage connection string to use for this binding.如果应用设置名称以“AzureWebJobs”开始,则只能在此处指定该名称的余下部分。If the app setting name begins with "AzureWebJobs", you can specify only the remainder of the name here.例如,如果将 connection 设置为“MyStorage”,Functions 运行时将会查找名为“MyStorage”的应用设置。For example, if you set connection to "MyStorage", the Functions runtime looks for an app setting that is named "MyStorage".如果将 connection 留空,函数运行时将使用名为 AzureWebJobsStorage 的应用设置中的默认存储连接字符串。If you leave connection empty, the Functions runtime uses the default Storage connection string in the app setting that is named AzureWebJobsStorage.
可以使用方法参数 ICollector<T> paramName 或 IAsyncCollector<T> paramName 访问输出表实体,其中 T 包括 PartitionKey 和 RowKey 属性。Access the output table entity by using a method parameter ICollector<T> paramName or IAsyncCollector<T> paramName where T includes the PartitionKey and RowKey properties.实现 ITableEntity 或继承 TableEntity 时通常会伴随使用这些属性。These properties are often accompanied by implementing ITableEntity or inheriting TableEntity.
另外,还可以使用 CloudTable 方法参数通过 Azure 存储 SDK 来写入到表。Alternatively you can use a CloudTable method parameter to write to the table by using the Azure Storage SDK.如果在尝试绑定到 CloudTable 时出现错误消息,请确保引用正确的存储 SDK 版本。If you try to bind to CloudTable and get an error message, make sure that you have a reference to the correct Storage SDK version.
可以使用方法参数 ICollector<T> paramName 或 IAsyncCollector<T> paramName 访问输出表实体,其中 T 包括 PartitionKey 和 RowKey 属性。Access the output table entity by using a method parameter ICollector<T> paramName or IAsyncCollector<T> paramName where T includes the PartitionKey and RowKey properties.实现 ITableEntity 或继承 TableEntity 时通常会伴随使用这些属性。These properties are often accompanied by implementing ITableEntity or inheriting TableEntity.paramName 值是在 function.json 的 name 属性中指定的。The paramName value is specified in the name property of function.json .
另外,还可以使用 CloudTable 方法参数通过 Azure 存储 SDK 来写入到表。Alternatively you can use a CloudTable method parameter to write to the table by using the Azure Storage SDK.如果在尝试绑定到 CloudTable 时出现错误消息,请确保引用正确的存储 SDK 版本。If you try to bind to CloudTable and get an error message, make sure that you have a reference to the correct Storage SDK version.
使用 context.bindings.<name> 访问输出事件,其中 <name> 是在 function.json 的 name 属性中指定的值。Access the output event by using context.bindings.<name> where <name> is the value specified in the name property of function.json .
有两个选项可用于使用 TableStorageOutput 注释从函数来输出表存储行:There are two options for outputting a Table storage row from a function by using the TableStorageOutput annotation:
返回值 :通过将注释应用于函数本身,函数的返回值将持久保存为表存储行。Return value : By applying the annotation to the function itself, the return value of the function is persisted as a Table storage row.
命令性 :若要显式设置消息值,请将注释应用于 OutputBinding<T> 类型的特定参数,其中 T 包括 PartitionKey 和 RowKey 属性。Imperative : To explicitly set the message value, apply the annotation to a specific parameter of the type OutputBinding<T>, where T includes the PartitionKey and RowKey properties.实现 ITableEntity 或继承 TableEntity 时通常会伴随使用这些属性。These properties are often accompanied by implementing ITableEntity or inheriting TableEntity.