迭代对象中的项

时间:2021-07-07 19:26:22

I am trying to parse through the json output of one of our es clusters to collect filter cache stats and was hoping to use Jq to do it. Here is the output from the curl command:

我正在解析我们其中一个es集群的json输出,以收集过滤器缓存统计数据,并希望使用Jq来实现这一点。下面是curl命令的输出:

{
  "_shards": {
    "total": 5662,
    "successful": 5662,
    "failed": 0
  },
  "_all": {
    "primaries": {
      "filter_cache": {
        "memory_size": "32.8gb",
        "memory_size_in_bytes": 35245081088,
        "evictions": 31347095
      }
    },
    "total": {
      "filter_cache": {
        "memory_size": "94.3gb",
        "memory_size_in_bytes": 101307321504,
        "evictions": 79329152
      }
    }
  },
  "indices": {
    "oreserverdk04180047": {
      "primaries": {
        "filter_cache": {
          "memory_size": "0b",
          "memory_size_in_bytes": 0,
          "evictions": 11
        }
      },
      "total": {
        "filter_cache": {
          "memory_size": "0b",
          "memory_size_in_bytes": 0,
          "evictions": 132
        }
      }
    },
    "janbe10200002": {
      "primaries": {
        "filter_cache": {
          "memory_size": "0b",
          "memory_size_in_bytes": 0,
          "evictions": 88
        }
      },
      "total": {
        "filter_cache": {
          "memory_size": "0b",
          "memory_size_in_bytes": 0,
          "evictions": 119
        }
      }
    }
  }
}

Basically I would like to get the output to look something like this:

基本上我想让输出看起来像这样:

oreserverdk04180047 0b
janbe10200002 0b

I just want the name of the index and the memory_size column from "total". I can get it if I run this through hardcoding the index names as such:

我只想要“total”中的索引和memory_size列的名称。如果我对索引名进行硬编码,就可以得到它:

jq '. | {memory_size: .indices.janbe10200002.total.filter_cache.memory_size}'

But I was hoping to iterate through using some sort of wild card for the index name.

但是我希望通过使用某种通配符来迭代索引名称。

2 个解决方案

#1


1  

jq -r  '
  .indices
  | to_entries[]
  | "\(.key) \(.value.total.filter_cache.memory_size)"
' input.json

Output:

输出:

oreserverdk04180047 0b
janbe10200002 0b

#2


0  

Here is a solution that uses foreach

这里有一个使用foreach的解决方案

  .indices
| foreach keys[] as $k (
      .
    ; .
    ; "\($k) \(.[$k].total.filter_cache.memory_size)"
  )

EDIT: I now realize a filter of the form foreach E as $X (.; .; R) can almost always be rewritten as E as $X | R so the above is really just

编辑:我现在意识到每个E的表单的过滤器为$X。;R)几乎都可以写成E = $X | R所以上面的式子就是

  .indices
| keys[] as $k
| "\($k) \(.[$k].total.filter_cache.memory_size)"

#1


1  

jq -r  '
  .indices
  | to_entries[]
  | "\(.key) \(.value.total.filter_cache.memory_size)"
' input.json

Output:

输出:

oreserverdk04180047 0b
janbe10200002 0b

#2


0  

Here is a solution that uses foreach

这里有一个使用foreach的解决方案

  .indices
| foreach keys[] as $k (
      .
    ; .
    ; "\($k) \(.[$k].total.filter_cache.memory_size)"
  )

EDIT: I now realize a filter of the form foreach E as $X (.; .; R) can almost always be rewritten as E as $X | R so the above is really just

编辑:我现在意识到每个E的表单的过滤器为$X。;R)几乎都可以写成E = $X | R所以上面的式子就是

  .indices
| keys[] as $k
| "\($k) \(.[$k].total.filter_cache.memory_size)"