使用阵列的Azure流分析作业

时间:2018-12-27 16:36:00

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

我在流分析中设置了一个作业,该作业从IotHub获取消息并在SQL表中加载值。

每条消息仅使用一组值时,一切工作正常,但是我想使用数组在一条消息中发送几组值。

以下是到达工作的Json消息的示例:

[{
    "valores": {
        "0": {
            "voltage": 230.8,
            "current": 0.18,
            "power": 32.093,
            "frequency": 50,
            "energy": 0.71,
            "dvcid": 1,
            "gway": "SPC-G02",
            "time": "2018-12-27T16:02:20.1690000Z"
        },
        "1": {
            "voltage": 230.7,
            "current": 0.144,
            "power": 23.759,
            "frequency": 50.1,
            "energy": 0.71,
            "dvcid": 1,
            "gway": "SPC-G02",
            "time": "2018-12-27T16:02:25.1690000Z"
        },
        "2": {
            "voltage": 230.7,
            "current": 0.143,
            "power": 23.369,
            "frequency": 50,
            "energy": 0.71,
            "dvcid": 1,
            "gway": "SPC-G02",
            "time": "2018-12-27T16:02:30.1740000Z"
        },
        "3": {
            "voltage": 230.4,
            "current": 0.163,
            "power": 28.075,
            "frequency": 50,
            "energy": 0.71,
            "dvcid": 1,
            "gway": "SPC-G02",
            "time": "2018-12-27T16:02:35.1730000Z"
        },
        "4": {
            "voltage": 230.5,
            "current": 0.167,
            "power": 29.207,
            "frequency": 50,
            "energy": 0.71,
            "dvcid": 1,
            "gway": "SPC-G02",
            "time": "2018-12-27T16:02:40.1810000Z"
        },
    },
    "EventProcessedUtcTime": "2018-12-27T16:03:00.9574234Z",
    "PartitionId": 3,
    "EventEnqueuedUtcTime": "2018-12-27T16:02:58.0290000Z",
    "IoTHub": {
        "MessageId": null,
        "CorrelationId": null,
        "ConnectionDeviceId": "RaspberryPi-Teste1",
        "ConnectionDeviceGenerationId": "636807313519039605",
        "EnqueuedTime": "2018-12-27T16:02:58.0260000Z",
        "StreamId": null
    }
}
]

此输出是从Job中的示例数据实用程序获得的。

我试图建立一个仅收集数据库单列(dvcid)的测试查询,但是当我测试该查询时,我收到一条消息,提示“发生了意外错误。”

 SELECT
 StageElement.arrayvalue.dvcid as dvcid
 INTO [Guardardb]
 FROM [Pc-Sdm230] AS evento
 CROSS APPLY GetArrayElements(evento.valores) AS StageElement

我尝试启动该作业失败。

当我测试以下查询时,一切正常,并且我可以在单个列中看到所有数组Valores:

SELECT
*
INTO [Guardardb]
FROM [Pc-Sdm230] AS evento

我不知道自己在做什么错。

1 个答案:

答案 0 :(得分:2)

GetArrayElement用于JSON中的Array元素。在您的情况下,您已嵌套JSON,因此您需要使用GetRecordProperties。

这是修改后的查询

   SELECT 
   StageElement.PropertyValue.dvcid as dvcid
   INTO [Guardardb]
   FROM [Pc-Sdm230] AS evento
   CROSS APPLY GetRecordProperties(evento.valores) AS StageElement

让我知道它是否对您有用。

谢谢

JS