New-StreamAnalyticsJob 无法为 IOT 中心创建操作监视输入



我们有一个流分析作业,该作业具有到 IOT 中心操作监视终结点的输入映射。 我们最初在 Azure 门户上定义了作业。 创建/更新后,它可以正常工作。

我们在多个"Azure 环境"中使用作业逻辑,现在将其保留在源代码管理中。 我们使用 Visual Studio 流分析项目类型来管理源代码。

我们使用New-StreamAnalyticsJobPowershell 命令将作业部署到不同的环境中。

但是,每次部署时,生成的流分析作业的输入都会指向 IOT 中心的消息传送终结点,而不是操作监视终结点。

我们是否可以在输入的 JSON 文件中输入一些内容来表示终端节点类型? 下面是 cmdlet 的 JSON 输入的Input内容:

"Inputs": [{
"Name": "IOT-Hub-Monitoring-By-Consumer-Group",
"Properties": {
"DataSource": {
"Properties": {
"ConsumerGroupName": "theConsumerGroup",
"IotHubNamespace": "theIotNamespace",
"SharedAccessPolicyKey": null,
"SharedAccessPolicyName": "iothubowner"
},
"Type": "Microsoft.Devices/IotHubs"
},
"Serialization": {
"Properties": {
"Encoding": "UTF8",
"Format": "LineSeparated"
},
"Type": "Json"
},
"Type": "Stream"
}
},
{
"Name": "IOT-Hub-Messaging-By-Consumer-Group",
"Properties": {
"DataSource": {
"Properties": {
"ConsumerGroupName": "anotherConsumerGroup",
"IotHubNamespace": "theIotNamespace",
"SharedAccessPolicyKey": null,
"SharedAccessPolicyName": "iothubowner"
},
"Type": "Microsoft.Devices/IotHubs"
},
"Serialization": {
"Properties": {
"Encoding": "UTF8",
"Format": "LineSeparated"
},
"Type": "Json"
},
"Type": "Stream"
}
}
]

IotHubProperties中是否有我们没有表达的endpoint元素? 它记录在某处吗?

我注意到 Azure 门户调用的终结点与此处指示的终结点不同:https://learn.microsoft.com/en-us/rest/api/streamanalytics/stream-analytics-definition

它使用 https://main.streamanalytics.ext.azure.com/api 下的终结点。 例如 GET/api/Jobs/GetStreamingJob?subscriptionId={guid}&resourceGroupName=MyRG&jobName=MyJobName

您会在结果中注意到 JSON:

{
"properties": {
"inputs": {
{
"properties": {
"datasource": {
"inputIotHubSource": {
"iotHubNamespace":"HeliosIOTHubDev",
"sharedAccessPolicyName":"iothubowner",
"sharedAccessPolicyKey":null,
---> "endpoint":"messages/events",  <---
"consumerGroupName":"devicehealthmonitoring"
}

对于操作监视,您将看到"endpoint":"messages/operationsMonitoringEvents"

他们似乎将"保存输入"实现为PATCH /api/Inputs/PatchInput?...,它采用类似构造的 JSON,具有相同的 2 个值用于endpoint。 您是否能够以某种方式使用该端点? 即像往常一样呼叫New-AzureRmStreamAnalyticsJob然后Invoke-WebRequest -Method Patch -Uri ...

--编辑--

Invoke-WebRequest是不行的 - 太多的身份验证无法尝试复制/模拟。

更好的选择是完成本教程,以创建控制台应用程序,并在使用 Powershell 脚本部署后设置终结点。

这样的东西应该可以工作(尽管绝对没有错误/空检查(:

string tenantId = "...";        //Tenant Id Guid
string subscriptionId = "...";  //Subcription Id Guid
string rgName = "...";          //Name of Resource Group
string jobName = "...";         //Name of Stream Analytics Job
string inputName = "...";       //Name-of-Input-requiring-operations-monitoring
string accesskey = "...";       //Shared Access Key for the IoT Hub
var login = new ServicePrincipalLoginInformation();
login.ClientId = "...";         //Client / Application Id for AD Service Principal (from tutorial)
login.ClientSecret = "...";     //Password for AD Service Principal (from tutorial)
var environment = new AzureEnvironment
{
AuthenticationEndpoint = "https://login.windows.net/",
GraphEndpoint = "https://graph.windows.net/",
ManagementEnpoint = "https://management.core.windows.net/",
ResourceManagerEndpoint = "https://management.azure.com/",
};
var credentials = new AzureCredentials(login, tenantId, environment)
.WithDefaultSubscription(subscriptionId);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
var client = new StreamAnalyticsManagementClient(credentials);
client.SubscriptionId = azure.SubscriptionId;
var job = client.StreamingJobs.List(expand: "inputs").Where(j => j.Name == jobName).FirstOrDefault();
var input = job.Inputs.Where(i => i.Name == inputName).FirstOrDefault();
var props = input.Properties as StreamInputProperties;
var ds = props.Datasource as IoTHubStreamInputDataSource;
ds.Endpoint = "messages/operationsMonitoringEvents";
ds.SharedAccessPolicyKey = accesskey;
client.Inputs.CreateOrReplace(input, rgName, jobName, inputName);

@DaveMontgomery的建议很好,但结果证明是不需要的。

简单的 CMDLET 升级解决了该问题。

根本问题原来是 Azure Powershell Cmdlet(包括版本4.1.x(使用的是旧版本的Microsoft.Azure.Management.StreamAnalytics程序集,即1.0Microsoft.Azure.Management.StreamAnalytics的第2.0版在几个月前发布,据我了解,该版本包括向InputsJSON 结构添加endpoint元素。

新的 CMDLET 版本记录在此处:https://github.com/Azure/azure-powershell/releases/tag/v4.2.0-July2017。 该版本的提交包括 https://github.com/Azure/azure-powershell/commit/0c00632aa8f767e58077e966c04bb6fc505da1ef,它升级到Microsoft.Azure.Management.StreamAnalytics v2.0

请注意,这是一个喙的变化,因为JSON从PascalCase更改为camelCase。

有了这个变化,我们可以向Properties/DataSource/PropertiesIOT 输入添加一个endpoint元素,并且部署的流分析作业包含一个正确缝制到operationsMonitoring终结点的 IOT 输入。

相关内容

最新更新