首页 > 解决方案 > 如何在 Kusto 中创建自定义输出对象

问题描述

我有像下面这样的数组作为我的对象的属性之一。我想提取某些字段并将其仍然作为输出中的数组返回。例如,我只想要namestorageAccountType返回如下

期望的输出

[
    {
        "name": "Data",
        "storageAccountType": "Standard_LRS"
    },
    {
        "name": "Disk2",
        "storageAccountType": "Standard_LRS"
    }
]

输入数组

[
    {
        "name": "Data",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Data",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 0
    },
    {
        "name": "Disk2",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Disk2",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 1
    }
]

标签: azure-data-explorerkql

解决方案


这是您可以遵循的方向(假设您实际上需要取回数组,而不是将数组中的每个元素都放在自己的行中。如果后者很好,请删除带有注释的行(// *

datatable(some_value:string, d:dynamic) // just a sample data set with 2 records
[
    "hello", dynamic([
    {
        "name": "Data",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Data",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 0
    },
    {
        "name": "Disk2",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Disk2",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 1
    }
]), "world", dynamic([
    {
        "name": "Data3",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Data",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 0
    },
    {
        "name": "Disk23",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Disk2",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 1
    }
])
]
// --> answer starts here <--
| extend r = rand() // *
| mv-apply d on (   
    project d = pack("name", d.name, "storageAccountType", d.managedDisk.storageAccountType)
)
| summarize d = make_list(d) by r, some_value // *
| project-away r // *

推荐阅读