I am trying to update a row in an Azure table based on an Azure Function. I see that the Table bindings can handle an ICollector which has an Add method which will add a row. I also see that you use an IQueryable to read the data.
How do you go about updating a specific row in the data?
I saw in WebJobs something related to InsertOrReplace which is a method of TableOperations but I don't know if or how that comes into play and how to use it with Azure Functions.
Bindings are optional and a function might have one or multiple input and/or output bindings. Triggers and bindings let you avoid hardcoding access to other services. Your function receives data (for example, the content of a queue message) in function parameters.
A binding is a connection to data within your function. Bindings are optional and come in the form of input and output bindings. An input binding is the data that your function receives. An output binding is the data that your function sends.
To update an entity, we use the Replace table operation. The first step is to retrieve the entity from the table and change the values to the required properties and perform the Replace operation. //Retrieve the storage account from the connection string.
Azure Table storage is a service that stores non-relational structured data (also known as structured NoSQL data) in the cloud, providing a key/attribute store with a schemaless design. Because Table storage is schemaless, it's easy to adapt your data as the needs of your application evolve.
Following is one way you can do this. These steps will get easier with our next release, but for now you need to manually bring in the Azure Storage SDK.
First, follow the steps in the "Package Management" section of this help page to pull in the Azure Storage SDK. You'll be uploading a project.json
that looks like this to your function folder:
{
"frameworks": {
"net46":{
"dependencies": {
"WindowsAzure.Storage": "7.0.0"
}
}
}
}
Note: In the next release we'll automatically include the Azure Storage SDK so you can just use it directly in your code. After you've pulled in the package, you can enter function metadata like the following in the Integrate tab tab Advanced Editor:
{
"bindings": [
{
"name": "input",
"type": "manualTrigger",
"direction": "in"
},
{
"name": "table",
"type": "table",
"tableName": "test",
"connection": "<your connection>",
"direction": "in"
}
]
}
And below is the corresponding code. We're binding to a CloudTable
here which lets us read/write entities:
#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; }
}
I used a ManualTrigger for this example, but the table binding will work with whatever trigger you have. With the above set up, I can enter a value in the Run input box of the portal and hit run. The function will query the entity, output its current values, then update using my input.
Other permutations are possible. For example, if you have an entity instance from another binding parameter, you can use the CloudTable in a similar way to update it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With