Azure函数表绑定:如何更新一行?
我想基于Azure函数更新Azure表中的一行。我发现表绑定可以处理一个ICollector,它有一个Add方法,它将添加一行。我也看到你使用IQueryable来读取数据。Azure函数表绑定:如何更新一行?
你如何去更新数据中的特定行?
我在WebJobs中看到了一些与InsertOrReplace相关的东西,这是TableOperations的一个方法,但我不知道它是如何起作用的以及如何将它用于Azure函数。
以下是您可以执行此操作的一种方法。使用我们的下一个版本,这些步骤会变得更容易,但现在您需要手动引入Azure存储SDK。
首先,按照this help page的“包管理”部分中的步骤操作,以获取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来更新它。
使用当前版本的函数,我能够使用声明性绑定进行行更新。这里有一个HTTP触发器的例子,它在Azure Table行中增加一个数字。
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}");
}
不幸的是,我一直无法使这种技术适用于节点。 –
我在https://github.com/Azure/azure-webjobs-sdk-script/issues/1663上报告了这个问题 –
这就是我所需要的。得到它的工作。谢谢 –
似乎自从最近更新以来没有工作。请参阅:https://social.msdn.microsoft.com/Forums/zh-CN/8f70fb3c-ad60-4cb4-be7a-a8b46c95ce8c/method-not-found-microsoftwindowsazurestoragetabletableoperationretrieve?forum=AzureFunctions –
请尝试更新* * WindowsAzure.Storage **从版本4.3到7.0。 – mathewc