2016-12-05 73 views
3

因此,我试图开发一个函数,它将从Azure存储队列读取数据并将其写入Azure存储表。我似乎无法找到任何相关的东西。我找到的代码读取队列:天青功能powershell从存储队列中读取并写入存储表

$in = Get-Content $triggerInput 
Write-Output "PowerShell script processed queue message '$in'" 

但有没有例子写的表,所以我不知道该怎么做。

回答

5

最近我做了同样的功能,你可以找到例子here。您需要功能QueueTrigger-PowerShell。 h

$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 

要控制写入数据的表,可以使用function.json。对我来说,行和分区键指定了有:

{ 
    "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" 
} 

这是我function.json,但原来它有分区和行硬编码到它的键值。现在,我使用PowerShell来生成这些(从另一个答案复制粘贴在这个线程):

PartitionKey = $requestBody.room 
RowKey = get-date -Format "yyyy-MM-dd H:m:s.ms" 
+0

谢谢,那正是我所需要的。不知道为什么我找不到它。辉煌! – ilivetoserve

+0

我在我们的repo [这里](https://github.com/Azure/azure-webjobs-sdk-templates/issues/375)上登录了一个问题,用于改进我们支持的所有各种绑定的所有语言的doc。语言/绑定组合的矩阵很大,但我们需要提供更好的指导。随时就这个问题分享任何关于我们如何改进这一点的建议。谢谢。 – mathewc

+0

我是否认为上述缺少PartitionKey和RowKey值?如果我没有指定这两个值,我就无法让它工作,我知道它也必须是独一无二的,[按照这里的注释](https://docs.microsoft.com/en-us/rest/API/storageservices/fileservices /理解,该表业务数据模型#partitionkey属性)。 – AndyHerb

2

PowerShell storage documentation内容详尽,为了写实体表存储,您需要提供一个独特的PartitionKey和RowKey值。因此,除非您在任何函数的外部管理RowKeys,否则我发现日期时间戳很有用。考虑到这一点,传入的JSON体是这样的:

{ 
    "room": "Boardroom", 
    "temp": "21.24" 
} 

送入你的PowerShell的功能(包括网络挂接和队列触发实例提供):

# 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 

这将导致一个新的实体(可被认为是数据库中的一行);假设您已经在函数中配置了Azure表存储输出对象并将其称为outputTable。

+0

我已经更新了我的答案,因此您可以更好地了解如何在没有PowerShell代码的情况下执行此操作 – likmoon