如何在Stream Analytics中读取IoT中心消息“应用程序属性”?

时间:2019-08-08 12:10:59

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

我已经丰富了基于Microsoft documentation的IoT中心消息并将其路由到内置端点,然后让Stream Analytics通过提供IoT中心作为输入来访问消息。

IoT中心消息丰富功能将丰富功能数据添加到消息的应用程序属性中,而不是主体本身,因此,要从Stream Analytics中获取丰富数据,我遇到了挑战-因为Stream Analytics的输出是Blob和它仅包含我发送到IoT中心的实际消息。

此处的扩展数据是指我基于设备双属性映射到IoT中心中注册的设备的某些数据(例如位置,身份等)。

我已经尝试过GetMetadataPropertyValueparsing JSON in Stream Analytics中提到的步骤,但是从直接从Stream Analytics获得“应用程序属性”方面算不上运气。

有人可以帮我弄清楚如何从Stream Analytics访问应用程序属性,或者至少指向正确的资源吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

在查询中尝试以下操作:

GetMetadataPropertyValue(iothub, '[User]') as userprops

您的浓缩数据将在用户属性中。

示例:

设备遥测数据:

{"counter":29,"time":"2019-08-08T13:42:26.1517415Z","deviceId":"device1","windSpeed":8.2023,"temperature":16.06,"humidity":79.46}

发布主题:

devices/device1/messages/events/$.ct=application%2Fjson&$.ce=utf-8&abcd=1234567

IoT Hub丰富的消息: enter image description here

ASA工作:

select 
    *,
    GetMetadataPropertyValue(iothub, '[User]') as userprops 
into
    outAF
from 
    iothub

Azure功能(outAF)上的输出:

[
  {
    "counter": 29,
    "time": "2019-08-08T13:42:26.1517415Z",
    "deviceId": "device1",
    "windSpeed": 8.2023,
    "temperature": 16.06,
    "humidity": 79.46,
    "EventProcessedUtcTime": "2019-08-08T13:42:25.7495769Z",
    "PartitionId": 1,
    "EventEnqueuedUtcTime": "2019-08-08T13:42:25.568Z",
    "IoTHub": {
      "MessageId": null,
      "CorrelationId": null,
      "ConnectionDeviceId": "device1",
      "ConnectionDeviceGenerationId": "636842046144267242",
      "EnqueuedTime": "2019-08-08T13:42:25.363Z",
      "StreamId": null
    },
    "User": {
      "abcd": "1234567",
      "status": "inprocess",
      "version": "42"
    },
    "userprops": {
      "abcd": "1234567",
      "status": "inprocess",
      "version": "42"
    }
  }
]

以下屏幕片段显示了来自第二个自定义端点的事件消息,用于充实消息,例如EventGrid:

{
  "id": "b983e8bf-88b5-cac3-9370-2c64037b2f1c",
  "topic": "/SUBSCRIPTIONS/00000000-0000-0000-0000-000000000000/RESOURCEGROUPS/myRG/PROVIDERS/MICROSOFT.DEVICES/IOTHUBS/myIOT",
  "subject": "devices/device1",
  "eventType": "Microsoft.Devices.DeviceTelemetry",
  "eventTime": "2019-08-08T13:42:25.363Z",
  "data": {
    "properties": {
      "abcd": "1234567",
      "status": "inprocess",
      "version": "42"
    },
    "systemProperties": {
      "iothub-content-type": "application/json",
      "iothub-content-encoding": "utf-8",
      "iothub-connection-device-id": "device1",
      "iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
      "iothub-connection-auth-generation-id": "636842046144267242",
      "iothub-enqueuedtime": "2019-08-08T13:42:25.363Z",
      "iothub-message-source": "Telemetry"
    },
    "body": {
      "counter": 29,
      "time": "2019-08-08T13:42:26.1517415Z",
      "deviceId": "device1",
      "windSpeed": 8.2023,
      "temperature": 16.06,
      "humidity": 79.46
    }
  },
  "dataVersion": "",
  "metadataVersion": "1"
}

答案 1 :(得分:0)