如何在Kusto中​​遍历数组?

时间:2019-05-16 00:55:39

标签: kusto azure-data-explorer

我需要遍历Azure资源图资源管理器(https://preview.portal.azure.com/)中的托管数据磁盘。我的查询在下面,但它返回JSON数组,我需要提取磁盘名称和正在使用的存储帐户的类型(下面是JSON返回示例)。因此,我想在屏幕上按计算机名称,磁盘名称和存储帐户类型进行分组。我当前的查询在下面,但显然由于返回JSON而无法正常工作

where type =~ 'Microsoft.Compute/virtualmachines' |
extend disks = properties.storageProfile.dataDisks |
project name, disks

相同的JSON输出

[
    {
        "name": "COMP02_DDisk1",
        "createOption": "Attach",
        "diskSizeGB": 400,
        "managedDisk": {
            "id": "/subscriptions/5f5c5be9-77d4db790171/resourceGroups/BRAZILSOUTHDB/providers/Microsoft.Compute/disks/COMP02_DDisk1",
            "storageAccountType": "Premium_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 0,
        "writeAcceleratorEnabled": false
    },
    {
        "name": "COMP02_DDisk2",
        "createOption": "Attach",
        "diskSizeGB": 400,
        "managedDisk": {
            "id": "/subscriptions/5f5c5be9-77d4db790171/resourceGroups/BRAZILSOUTHDB/providers/Microsoft.Compute/disks/COMP02_DDisk2",
            "storageAccountType": "Premium_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 1,
        "writeAcceleratorEnabled": false
    }
]

enter image description here

2 个答案:

答案 0 :(得分:1)

希望您做的很好。

您也可以尝试这种方式,首先我从整个集合中找到了networksecuritygroups,然后过滤了 defaultSecurityRules ,它又是一个数组。 使用本地变量规则中的 mvexpand 收集它之后,您应该能够在应用后获取所需的对象。

 where type =~ "microsoft.network/networksecuritygroups"
| mvexpand rules = properties.defaultSecurityRules
| where rules.properties.destinationAddressPrefix =~ "*"

您也可以参考下面给出的链接,希望它也会对您有所帮助。

答案 1 :(得分:0)

在这种情况下,使用mv-expand扩展数组然后为每个记录应用动态属性访问器通常会很有帮助。

https://docs.microsoft.com/en-us/azure/kusto/query/mvexpandoperator

示例:

print d = dynamic([
    {
        "name": "COMP02_DDisk1",
        "createOption": "Attach",
        "diskSizeGB": 400,
        "managedDisk": {
            "id": "/subscriptions/5f5c5be9-77d4db790171/resourceGroups/BRAZILSOUTHDB/providers/Microsoft.Compute/disks/COMP02_DDisk1",
            "storageAccountType": "Premium_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 0,
        "writeAcceleratorEnabled": false
    },
    {
        "name": "COMP02_DDisk2",
        "createOption": "Attach",
        "diskSizeGB": 400,
        "managedDisk": {
            "id": "/subscriptions/5f5c5be9-77d4db790171/resourceGroups/BRAZILSOUTHDB/providers/Microsoft.Compute/disks/COMP02_DDisk2",
            "storageAccountType": "Premium_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 1,
        "writeAcceleratorEnabled": false
    }
])
| mv-expand d
| project d.name, d.managedDisk.storageAccountType

它将输出:

| d_name        | d_managedDisk_storageAccountType |
|---------------|----------------------------------|
| COMP02_DDisk1 | Premium_LRS                      |
| COMP02_DDisk2 | Premium_LRS                      |