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

如何在Splunk中提取JSON对象键并分析产品绿色计数降幅?

Splunk SPL处理产品数据:提取JSON键、生成表格及降幅分析

场景说明

我的Splunk实例每小时从数据库拉取产品数据,返回JSON结构如下:

{
  "counts": {
    "green": 413,
    "red": 257,
    "total": 670,
    "product_list": {
      "urn:product:1": {
        "name": "M & Ms",
        "total": 332,
        "green": 293,
        "red": 39
      },
      "urn:product:2": {
        "name": "Christmas Ornaments",
        "total": 2,
        "green": 0,
        "red": 2
      },
      "urn:product:3": {
        "name": "Traffic Lights",
        "total": 1,
        "green": 0,
        "red": 1
      },
      "urn:product:4": {
        "name": "Stop Signs",
        "total": 2,
        "green": 0,
        "red": 2
      }
    }
  }
}

已有针对全局counts.green的24小时降幅告警查询:

index=database_catalog source=RedGreenData | head 1 
| spath path=counts.green output=green_now
| table green_now
| join host
    [| search index=database_catalog source=RedGreenData latest=-1d | head 1 | spath path=counts.green output=green_yesterday
    | table green_yesterday]
| where green_yesterday > 0
| eval delta=(green_yesterday - green_now)/green_yesterday * 100 
| where delta > 10 

我需要实现:提取所有产品的ID、名称,生成包含今日绿色计数、昨日绿色计数的表格,筛选出昨日绿色计数为正且降幅超过10%的产品,当前卡在提取JSON对象的键(即product_list中的urn:product:*)这一步。


完整解决方案

核心SPL查询

// 1. 提取今日产品的所有数据
index=database_catalog source=RedGreenData 
| head 1  // 获取最新的一小时数据
| spath path=counts.product_list output=product_list  // 提取产品列表JSON对象
| spath input=product_list mode=jsonkeys output=product_ids  // 提取所有产品ID(JSON对象的键)
| mvexpand product_ids  // 将多值ID拆分为单个事件,每个事件对应一个产品
| spath input=product_list path="$product_ids$.name" output=name  // 动态提取产品名称
| spath input=product_list path="$product_ids$.green" output=green_now  // 今日绿色计数
| spath input=product_list path="$product_ids$.red" output=red  // 红色计数
| spath input=product_list path="$product_ids$.total" output=total  // 总计数
| rename product_ids as ID
| table ID name green_now red total

// 2. 关联昨日同一小时的产品绿色计数
| join type=left ID
    [| search index=database_catalog source=RedGreenData latest=-1d earliest=-1d+1h  // 匹配昨日同时间段数据
     | head 1 
     | spath path=counts.product_list output=product_list_yesterday
     | spath input=product_list_yesterday mode=jsonkeys output=product_ids_yesterday
     | mvexpand product_ids_yesterday
     | spath input=product_list_yesterday path="$product_ids_yesterday$.green" output=green_yesterday
     | rename product_ids_yesterday as ID
     | table ID green_yesterday]

// 3. 筛选并计算降幅
| where green_yesterday > 0  // 过滤昨日无绿色计数的产品
| eval delta_percent=round((green_yesterday - green_now)/green_yesterday * 100, 2)  // 计算降幅百分比,保留2位小数
| where delta_percent > 10  // 筛选降幅超10%的产品

// 4. 整理输出表格
| table ID name green_now green_yesterday delta_percent red total
| rename green_now as "今日绿色计数", green_yesterday as "昨日绿色计数", delta_percent as "降幅(%)"

关键步骤解释

  1. 提取JSON对象的键:
    使用spath input=product_list mode=jsonkeys output=product_ids,mode=jsonkeys参数会直接提取JSON对象的所有键(即产品ID),生成多值字段product_ids。
  2. 拆分多值字段:
    通过mvexpand product_ids将多值的产品ID拆分为独立事件,确保每个事件对应一个产品,方便后续提取单个产品的属性。
  3. 动态提取产品属性:
    利用SPL的变量引用语法"$product_ids$.name",将当前事件的product_ids值作为JSON路径的一部分,动态提取对应产品的名称、计数等属性。
  4. 时间对齐:
    关联昨日数据时,使用latest=-1d earliest=-1d+1h确保取昨日同一小时的数据,避免因每小时拉取导致的时间偏差,保证数据对比的准确性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 10:50:27