使用GJSON语法的选择表达式,就像在jq示例中一样。

huangapple go评论92阅读模式
英文:

Selection expression using GJSON syntax, as in the example with jq

问题

我正在尝试使用Telegraf配置HTTP输入插件,但我需要使用选择表达式选择特定的指标,并将它们写入特定的Influxdb数据库。我知道如何使用jq进行选择,但我无法使用GJSON请求来实现。

我的输出如下所示:

  [
      {
        "properties": {
          "headers": {
            "database": "telegraf"
          },
          "content_type": "text/plain"
        },
        "payload": "net,cluster_id=cluster01,dc=dc02,host=specific_host01,instance=instance-00107205 network_write_packets=15100740,network_write_drops=0 1628285530000000000\n",
        "payload_encoding": "string"
      },
      {
        "properties": {
          "headers": {
            "database": "stats"
          },
          "content_type": "text/plain"
        },
        "payload": "dsk_stats,Region=Q3,host=specific_host01 disk_usage=8.344534641 1628285530000000000\n",
        "payload_encoding": "string"
      }
  ]

为了通过stats选择指标,我使用jq命令:

    curl -s -H "content-type:application/json" localhost:8080 |jq '.[] | select(.properties.headers.database=="stats") | "\(.payload)"'
    dsk_stats,Region=Q3,host=specific_host01 disk_usage=8.344534641 1628285530000000000\n

是否可能使用GJSON语法进行这样的JSON选择?

英文:

I am trying to configure the HTTP input plugin using Telegraf, but I need to select certain metrics using the select expression and write them to a specific Influxdb database. I know how to select using jq, but I can't do it using a GJSON request.

My output look like this:

  [
      {
        "properties": {
          "headers": {
            "database": "telegraf"
          },
          "content_type": "text/plain"
        },
        "payload": "net,cluster_id=cluster01,dc=dc02,host=specific_host01,instance=instance-00107205 network_write_packets=15100740,network_write_drops=0 1628285530000000000\n",
        "payload_encoding": "string"
      },
      {
        "properties": {
          "headers": {
            "database": "stats"
          },
          "content_type": "text/plain"
        },
        "payload": "dsk_stats,Region=Q3,host=specific_host01 disk_usage=8.344534641 1628285530000000000\n",
        "payload_encoding": "string"
      }
  ]

For choosing metrics by stats I'm using jq:

    curl -s -H "content-type:application/json" localhost:8080 |jq '.[] | select(.properties.headers.database=="stats") | "\(.payload)"
    dsk_stats,Region=Q3,host=specific_host01 disk_usage=8.344534641 1628285530000000000\n

Is it possible to make such json selection using the GJSON syntax?

答案1

得分: 2

给你这个:

#(properties.headers.database=="stats").payload

https://play.golang.org/p/7bXY6F2MQeZ

英文:

Here you go

#(properties.headers.database=="stats").payload

https://play.golang.org/p/7bXY6F2MQeZ

huangapple
  • 本文由 发表于 2021年8月7日 06:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/68688056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定