什么是用于时序见解中的有用 Azure IoT 中心 JSON 消息结构

问题描述

标题听起来很全面,但我想我的基本问题很简单。

背景

我 Azure,我有一个 IoT 中心,我要向其发送消息。我使用 samples from the Azure Iot SDK for python修改版本之一。

发送工作正常。但是,我发送的不是字符串,而是 JSON 结构。

当我使用 Cloud shell 观察流入 IoT 中心的事件时,它看起来像这样:

PS /home/marcel> az iot hub monitor-events --hub-name weathertestiothub
This extension 'azure-cli-iot-ext' is deprecated and scheduled for removal. Please remove and add 'azure-iot' instead.
Starting event monitor,use ctrl-c to stop...
{
"event": {
        "origin": "raspBerrypi-zero-wh","payload": "{ \"timestamp\": \"1608643863720\",\"locationDescription\": \"Attic\",\"temperature\": \"21.941\",\"relhumidity\": \"71.602\" }"
    }
}

问题

数据看起来不错,除了这里的有效载荷看起来很奇怪。但是,有效载荷实际上是我使用 SDK 示例从设备发送的内容

这是正确的做法吗?最后,我很难将数据真正输入到时间序列洞察模型中。所以我想,应该怪我的结构。

问题

建议发送到 IoT 中心以备后用的 JSON 数据结构是什么?

解决方法

您应该将以下 2 行添加到您的 Python SDK 示例中的消息中:

msg.content_encoding = "utf-8"
msg.content_type = "application/json"

这应该可以解决您的格式问题。

我们还更新了示例以反映这一点:https://github.com/Azure/azure-iot-sdk-python/blob/master/azure-iot-device/samples/sync-samples/send_message.py

,

我最终使用了@elhorton 的提示,但这不是关键变化。尽管如此,Azure Shell Monitor 中的格式现在看起来好多了:

"event": {
    "origin": "raspberrypi-zero-wh","payload": {
        "temperature": 21.543947753906245,"humidity": 69.22964477539062,"locationDescription": "Attic"
    }
}

关键是:

  1. 以 ISO 格式包含消息源时间
    from datetime import datetime
    timestampIso = datetime.now().isoformat()
    message.custom_properties["iothub-creation-time-utc"] = timestampIso
  1. 使用 locationDescription 作为 Time Series ID Property 参见 https://docs.microsoft.com/en-us/azure/time-series-insights/how-to-select-tsid(也许我也可以使用 iothub-connection-device-id,但我没有单独测试)
,

我猜使用“iothub-connection-device-id”将使“raspberrypi-zero-wh”作为时间序列实例的名称。我同意您选择使用“locationDescription”作为 TSID;所以Attic 成为时间序列实例名称,温度湿度 将是您的变量。