Azure流分析复杂查询未返回期望的结果

时间:2018-07-25 15:32:38

标签: azure azure-stream-analytics

这是输入的JSON数据包。我正在流分析中编写转换查询,以获取source和masterTag属性。

[{
      "source": "xda",
      "data": 
        [{
            "masterTag": "UNIFY",
            "speed": 180
        }],
      "EventEnqueuedUtcTime": "2018-07-20T19:28:18.5230000Z",
  },
  {
      "source": "xda",
      "data": [{
            "masterTag": "UNIFY",
            "speed": 214
        }],
      "EventEnqueuedUtcTime": "2018-07-20T19:28:20.5550000Z",
  }
]

这就是我要做的:

WITH data AS
(
    SELECT
        source,
        GetArrayElement(data,0) as data_packet 
    FROM input 
)

SELECT 
    source, 
    data_packet.masterTag 
INTO 
    output
FROM data

但是,此查询仅返回一个元素。如何更新此查询以获取JSON数据包中的所有元素?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

我对您的源数据和查询进行了测试,它确实获得了2个输出元素。所以,我想你的意思是如何用数据数组返回所有元素有更多的元素。

您可以使用以下查询:

SELECT 
    jsoninput.source, 
    arrayElement.ArrayValue.masterTag
INTO 
    output
FROM jsoninput
CROSS APPLY GetArrayElements(jsoninput.data) AS arrayElement 

Json示例:

[{
      "source": "xda",
      "data": 
        [{
            "masterTag": "UNIFY1",
            "speed": 180
        },
        {
            "masterTag": "UNIFY2",
            "speed": 180
        }],
      "EventEnqueuedUtcTime": "2018-07-20T19:28:18.5230000Z",
  },
  {
      "source": "xda",
      "data": [{
            "masterTag": "UNIFY3",
            "speed": 214
        },
        {
            "masterTag": "UNIFY4",
            "speed": 180
        }],
      "EventEnqueuedUtcTime": "2018-07-20T19:28:20.5550000Z",
  }
]

输出:

enter image description here

希望它对您有帮助。