【问题标题】:How to sort_by to get latest object using jq如何 sort_by 使用 jq 获取最新对象
【发布时间】:2019-11-07 16:29:53
【问题描述】:

所以我运行这个:

aws ec2 describe-snapshots \
    --region xxxxxxxxx \
    --owner-ids xxxxxxxxxxxx \
    | jq '.Snapshots[] | select(.VolumeId=="vol-xxxxxxxxxxxxx")'

我得到了这个https://jqplay.org/s/SnT8lDwycY

如何按StartTime 排序并返回最新的"SnapshotId": "snap-xxxxxxxxxxxxxxxx"

当我添加| sort_by(.StartDate)' 时出现以下错误:

jq: error (at <stdin>:22106): Cannot index string with string "StartDate"

【问题讨论】:

    标签: json amazon-web-services amazon-ec2 jq


    【解决方案1】:

    jqplay 上显示的数据是 JSON 对象流。处理这种流的一种方法是使用 jq 的 -s 命令行选项。这实际上将它们组合成一个列表,然后可以对其进行排序。

    所以如果你不关心领带,一个简单的解决方法是:

    sort_by(.StartTime) | .[-1] | .SnapshotId
    

    如果您的 jq 足够最新,则可以缩写为:

    sort_by(.StartTime)[-1].SnapshotId
    

    【讨论】:

    • 你知道如何将它映射或转置为数组吗?
    • 你指的是什么“这个”?这里的“转置”是什么意思?如果您只想将结果作为一个数组,那么您可以将整个 jq 表达式括在方括号中。如果这不是您的想法,那么也许您应该问一个单独的 SO Q?
    【解决方案2】:

    以下是一些如何使用内置--query 选项的示例。每个示例都建立在前一个示例的基础上。

    获取所有快照,按开始时间升序排序:

    aws ec2 describe-snapshots \
        --owner-ids xxxxxxxxxxxx \
        --query 'sort_by(Snapshots, &StartTime)'
    

    获取最旧的快照:

    aws ec2 describe-snapshots \
        --owner-ids xxxxxxxxxxxx \
        --query 'sort_by(Snapshots, &StartTime)[0]'
    

    获取最新快照:

    aws ec2 describe-snapshots \
        --owner-ids xxxxxxxxxxxx \
        --query 'sort_by(Snapshots, &StartTime)[-1]'
    
    # Example of output:
    {
        "Description": "End of Q3 snapshot",
        "Encrypted": false,
        "OwnerId": "xxxxxxxxxxxx",
        "Progress": "100%",
        "SnapshotId": "snap-0f601234abcd12345",
        "StartTime": "2019-09-30T23:59:59.999Z",
        "State": "completed",
        "VolumeId": "vol-0f123dc5b1dd911d3",
        "VolumeSize": 8
    }
    

    获取最新快照的 ID:

    aws ec2 describe-snapshots \
        --owner-ids xxxxxxxxxxxx \
        --query 'sort_by(Snapshots, &StartTime)[-1].SnapshotId'
    
    # Example of output:
    "snap-0f601234abcd12345"
    

    以纯文本形式获取最新快照的 ID:

    aws ec2 describe-snapshots \
        --owner-ids xxxxxxxxxxxx \
        --query 'sort_by(Snapshots, &StartTime)[-1].SnapshotId' \
        --output text
    
    # Example of output:
    snap-0f601234abcd12345
    

    如果你更喜欢使用jq,你可以获得如下相同的结果(如@peak所示):

    aws ec2 describe-snapshots \
        --owner-ids xxxxxxxxxxxx \
        | jq -r '.Snapshots | sort_by(.StartTime)[-1].SnapshotId'
    

    【讨论】:

    • 谢谢,我今天试试,回来报告。
    • 根据您的示例和 aws 文档,我想出了这个。谢谢,更好的答案。 aws --region xx-xxxx-x ec2 describe-snapshots --owner-ids xxxxxxxxxxxx --filters "Name=volume-id, Values=vol-xxxxxxxxxx" --query 'sort_by(Snapshots, &amp;StartTime)[-1].SnapshotId' --output text
    【解决方案3】:

    想通了……

    aws ec2 describe-snapshots \
        --region xx-xxxx-x  \
        --owner-ids xxxxxxxxxx \
        | jq -r '.Snapshots[] | select(.VolumeId=="vol-xxxxxxxxxxxxxxx") \
        | [(.StartTime | split(", ")), (.SnapshotId | split(", "))] \
        | transpose \
        | { Snapshots: map({ StartTime:.[0], SnapshotId:.[1] }) } \
        | .Snapshots \
        | sort_by(.StartTime) \
        | grep SnapshotId -m 1 \
        | cut -d : -f 2 | sed 's/"//g''
    

    【讨论】:

    • 这看起来非常复杂和脆弱。
    • 我知道,但我不太擅长 jq,所以欢迎任何反馈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2023-02-23
    • 1970-01-01
    • 2018-08-10
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多