c# - 从存储帐户 blob 读取和解析 Azure IoT 中心遥测

标签 c# azure azure-blob-storage iot azure-iot-hub

我有一个 IoT 中心,其中有 2 个 IoT 设备都发送环境数据(温度和湿度),我创建了一个自定义端点并路由到我的 Azure 存储帐户,它保存在那里,以便我可以回顾。问题是它发送原始遥测数据,包括奇怪的字符和类似的东西。我发现它是 CSV 格式,但我仍然无法解析它。

我的问题是, 我想直接从 blob 获取原始遥测数据,并使用 C# 将其解析为 SensorResult 类。 我想出了如何读取 blob 数据,我只需要解析遥测数据。但我无法弄清楚那部分。

原始遥测数据片段:

Objavro.codecnullavro.schemaÐ{"type":"record","name":"Message","namespace":"Microsoft.Azure.Devices","fields":[{"name":"EnqueuedTimeUtc","type":"string"},{"name":"Properties","type":{"type":"map","values":"string"}},{"name":"SystemProperties","type":{"type":"map","values":"string"}},{"name":"Body","type":["null","bytes"]}]}°ÈF¬êYâS:á…ð82018-06-29T18:34:10.4320000Z temperatureAlert
falsemessageIdmessage_0correlationIdcorrelation_0$connectionDeviceIdraspi(connectionAuthMethodœ{"scope":"device","type":"sas","issuer":"iothub","acceptingIpFilterRule":null}8connectionDeviceGenerationId$0enqueuedTime82018-06-29T18:34:10.4320000Z¨{"deviceId": "Raspberry Pi - Python","temperature": 28.801060,"humidity": 40.097196}82018-06-29T18:34:12.3400000Z temperatureAlert

第一行是遥测的标题,下面的所有行只是来自物联网设备的消息。

谢谢。

最佳答案

以下是 avro reader 的代码片段:

 using Microsoft.Hadoop.Avro.Container;

 // ...
 using (var reader = AvroContainer.CreateGenericReader(myBlob))
 {
    while (reader.MoveNext())
    {
       foreach (dynamic record in reader.Current.Objects)
       {
          var bodyText = Encoding.UTF8.GetString(record.Body);
          log.Info($"AvroRecord = {bodyText}");
       }
    }
 }

关于c# - 从存储帐户 blob 读取和解析 Azure IoT 中心遥测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52017897/

相关文章:

c# - 如何使用字符串中的 XML 标记?

c# - 文本框/输入文本中的 HTML 标记

c# - 在 ArrayList c# 中的某个位置插入

c# - 文件不包含 "WriteAllTextAsync"的定义?

azure - 如何从客户端访问 Azure Blob 服务?

c# - 使用 asp-ajax 按顺序加载项目并提高 Web 应用程序速度

angular - 使用 Github 操作找不到 Azure 静态 Web 应用程序 : staticwebapp. config.json

caching - 将 Azure Redis 缓存设置为 LRU

azure - 使用 blobproperties.getcreatedtime 时 SparkHDInsights 集群不起作用

azure - 如何查看 Azure 存储资源管理器 BLOB 的文件夹