[英]azure function powershell read from storage queue and write to storage table
So I'm trying to develop a function that would read data from an Azure Storage queue and write it to an Azure storage table. 所以我正在尝试开发一个函数,该函数将从Azure存储队列中读取数据并将其写入Azure存储表。 I can't seem to find anything relevant.
我似乎找不到任何相关的东西。 I found the code to read the queue:
我找到了读取队列的代码:
$in = Get-Content $triggerInput
Write-Output "PowerShell script processed queue message '$in'"
But there are no examples to write to the table, so I'm not sure how to do that. 但是没有例子写到表中,所以我不知道该怎么做。
Recently I did the same function and you can find the examples here . 最近我做了同样的功能,你可以在这里找到这些例子。 You need the function
QueueTrigger-PowerShell
. 您需要
QueueTrigger-PowerShell
功能。 hth 心连心
$json = Get-Content $triggerInput | ConvertFrom-Json
Write-Output "PowerShell script processed queue message '$json'"
$title = "PowerShell Table Entity for message {0}" -f $json.id
$entity = [PSObject]@{
Status = 0
Title = $title
}
$entity | ConvertTo-Json | Out-File -Encoding UTF8 $outputTable
To control to which table to write data you can use function.json. 要控制写入数据的表,可以使用function.json。 For me row and partition keys were specified there:
对我来说,那里指定了行和分区键:
{
"type": "table",
"name": "outputTable",
"tableName": "pancakeTable",
"connection": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
"direction": "out",
"partitionKey": "some value",
"rowKey": "I don't remember what was here but it was some form of variable (guid) generated from the request by Azure"
}
This is my function.json, but originally it had partition and row key values hardcoded into it. 这是我的function.json,但最初它将分区和行键值硬编码到其中。 Right now I'm using powershell to generate those (copy-pasted from another answer in this thread):
现在我正在使用powershell来生成那些(从此线程中的另一个答案复制粘贴):
PartitionKey = $requestBody.room
RowKey = get-date -Format "yyyy-MM-dd H:m:s.ms"
As detailed in the PowerShell storage documentation , in order to write entities to table storage you need to provide a unique PartitionKey and RowKey value. 正如PowerShell存储文档中所详述的,为了将实体写入表存储,您需要提供唯一的PartitionKey和RowKey值。 Because of this, unless you're managing RowKeys outside in whatever's calling your Function, I found a datetime stamp to be useful.
因此,除非您在任何调用函数的情况下管理RowKeys,否则我发现日期时间戳有用。 With this in mind, an incoming json body like this:
考虑到这一点,像这样的传入json体:
{
"room": "Boardroom",
"temp": "21.24"
}
feeds into your PowerShell function (both WebHook and Queue trigger examples provided): 提供给PowerShell函数(提供的WebHook和Queue触发器示例):
# WebHook example
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
# Queue example
$requestBody = Get-Content $triggerInput | ConvertFrom-Json
Write-Output "PowerShell message body '$requestBody'"
$entity = [PSObject]@{
PartitionKey = $requestBody.room
RowKey = get-date -Format "yyyy-MM-dd H:m:s.ms"
Temp = $requestBody.temp
}
$entity | ConvertTo-Json | Out-File -Encoding UTF8 $outputTable
This results in one new entity (can be thought of as a row in database terms); 这导致一个新实体(可以被认为是数据库术语中的一行); assuming you've configured an Azure Table Storage output object in the Function and called it outputTable.
假设您已在Function中配置了Azure Table Storage输出对象并将其命名为outputTable。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.