You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Azure Stream Analytics将嵌套动态键JSON写入Azure表?

问题:嵌套动态键JSON通过Azure Stream Analytics写入Azure表

需要将以下带有动态设备键的嵌套JSON数据,通过Azure Stream Analytics从Azure IoT Hub写入Azure表。目前能处理简单JSON格式的数据,但对嵌套且含动态键的数据处理方式感到困惑,期望最终结果符合目标表结构(原问题提及两张表截图,此处以数据平铺结构为目标)。

示例JSON数据

[
  {
    "device1": [
      {
        "name": "temperature",
        "value": 50
      },
      {
        "name": "voltage",
        "value": 220
      }
    ]
  },
  {
    "device2": [
      {
        "name": "temperature",
        "value": 10
      },
      {
        "name": "voltage",
        "value": 200
      }
    ]
  }
]

尝试过的简单查询代码

SELECT
    i.messageId AS messageId,
    i.name AS name,
    i.PartitionId,
    i.EventProcessedUtcTime,
    i.EventEnqueuedUtcTime
INTO
    [table]
FROM
    [input] i

解决方案

针对动态键嵌套结构,需要使用Azure Stream Analytics的GetRecordProperties函数提取动态设备键,再结合CROSS APPLY展开嵌套数组,将数据平铺为符合Azure表要求的行格式,具体查询如下:

SELECT
    deviceProps.PropertyName AS DeviceId,
    metric.ArrayValue.name AS MetricName,
    metric.ArrayValue.value AS MetricValue,
    i.PartitionId,
    i.EventProcessedUtcTime,
    i.EventEnqueuedUtcTime
INTO
    [table]
FROM
    [input] i
CROSS APPLY GetRecordProperties(i) AS deviceProps
CROSS APPLY GetArrayElements(deviceProps.PropertyValue) AS metric

关键说明

  • GetRecordProperties(i):将顶层的动态设备键(如device1、device2)转换为键值对,PropertyName对应设备名称,PropertyValue对应该设备的指标数组。
  • GetArrayElements(deviceProps.PropertyValue):展开每个设备的指标数组,将数组中的每个对象拆分为单独的行,提取其中的name和value字段。

执行该查询后,数据会被拆分为如下结构的行,适配Azure表的存储需求:

DeviceIdMetricNameMetricValuePartitionIdEventProcessedUtcTimeEventEnqueuedUtcTime
device1temperature50.........
device1voltage220.........
device2temperature10.........
device2voltage200.........

内容的提问来源于stack exchange,提问作者Jaykant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 05:40:06