使用 stats() 时如何从字段中提取值

How to extract a value from fields when using stats()

查询:

index = test
| stats values(*) as * by ip_addr, location
| where location="USA"
| fields timestamp, user, ip, location, message

结果:

+--------------------------------------------------------------------+
| timestamp         | user   | ip          | location | message      |
+--------------------------------------------------------------------+
| 08/08/2020 17:00  | thomas | 10.10.10.10 | USA      | Hello, world!|
| 08/08/2020 17:05. | unknown|             |          | I love steak!|
| 08/08/2020 17:10. |        |             |          | I love soda! |
+--------------------------------------------------------------------+
| 08/08/2020 17:00  | jeffry | 10.10.10.20 | USA      | Hello, world!|
| 08/08/2020 17:35  | unknown|             |          | I love pancke|
| 08/08/2020 17:40  |        |             |          | I love waffle|
+--------------------------------------------------------------------+

我想:

  1. 使那些多个时间戳成为一个时间戳
  2. 删除“用户”字段中的“未知”值
  3. 让“消息”字段只显示“Hello, world!” - 我不关心其余的。

我试过:

index = test
| stats values(*) as * by ip_addr
| where location="USA"
| eval user=replace(user, "unknown", "")
| fields timestamp, user, ip, location, message

但它删除了“用户”字段下的所有值。有什么建议吗?我的 2 号和 3 号目标看起来很相似。如果我能破解其中一个,我想我可以轻松解决另一个。

是的,时间戳可以取平均值,如果它们是纪元(整数)形式的话。 values(*) 函数的结果是一个 multi-value 字段,它不能很好地与 replace 或大多数其他不是为它们设计的命令和函数一起使用。这就是我使用下面的 mvfiltermvdedup 命令的原因。

index = test
| where location="USA"
| stats earliest(timestamp) as timestamp, values(*) as * by ip_addr
| eval user=mvdedup(mvfilter(!match(user, "unknown")),
 message=mvdedup(mvfilter(match(message, "Hello, world!"))
| fields timestamp, user, ip, location, message