2016-12-05 95 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