azure - 将 JSON 数据行转换为列

标签 azure azure-stream-analytics stream-analytics

我需要以下方面的帮助。 我有这样的数据

[{
"id": "0001",
"type": "donut",
"name": "Cake",
"topping":
    [
        { "id": "5003", "type": "Chocolate" },
        { "id": "5004", "type": "Maple" }
    ]
}]

我想将其转换为以下内容

enter image description here

参数将是动态的或多个,而不仅仅是 Chocolate 和 Maple )

我想创建一个流分析查询来处理这些数据并将其存储到目标表中,目标表中已经有这些列,如 Id、Name、Type、Chocolate、Maple...... 请帮助我。

最佳答案

您可以通过udf获得帮助在 ASA 中。

UDF 代码:

function main(arg) {
    var array = arg.topping;
    var map = {};
    map["id"] = arg.id;
    map["type"] = arg.type;
    map["name"] = arg.name;
    for(var i=0;i<array.length;i++){        
        var key=array[i].type;        
        map[key] = array[i].id;      
    }
    return map;  
}

SQL:

WITH 
c AS
(
    SELECT 
    udf.processArray(jsoninput) as result
    from jsoninput
)

select c.result
INTO
    jaycosmos
from c

示例数据:

[{
"id": "0001",
"type": "donut",
"name": "Cake",
"topping":
    [
        { "id": "5003", "type": "Chocolate" },
        { "id": "5004", "type": "Maple" }
    ]
},
{
"id": "0002",
"type": "donut2",
"name": "Cake2",
"topping":
    [
        { "id": "5005", "type": "Chocolate" }
    ]
}
]

输出:

enter image description here

关于azure - 将 JSON 数据行转换为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53075735/

相关文章:

python - 错误: "Your deployment does not have an associated swagger.json" - ACI deployment on Stream Analytics Job

angular - 在 Azure 中部署 Angular 应用程序,但刷新时出现 URL 重写模块错误

azure - 通过 Microsoft Graph API 将用户添加到全局地址列表 (GAL)

azure - 尝试将自定义域添加到新应用服务时与现有主机名冲突

c# - 使用弹性数据库客户端库、EF 和 Identity 时找不到 DbContext

azure - 流分析查询达到大小限制

azure - Azure 流分析中的跳跃窗口

azure-sql-database - 流分析 - 如何处理引用输入中的 json

azure - 流分析无法立即处理,恒定 5 秒水印

azure - Azure 流分析的嵌套条件