如何在KQL中使用正则表达式列表调用replace_regex函数?
在KQL中高效批量替换URL中的ID以实现分组统计
问题背景
我在Azure环境中使用Kusto查询语言(KQL)对Dependencies表的活动数据进行汇总统计,但Name字段是多种格式的URL,其中包含客户ID,导致无法正常分组统计。我已准备好一组用于过滤ID的正则表达式,希望找到比嵌套调用replace_regex更高效、优雅的实现方式。
示例表
| Name | Success |
|---|---|
| api/path?id=0150403650 | True |
| api/path?id=0150403651 | False |
| another/path/0150403611/validate | True |
| another/path/0150403612/validate | False |
| more/paths/4863FDA918BB42959C88E06D72E04238/etc | True |
| more/paths/4863FDA918BB42959C88E06D72E04238/etc | False |
期望结果
| Name | Success | Count |
|---|---|---|
| api/path?id=id | True | 1234 |
| api/path?id=id | False | 1234 |
| another/path/id/validate | True | 1234 |
| another/path/id/validate | False | 1234 |
| more/paths/guid/etc | True | 1234 |
| more/paths/guid/etc | False | 1234 |
当前可运行但不够优雅的写法
dependencies | extend call = replace_regex(replace_regex(name, @"[\/]?[a-zA-Z0-9]{32}", "guid"), @"[\/]+[0-9]{10}", "id") | summarize c = count(call) by call, success
设想的写法(不可行)
let regs = dynamic([@"[\/]?[a-zA-Z0-9]{32}", @"[\/]+[0-9]{10}", @"[=]{1}[0-9]{10}"]); dependencies //| where name matches regex regs | extend call = replace_regex(name, regs, "") | summarize c = count(call) by call, success
解决方案
KQL的replace_regex不支持直接传入正则表达式数组批量替换,但可以通过以下两种方式实现更优雅的批量替换:
方式1:使用reduce迭代替换规则(易维护)
定义包含正则模式和对应替换值的规则数组,通过reduce遍历规则依次替换,避免嵌套写法:
let replace_rules = dynamic([ {pattern: @"[\/]?[a-zA-Z0-9]{32}", replacement: "guid"}, {pattern: @"\/[0-9]{10}", replacement: "/id"}, {pattern: @"=[0-9]{10}", replacement: "=id"} ]); dependencies | extend call = name | reduce over replace_rules with (call = replace_regex(call, replace_rules.pattern, replace_rules.replacement)) | summarize Count = count() by call, Success
方式2:合并正则分支(性能更优)
将所有正则规则合并为一个模式,通过match_group判断匹配分支并返回对应替换值,适合规则较少的场景:
dependencies | extend call = replace_regex(name, @"([\/]?[a-zA-Z0-9]{32})|(\/[0-9]{10})|(=[0-9]{10})", iif(match_group(1) != "", "guid", iif(match_group(2) != "", "/id", "=id"))) | summarize Count = count() by call, Success
内容的提问来源于stack exchange,提问作者404usernotfound
相关产品推荐
相关产品推荐

