简体   繁体   English

azure function powershell从存储队列中读取并写入存储表

[英]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.

相关问题 如何使用HTTP触发器PowerShell Azure函数从Azure表存储中读取数据? - How to read from Azure Table Storage with a HTTP Trigger PowerShell Azure Function? 使用 PowerShell 脚本从 Azure Blob 存储读取 JSON 文件并写回 Blob 存储中的另一个文件 - Read JSON file from Azure Blob Storage using PowerShell script and write back to another file in blob storage Azure powershell function 如何将消息添加到存储帐户队列 - Azure powershell function how to add message to storage account queue 写入 Azure 存储 Blob (PowerShell) - Write to Azure Storage Blob (PowerShell) Powershell Azure Function 将 Excel 文件写入 Azure Blob 存储 - Powershell Azure Function to write an Excel file to Azure Blob Storage Azure Function - Powershell 和 Z3A580F142203677F1F0BC308INTSERTREPLACEZ5 存储表 - Azure Function - Powershell and Azure storage table (INSERT OR REPLACE) Azure Functions PowerShell - 从 Azure 存储表访问时间戳值 - Azure Functions PowerShell - Accessing Timestamp value from Azure Storage Table 从Automation PowerShell中的Azure Blob存储读取XML - Read XML from Azure Blob storage in Automation PowerShell 从Azure Powershell访问存储 - Acces storage from Azure Powershell 通过Powershell将实体从XML文件上传到Azure存储表 - Uploading entities to azure storage table from XML file through powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM