在 Powershell 中使用 POST Rest Api 调用在 Kusto 数据摄取方面需要帮助

问题描述 投票:0回答:1

作为 POC 的一部分,我需要使用 PowerShell 使用 RestAPI 将数据提取到 Kusto 表中。

参考Kusto Streaming Ingestion Doc

后,我想出了以下几行脚本

然而,脚本只是无限运行,什么都不做,永远不会完成。我不得不关闭 PowerShell 窗口并重新打开它。有人可以更正下面的 POST Rest Api 调用以使其工作。或者,如果我在 $authHeader 中更改任何内容,脚本将因 BadRequest(400) 错误而失败。

$Collection = Get-AzResource | select ResourceName,ResourceGroupName,ResourceType,Location,ResourceId,Tags | ConvertTo-Json

$accessToken = (Get-AzAccessToken -ResourceUrl "https://MyKustoClustername.westus2.kusto.windows.net").Token

$authHeader = @{
        'Authorization'='Bearer ' + $accessToken
        'Accept-Encoding'='deflate'
        'Content-Length'='8000'
        'Host'='MyKustoClustername.westus2.kusto.windows.net'
        }

$Uri = -join("https://MyKustoClustername.westus2.kusto.windows.net/v1/rest/ingest/MyDatabasename/VinnyRunBookTest?streamFormat=Json&mappingName=VinnyRunBookTestJsonMapping")
Invoke-RestMethod -Uri $Uri -Method POST -Headers $authHeader -Body $TestCollection
powershell azure-powershell azure-data-explorer azure-rest-api data-ingestion
1个回答
0
投票

想通了。 Kusto 不接受完美的 JSON 格式。它只接受 Unchained Serialized json 格式的有效载荷。我不太确定这种格式的实际名称是什么 :)

下面的代码有效。

$accessToken = (Get-AzAccessToken -ResourceUrl https://MyKustoClusterName.westus2.kusto.windows.net).Token

$authHeader = @{
        'Content-Type'='application/json'
        'Authorization'='Bearer ' + $accessToken
        'Host'='MyKustoClusterName.westus2.kusto.windows.net'
        }

$Collection = @'
{"ResourceName":"Test1","ResourceGroupName":"Test1","ResourceType":"Test1","Location":"Test1","ResourceId":"Test1","Tags":"Desktop"}
{"ResourceName":"Test2","ResourceGroupName":"Test2","ResourceType":"Test2","Location":"Test2","ResourceId":"Test2","Tags":"Desktop"}
'@
$Collection

$Uri = https://MyKustoClusterName.westus2.kusto.windows.net/v1/rest/ingest/MyDBName/VinnyRunBookTest?streamFormat=json
$Response = Invoke-RestMethod -Uri $Uri -Method POST -Headers $authHeader -Body $Collection -Verbose

© www.soinside.com 2019 - 2024. All rights reserved.