繁体   English   中英

如何使用 jq 获得每个组和子组的最大值

[英]how to get a max per group and subgroup using jq

jq 是一个了不起的工具,它做了很多。 作为输入我有

[
  {
    "backup": [
      {
        "timestamp": { "start": 1642144383, "stop": 1642144386 },
        "info": {  "size": 1200934840},
        "type": "full"
      },
      {
        "timestamp": {"start": 1642144388, "stop":  1642144392 },
        "info": { "size": 1168586300
        },
        "type": "incr"
      },
      {
        "timestamp": {"start": 1642145388, "stop":  1642145392 },
        "info": { "size": 1168586330
        },
        "type": "incr"
      }
    ],
    "name": "dbname1"
  },
  {
    "backup": [
      {
        "timestamp": { "start": 1642144383, "stop": 1642144386 },
        "info": {  "size": 1200934840},
        "type": "full"
      },
      {
        "timestamp": {"start": 1642144388, "stop":  1642144392 },
        "info": { "size": 1168586300
        },
        "type": "incr"
      }
    ],
    "name": "dbname2"
  }
]

并使用

jq 'map([.backup[] + {name}] | max_by(.timestamp.stop))'

我得到了最新的 timestamp.stop 名称。 我应该如何更改它以获取名称和组的最新 timestamp.stop? 在 SQL 这将类似于 max(.timestamp.stop) group by.name,.type 希望 output 像:

[
  {
    "timestamp": {
      "start": 1642144383,
      "stop": 1642144386
    },
    "info": {
      "size": 1200934840
    },
    "type": "full",
    "name": "dbname1"
  },
  {
    "timestamp": {
      "start": 1642145388,
      "stop": 1642145392
    },
    "info": {
      "size": 1168586330
    },
    "type": "incr",
    "name": "dbname1"
  },
  {
    "timestamp": {
      "start": 1642144383,
      "stop": 1642144386
    },
    "info": {
      "size": 1200934840
    },
    "type": "full",
    "name": "dbname2"
  },
  {
    "timestamp": {
      "start": 1642144388,
      "stop": 1642144392
    },
    "info": {
      "size": 1168586300
    },
    "type": "incr",
    "name": "dbname2"
  }
]

删除内括号以展平数组,然后group_by两个条件(这使您的条件成为一个数组)和map您的max_by到结果数组:

jq 'map(.backup[] + {name}) | group_by([.name, .type]) | map(max_by(.timestamp.stop))'
[
  {
    "timestamp": {
      "start": 1642144383,
      "stop": 1642144386
    },
    "info": {
      "size": 1200934840
    },
    "type": "full",
    "name": "dbname1"
  },
  {
    "timestamp": {
      "start": 1642145388,
      "stop": 1642145392
    },
    "info": {
      "size": 1168586330
    },
    "type": "incr",
    "name": "dbname1"
  },
  {
    "timestamp": {
      "start": 1642144383,
      "stop": 1642144386
    },
    "info": {
      "size": 1200934840
    },
    "type": "full",
    "name": "dbname2"
  },
  {
    "timestamp": {
      "start": 1642144388,
      "stop": 1642144392
    },
    "info": {
      "size": 1168586300
    },
    "type": "incr",
    "name": "dbname2"
  }
]

演示

这似乎产生了预期的 output。 在执行max_by之前,您需要按.type记录进行额外分组

map( .backup[] + {name} ) | group_by(.name)[] | 
  group_by(.type) | map(max_by(.timestamp.stop))

jqplay演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM