JMES 路径:从 AWS CLI 结果中排除前 5 条记录

JMES Path: Exclude top 5 records from AWS CLI results

我想使用 AWS CLI 获取 RDS 实例的可用快照列表。

我可以使用此查询在创建快照时按降序排列所有快照:

aws rds describe-db-snapshots --db-instance-identifier sample-db \
 --query="(reverse(sort_by(DBSnapshots[?SnapshotType=='manual'], &SnapshotCreateTime)))[*].{DBSnapshotIdentifier: DBSnapshotIdentifier, DBSnapshotARN: DBSnapshotArn, SnapshotCreateTime: SnapshotCreateTime}" \
 --no-paginate --output json

我还可以通过将 [*] 替换为 [:5]:

从此查询中获取 前 5 条记录
aws rds describe-db-snapshots --db-instance-identifier sample-db \
 --query="(reverse(sort_by(DBSnapshots[?SnapshotType=='manual'], &SnapshotCreateTime)))[:5].{DBSnapshotIdentifier: DBSnapshotIdentifier, DBSnapshotARN: DBSnapshotArn, SnapshotCreateTime: SnapshotCreateTime}" \
 --no-paginate --output json

但是,我想获取所有记录不包括前 5。我将不胜感激 JMES Path 上关于我如何实现这一目标的任何意见。

您想要实现的目标称为 slicing,而您实际上已经非常接近解决方案了。

JMESPath 的切片语法是从 Python 世界借用的,如下所示:

This syntax introduces Python style array slicing that allows a start position, stop position, and step. This syntax also proposes following the same semantics as python slices.

[start:stop:step]

Each part of the expression is optional. You can omit the start position, stop position, or step. No more than three values can be provided in a slice expression.

The step value determines how my indices to skip after each element is plucked from the array. A step of 1 (the default step) will not skip any indices. A step value of 2 will skip every other index while plucking values from an array. A step value of -1 will extract values in reverse order from the array. A step value of -2 will extract values in reverse order from the array while, skipping every other index.

来源:https://jmespath.org/proposals/array-slices.html#syntax

因此您的查询应显示为 [5:],以表明您希望切片在第五个元素之后开始,并以:

结尾
aws rds describe-db-snapshots --db-instance-identifier sample-db \
 --query="(reverse(sort_by(DBSnapshots[?SnapshotType=='manual'], &SnapshotCreateTime)))[5:].{DBSnapshotIdentifier: DBSnapshotIdentifier, DBSnapshotARN: DBSnapshotArn, SnapshotCreateTime: SnapshotCreateTime}" \
 --no-paginate --output json