我正在尝试根据 Azure 函数更新 Azure 表中的一行。我看到 Table 绑定可以处理具有 Add 方法的 ICollector,该方法将添加一行。我还看到您使用 IQueryable 来读取数据。
您如何更新数据中的特定行?
我在 WebJobs 中看到了与 InsertOrReplace 相关的东西,它是 TableOperations 的一种方法,但我不知道它是否或如何发挥作用以及如何将它与 Azure Functions 一起使用。
我正在尝试根据 Azure 函数更新 Azure 表中的一行。我看到 Table 绑定可以处理具有 Add 方法的 ICollector,该方法将添加一行。我还看到您使用 IQueryable 来读取数据。
您如何更新数据中的特定行?
我在 WebJobs 中看到了与 InsertOrReplace 相关的东西,它是 TableOperations 的一种方法,但我不知道它是否或如何发挥作用以及如何将它与 Azure Functions 一起使用。
以下是您可以执行此操作的一种方法。在我们的下一个版本中,这些步骤将变得更加容易,但现在您需要手动引入 Azure 存储 SDK。
首先,按照此帮助页面的“包管理”部分中的步骤来拉入Azure 存储 SDK。您将上传一个project.json
看起来像这样的函数文件夹:
{
"frameworks": {
"net46":{
"dependencies": {
"WindowsAzure.Storage": "7.0.0"
}
}
}
}
注意:在下一个版本中,我们将自动包含 Azure 存储 SDK,因此您可以直接在代码中使用它。拉入包后,您可以在“集成”选项卡选项卡“高级编辑器”中输入如下函数元数据:
{
"bindings": [
{
"name": "input",
"type": "manualTrigger",
"direction": "in"
},
{
"name": "table",
"type": "table",
"tableName": "test",
"connection": "<your connection>",
"direction": "in"
}
]
}
下面是相应的代码。我们绑定到一个CloudTable
允许我们读/写实体的这里:
#r "Microsoft.WindowsAzure.Storage"
using System;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
public static void Run(string input, CloudTable table, TraceWriter log)
{
TableOperation operation = TableOperation.Retrieve<Person>("AAA", "001");
TableResult result = table.Execute(operation);
Person person = (Person)result.Result;
log.Verbose($"{person.Name} is {person.Status}");
person.Status = input;
operation = TableOperation.Replace(person);
table.Execute(operation);
}
public class Person : TableEntity
{
public string Name { get;set; }
public string Status { get;set; }
}
我在此示例中使用了 ManualTrigger,但表绑定将与您拥有的任何触发器一起使用。通过上面的设置,我可以在门户的运行输入框中输入一个值并点击运行。该函数将查询实体,输出其当前值,然后使用我的输入进行更新。
其他排列是可能的。例如,如果您有来自另一个绑定参数的实体实例,则可以以类似的方式使用 CloudTable 来更新它。
使用今天的 bindings,您可以将ETag
属性设置*
为执行 upsert 的值:
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log,
[Table("test")] IAsyncCollector<PocoClass> table)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
if (name == null)
return new BadRequestResult();
await table.AddAsync(new PocoClass { Name = name });
return new OkObjectResult($"Hello, {name}");
}
public sealed class PocoClass
{
public string PartitionKey { get; } = "partition";
public string RowKey { get; } = "row";
public string ETag { get; } = "*";
public string Name { get; set; }
}
使用当前版本的 Functions,我能够使行更新与声明性绑定一起工作。这是一个 HTTP 触发器示例,它在 Azure 表行中增加一个数字。
function.json
:
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"route": "HttpTriggerTableUpdate/{partition}/{rowkey}"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "table",
"name": "inputEntity",
"tableName": "SOTrial",
"partitionKey": "{partition}",
"rowKey": "{rowkey}",
"connection": "my_STORAGE",
"direction": "in"
},
{
"type": "table",
"name": "outputEntity",
"tableName": "SOTrial",
"partitionKey": "{partition}",
"rowKey": "{rowkey}",
"connection": "my_STORAGE",
"direction": "out"
}
],
"disabled": false
}
C#函数:
#r "Microsoft.WindowsAzure.Storage"
using System;
using System.Net;
using Microsoft.WindowsAzure.Storage.Table;
public class Entity : TableEntity
{
public int Number {get; set;}
}
public static HttpResponseMessage Run(HttpRequestMessage req, string partition,
string rowkey, Entity inputEntity, out Entity outputEntity)
{
if (inputEntity == null)
outputEntity = new Entity { PartitionKey = partition, RowKey = rowkey, Number = 1};
else
{
outputEntity = inputEntity;
outputEntity.Number += 1;
}
return req.CreateResponse(HttpStatusCode.OK, $"Done, Number = {outputEntity.Number}");
}