bash 脚本对 "sum" 个变量求和并将其与主题名称一起显示在 JSON 中

bash script to sum number of "sum" variable and display it with the topic name in a JSON

我有此命令返回的 json:./kafka-log-dirs.sh --bootstrap-server localhost:9092 --describe

 {
  "version": 1,
  "brokers": [
    {
      "broker": 0,
      "logDirs": [
        {
          "logDir": "/var/lib/kafka/data/kafka-log0",
          "error": null,
          "partitions": [
            {
              "partition": "pfxdata-mutation-5",
              "size": 1326989,
              "offsetLag": 0,
              "isFuture": false
            },
            {
              "partition": "__consumer_offsets-13",
              "size": 0,
              "offsetLag": 0,
              "isFuture": false
            },
            {
              "partition": "events.oxf-5",
              "size": 279158658,
              "offsetLag": 0,
              "isFuture": false
            }
          ]
        }
      ]
    },
    {
      "broker": 1,
      "logDirs": [
        {
          "logDir": "/var/lib/kafka/data/kafka-log1",
          "error": null,
          "partitions": [
            {
              "partition": "__consumer_offsets-46",
              "size": 0,
              "offsetLag": 0,
              "isFuture": false
            },
            {
              "partition": "events.oxfcs-10",
              "size": 0,
              "offsetLag": 0,
              "isFuture": false
            }
          ]
        }
      ]
    }
  ]
}

我想执行命令或 bash 脚本来显示如下内容:

broker 0
Topics                  Size
pfxdata-mutation-5    1326989
__consumer_offsets-13    0
events.oxf-5          279158658
total size            280485647

broker1
Topics                  Size
__consumer_offsets-46    0
events.oxfcs-10          0 
total size               0

因此显示具有大小的分区,然后对所有分区求和,然后在终端上显示。 我对 bash 脚本编写非常陌生,所以我不知道如何开始这样做。 谢谢

试试这个脚本:

#!/bin/bash
temp=$(jq length sample.json)
len=$((temp-1))

x=0

while [ $x -le $len ]
        do
                partitions=$(jq ".brokers[${x}].logDirs[].partitions[]" sample.json)
                part_size=$(jq ".brokers[${x}].logDirs[].partitions" sample.json)
                total_size=$(echo $part_size | jq 'map(.size) | add')

                echo "broker ${x}"
                echo 'Topics    Size'
                echo $partitions | jq -r '"\(.partition)\t\(.size)"'
                echo "total size        ${total_size}"
                printf "\n"

                x=$(( $x + 1 ))
        done

使用您的文件替换 sample.json 文件

这是一个朴素的变体,只有 jq:

jq -r '.brokers[]
       |.broker,
        (.logDirs[].partitions as $parts
                                |($parts[]|[.partition,.size]),
                                 ["total size",([$parts[].size]|add)]
        |@tsv
        )' sample.json