如何在AWS CLI中筛选多个标识符值

huangapple go评论73阅读模式
英文:

How to filter for multiple identifier values in AWS CLI

问题

在AWS CLI中筛选多个标识值时,使用或不使用句点结果相同。句点是否可选是一个问题。

官方文档没有提到可以省略句点。

以下是两个查询的输出结果将相同:

aws ec2 describe-instances \ 
  --query 'Reservations[].Instances[].[InstanceId,InstanceType]'

返回

[
    [
        "i-0f74894a273e9a76f",
        "t2.micro"
    ],
    [
        "i-007598b789e08ca19",
        "t2.micro"
    ],
    [
        "i-0e2defdabf5771dfa",
        "t2.micro"
    ],
    [
        "i-07db1ecca76653ae5",
        "t2.micro"
    ]
]

aws ec2 describe-instances \
  --query 'Reservations[].Instances[][InstanceId,InstanceType]'

返回

[
    [
        "i-0f74894a273e9a76f",
        "t2.micro"
    ],
    [
        "i-007598b789e08ca19",
        "t2.micro"
    ],
    [
        "i-0e2defdabf5771dfa",
        "t2.micro"
    ],
    [
        "i-07db1ecca76653ae5",
        "t2.micro"
    ]
]

为什么会这样?

英文:

When filtering multiple identifier values in the AWS CLI, the results are the same with or without a period. Is the period optional?

The official references does not mention that it can be omitted.

The output of the following two queries will be the same:

aws ec2 describe-instances \ 
  --query 'Reservations[].Instances[].[InstanceId,InstanceType]'

Returns

[
    [
        "i-0f74894a273e9a76f",
        "t2.micro"
    ],
    [
        "i-007598b789e08ca19",
        "t2.micro"
    ],
    [
        "i-0e2defdabf5771dfa",
        "t2.micro"
    ],
    [
        "i-07db1ecca76653ae5",
        "t2.micro"
    ]
]

And

aws ec2 describe-instances \
  --query 'Reservations[].Instances[][InstanceId,InstanceType]'

Returns

[
    [
        "i-0f74894a273e9a76f",
        "t2.micro"
    ],
    [
        "i-007598b789e08ca19",
        "t2.micro"
    ],
    [
        "i-0e2defdabf5771dfa",
        "t2.micro"
    ],
    [
        "i-07db1ecca76653ae5",
        "t2.micro"
    ]
]

Why is that?

答案1

得分: 3

两者等价是因为Reservations[].Instances[]的结果是一个JSON数组。因此,您可以对该数组执行Multiselect操作,例如 [].[InstanceId,InstanceType] 或对数组本身的字段进行projection操作,比如[][InstanceId,InstanceType]

因此,在一个简单的JSON中:

{
  "foo": [{
    "bar": "baz",
    "qux": "quux"
  }]
}

这些表达式:

  • foo[].[bar, qux]
    
  • foo[][bar, qux]
    

是相同的,并且都产生如下结果:

[
  [
    "baz",
    "quux"
  ]
]

也就是说,你应该偏向于使用第一种形式 foo[].[bar, qux],不仅因为这是教程中使用的语法,而且它在所有情况下都有效,包括父JSON节点是对象而不是数组的情况。

以下是一个示例,展示了两种语法的差异,给定以下JSON:

{
  "foo": {
    "bar": "baz",
    "qux": "quux"
  }
}
  • 表达式:
    foo.[bar, qux]
    

    会按预期工作,并返回

    [
      "baz",
      "quux"
    ]
    
  • foo[bar, qux]
    

    将导致错误并返回空值。

英文:

The two are equivalent because the result of Reservations[].Instances[] is a JSON array.
So you can either do a Multiselect on top of this array [].[InstanceId,InstanceType] or do a projection of the fields in the array itself [][InstanceId,InstanceType].

So, indeed on a simple JSON:

{
  "foo": [{
    "bar": "baz",
    "qux": "quux"
  }]
}

These expressions:

  • foo[].[bar, qux]
    
  • foo[][bar, qux]
    

are the same and both yields

[
  [
    "baz",
    "quux"
  ]
]

This said, you should probably favour the first form foo[].[bar, qux], not only because this is the syntax used in the tutorials, but also because it will work in all the situation, so, also, if the parent JSON node is an object and not an array.

Here is a example where the two syntax are not identical, given the JSON:

{
  "foo": {
    "bar": "baz",
    "qux": "quux"
  }
}
  • The expression:
    foo.[bar, qux]
    

    will work as expected and return

    [
      "baz",
      "quux"
    ]
    
  • While
    foo[bar, qux]
    

    will just error out and gives you nothing in return.

huangapple
  • 本文由 发表于 2023年2月10日 03:23:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定