Azure 流分析 JSON 解析

Azure Stream Analytics JSON Parsing

尝试建立一个相对简单的 ASA 查询来提取作为数组摄取的数据。我似乎无法弄清楚的是如何让返回的 table 自动填充每个键的列。这意味着我们需要每一行都有一个温度值、湿度值、照明值和接触值(如果存在)列。其他一些数组将只有键:温度和键:湿度。

谢谢!

"telemetry": {
      "data": [
        {
          "key": "temperature",
          "value": 19.1,
          "unit": "°C"
        },
        {
          "key": "humidity",
          "value": 38.5,
          "unit": "%"
        },
        {
          "key": "illumination",
          "value": 0,
          "unit": "lx"
        },
        {
          "key": "accelerationStatus",
          "value": "heartbeat",
          "meaning": "Heartbeat"
        },
        {
          "key": "accelerationX",
          "value": -0.02,
          "unit": "g"
        },
        {
          "key": "accelerationY",
          "value": 0,
          "unit": "g"
        },
        {
          "key": "accelerationZ",
          "value": 1.01,
          "unit": "g"
        },
        {
          "key": "contact",
          "value": "open",
          "meaning": "Window opened"
        }
      ],

您可以在 ASA 中使用 UDF。

UDF函数代码:

function main(arg) {
    var array = arg.telemetry.data;
    var map = {};
    for(var i=0;i<array.length;i++){        
        var key=array[i].key;
        if(key == "temperature" || key == "humidity" || key == "illumination"){
            map[key] = array[i].value + "" + array[i].unit;
        }
        if(key == "contact"){
             map[key] = array[i].value;
        }    
    }
    return map;
}

SQL:

WITH 
c AS
(
    SELECT 
    udf.processArray(t) AS result
    FROM input AS t
)

SELECT c.result
FROM c

结果: