英文:
Azure Resource Graph KQL - filter virtual machines having tags with given prefixes
问题
以下是翻译好的部分:
我们有很多在Azure中创建的虚拟机。每个虚拟机都有标签。其中一些标签的键是动态构建的。这些键具有固定的前缀和动态的后缀。我们希望找到一种方法,可以筛选出所有具有给定前缀的2个或更多键的虚拟机。
考虑以下虚拟机:
- VM1:{name: "vm-1", tags: {"bingo-1":1, "tringo-2":1, "key1": "value1"}}
- VM2:{name: "vm-2", tags: {"tringo-2":1, "key2": "value2"}}
- VM3:{name: "vm-3", tags: {"bingo-1":1, "key3": "value3"}}
预期输出:vm-1
,因为它是唯一包含两个标签键前缀的虚拟机。
尝试:
我已经让这个工作了,但这似乎不是最优的,因为它两次调用了bag_keys函数。
resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| where resourceGroup =~ 'test-resource-group'
| where bag_keys(tags) hasprefix "bingo-"
| where bag_keys(tags) hasprefix "tringo-"
| project name
这可以按预期工作,但代价是两次调用bag_keys。是否有更符合惯用方法且更优化的方式来实现相同的目标,而不必两次调用bag_keys?
英文:
We have a lot of virtual machines that we create in azure. Each virtual machine has tags. Keys for some of these tags are dynamically constructed. Such keys have a fixed prefix and a dynamic suffix. We wanted a way to filter all virtual machines which have 2 or more keys with given prefixes.
Consider the following Virtualmachines:
- VM1: {name: "vm-1", tags: {"bingo-1":1, "tringo-2":1, "key1": "value1"}}
- VM1: {name: "vm-1", tags: {"tringo-2":1, "key2": "value2"}}
- VM1: {name: "vm-1", tags: {"bingo-1":1, "key3": "value3"}}
Expected Output: vm-1
as that is the only VM which contains both the tag-key-prefixes.
Attempt:
I got this working but this does not seem to be optimal as it calls bag_keys function twice.
resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| where resourceGroup =~ 'test-resource-group'
| where bag_keys(tags) hasprefix "bingo-"
| where bag_keys(tags) hasprefix "tringo-"
| project name
This works as expected but at the cost of invoking bag_keys twice. Is there a more idiomatic and optimal way to achieve the same without calling bag_keys twice?
答案1
得分: 0
与使用两个 `where` 操作符不同,首先我使用了 [mv-expand](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mvexpandoperator) 操作符将标签合并为一条记录,然后使用 `and` 来检查和过滤两个前缀的标签,以优化您的代码。
```ps1
resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| where resourceGroup =~ '<resourcegroup>'
| mvexpand tags
| extend tagKey = tostring(bag_keys(tags)[0])
| extend tagValue = tostring(tags[tagKey])
| where tagKey hasprefix "creat" and tagKey hasprefix "cr"
| project name, tags, tagKey, tagValue
更新:
根据评论提供的信息,您可以使用 extend
操作符。它会创建一个包含标签对象中所有键的新列,如下所示。
extend tagKeys = bag_keys(tags)
| where tagKeys hasprefix "bingo-" and tagKeys hasprefix "tringo-"
请参考 @John Kilmister 撰写的资源图探索器查询 文章 获取更多相关查询示例。
<details>
<summary>英文:</summary>
Instead of using two `where` operators, firstly I included
[mv-expand](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mvexpandoperator) operator to join tags into one record and then used an `and` to check and filter the tags of both prefixes to optimize your code.
```ps1
resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| where resourceGroup =~ '<resourcegroup>'
| mvexpand tags
| extend tagKey = tostring(bag_keys(tags)[0])
| extend tagValue = tostring(tags[tagKey])
| where tagKey hasprefix "creat" and tagKey hasprefix "cr"
| project name, tags, tagKey, tagValue
Update:
As given in comments, You can use extend
operator. It creates a new column that contains all the keys in the tags object in this way.
extend tagKeys = bag_keys(tags)
| where tagKeys hasprefix "bingo-" and tagKeys hasprefix "tringo-"
Refer resource graph explorer query article by @John Kilmister for more related query samples.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论