无法将 JSON 数据引入 Azure 事件中心

Unable to ingest JSON data into Azure Event Hub

我编写了以下 Powershell 脚本以从 API 端点 (https://data.melbourne.vic.gov.au/resource/vh2v-4nfs) 获取 JSON 数据,然后将此数据以 JSON 格式写入 Azure Event中心。我能够成功地从端点获取数据,但是数据没有被引入 Azure 事件中心。

谁能告诉我以下代码有什么问题:

$url = "https://data.melbourne.vic.gov.au/resource/vh2v-4nfs"
$apptoken = "k7lQcUCVFoROv7rQh9fSSXMkZ"

# Set header to accept JSON
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept","application/json")
$headers.Add("X-App-Token",$apptoken)

$results = Invoke-RestMethod -Uri $url -Method get -Headers $headers

$results


$method = "POST"
$URI = "https://YOURNS.servicebus.windows.net/eh-streetparking/messages"
$signature = "SharedAccessSignature sr=YOURNS.servicebus.windows.net%2feh-streetparking&sig=K6bfL1VjW9FUcL0B5xaI%3d&se=16722&skn=eh-sap-streetparking"

#$authInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$signature"))

# API headers
$headers = @{
            "Authorization"=$signature;
#            "Content-Type"="application/json;type=entry;charset=utf-8";
            "Content-Type"="application/json";
            }


# execute the Azure REST API

foreach ( $result in $results)
{
Invoke-RestMethod -Uri $URI -Method $method -Headers $headers -Body $result
}

您从 Invoke-RestMethod 得到的 return 结果实际上是一个 反序列化的 PowerShell 对象,而不是 JSON。它似乎也在某个时候删除了引号。

PSObject ($results) 看起来像这样:$x = @{account_id="12345"; username="12345"; is_locked="False"; employee_id="12345"; first_name="John"; middle_initial="Roger"; last_name="Doe"; full_name="John Roger Doe"}

您可以执行此操作以访问单个值:

$x.full_name

最后,跟随this syntax发送POST请求:

$Cred = Get-Credential
$Url = "https://server.contoso.com:8089/services/search/jobs/export"
$Body = @{
    search = "search index=_internal | reverse | table index,host,source,sourcetype,_raw"
    output_mode = "csv"
    earliest_time = "-2d@d"
    latest_time = "-1d@d"
}
Invoke-RestMethod -Method 'Post' -Uri $url -Credential $Cred -Body $body -OutFile output.csv