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

时间:2017-07-07 19:21:19

标签: azure-powershell azure-stream-analytics azure-iot-hub

我们有一个Stream Analytics作业,它具有到IOT Hub Operations Monitoring端点的输入映射。我们最初在Azure门户上定义了我们的工作。它在创建/更新时工作正常。

我们在多个" Azure环境中使用作业逻辑"并且现在将其保留在源代码管理中。我们使用Visual Studio Stream Analytics Project类型来管理源代码。

我们正在使用New-StreamAnalyticsJob Powershell命令将我们的工作部署到不同的环境中。

但是,每次部署时,生成的Stream Analytics作业输入都指向IOT Hub的Messaging端点,而不是Operations Monitoring端点。

我们可以输入输入的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" } } ],

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

2 个答案:

答案 0 :(得分:1)

我注意到Azure门户调用的端点与此处指示的端点不同:https://docs.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是一种禁止太多的身份验证,无法尝试复制/模拟。

更好的选择是通过this tutorial来创建控制台应用程序,并在使用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);

答案 1 :(得分:0)

来自@DaveMontgomery的建议很好,但事实并非如此。

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

根问题原来是Azure Powershell Cmdlet(包括版本4.1.x)使用旧版本的Microsoft.Azure.Management.StreamAnalytics程序集,即1.02.0的{​​{1}}版本在几个月前发布,根据我的理解,该版本包含Microsoft.Azure.Management.StreamAnalytics元素到endpoint JSON结构。

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

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

通过此更改,我们可以在Microsoft.Azure.Management.StreamAnalytics v2.0 / endpoint / Properties IOT输入中添加DataSource元素,并且已部署的流分析作业包含物联网输入正确缝制到Properties端点。