计算 kusto 语言中 make_set 创建的数组中有多少个元素

问题描述 投票:0回答:2

如何计算 KQL 中 make_set 创建的数组中的元素数量?

kql azure-sentinel
2个回答

0
投票

我想贡献我的解决方案,它实现了 Yoni 的建议,但更进一步,将前 N 个元素枚举为另一列。

我的数据以时间戳和 http 响应 JSON 的表开始。该 JSON 是一个消息属性,我在其中查找错误代码,后跟消息。

我想要的是总结表格并显示错误代码/消息、每个错误代码的计数(按 client_id 分组),最后是前 10 个 client_id 值:

connection_Logs
| where requestJson.message has_any ("FAILURE") // filter for error logs
| extend error = tostring(extract(@"Error: ([0-9]+[A-Za-z ]+)", 1, tostring(requestJson.message)))
| extend client_id = extract(@"CLIENT_ID: ([A-Z]+)", 1, tostring(requestJson.message))
| distinct error, client_id // count each error once per client_id
| summarize client_id_set=make_set(client_id) by error // build a client_id dynamic array of client_id values for each error
| project ['Error Message']=error, Count=array_length(client_id_set), ['Client IDs (max 10)']=
    strcat_array( // join all array values into a string
        array_concat(
            array_slice(client_id_set, 0, 10), // take first 10 client ids
            iff(array_length(client_id_set) > 10, parse_json('["..."]'), parse_json('[]')) // if there are more than 10, append "..." to array
        ),
    ", ") // join all the elements into a string separated by ", "
© www.soinside.com 2019 - 2024. All rights reserved.