You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:在Kusto中生成累积唯一服务器ID数组

解决KQL累计唯一服务器ID数组的问题

你的问题出在prev(ServerIds)只能获取上一行的服务器ID数组,所以从第三行开始,你只是把当前行的ID和第二行的ID合并,完全没包含第一行的内容,自然无法得到截至当前时间点的所有唯一ID。

要实现持续累加的唯一ID数组,应该用KQL的scan操作符,它可以维护一个跨行的状态变量,逐步累计所有历史的唯一ID。

修改后的查询如下:

range Time from 1 to 100 step 1
| extend ServerIds=array_sort_asc(pack_array(rand(100), rand(100),
    rand(100), rand(100), rand(100), rand(100),
    rand(100), rand(100), rand(100), rand(100)))
| extend ServerIds=array_slice(ServerIds, 0, toint(rand(10)))
// 生成100个长度1到10的随机数组
| sort by Time asc
| scan declare(CumulativeUnique: dynamic = dynamic([])) with (
    step s: CumulativeUnique = array_sort_asc(set_union(s.CumulativeUnique, s.ServerIds));
)
| project Time, ServerIds, CumulativeUniqueServerIds = CumulativeUnique

关键说明:

  • scan操作符里声明了一个状态变量CumulativeUnique,初始化为空数组
  • 每一步都会把当前行的ServerIds和之前累计的CumulativeUnique做并集,再排序后更新状态
  • 最终通过project把状态变量重命名为你需要的CumulativeUniqueServerIds

这个方案完全适配你的场景:服务器总数少于1000、每个时间步新增少于10台,性能上不会有任何问题。

内容的提问来源于stack exchange,提问作者Tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 04:54:57