azure-iot-hub - 如何在流分析中读取 IoT 中心消息 'application properties'?

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

我已经根据 Microsoft documentation 丰富了 IoT Hub 消息并将其路由到内置端点,然后让流分析通过提供 IoT 中心作为输入来访问消息。

IoT Hub 消息扩充将扩充数据添加到消息的应用程序属性而不是正文本身,因此我在尝试从流分析获取扩充数据时遇到了挑战 - 因为流分析的输出是 Blob 和它只包含我发送到 IoT 中心的实际消息。

此处的丰富数据是指我根据设备孪生属性映射到在 IoT 中心注册的设备的某些数据(如位置、身份等)。

我已经尝试了 GetMetadataPropertyValue 中提到的步骤和 parsing JSON in Stream Analytics ,但就直接从流分析获取“应用程序属性”而言,运气不佳。

谁能帮我弄清楚如何从流分析访问应用程序属性或至少指向正确的资源?

谢谢。

最佳答案

在您的查询中尝试以下操作:

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

您的丰富数据将在 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 中心丰富消息: 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"
}

关于azure-iot-hub - 如何在流分析中读取 IoT 中心消息 'application properties'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57412289/

相关文章:

azure - 未找到事件 - Azure 事件中心

Azure 流分析 : Regex in Reference Data

azure - 在 Azure 流分析上获取正在行驶的汽车之间的实时距离

azure - 我可以为 azure IoTHub 中的设备配置设置复杂的目标条件吗?

azure - 如何使用 Azure CLI 从 Azure IoT 中心删除所有设备?

azure - 如何从 Azure IoT 中心获取设备上的日期时间?

Azure 流分析不提供 DocumentDB 输出接收器

azure - 非分区流分析作业输出

Azure 主题并通过 MQTT 发送消息

azure - 将iothub消息分发到所有分区