All Products
Search
Document Center

Alibaba Cloud CLI:Aggregate the results of paging operations

Last Updated:Jul 24, 2024

When you use Alibaba Cloud CLI to call paging operations of Alibaba Cloud services, only the results on the first page are returned by default. To obtain all results, you can use Alibaba Cloud CLI to aggregate the results.

Description of the --pager option

You can use --pager option to aggregate the results of the ARRAY type of a paging operation. The following table describes the parameters contained in the --pager option:

Parameter

Description

PageNumber

Corresponds to the parameter that indicates the page number in the response of a paging operation. Default value: PageNumber.

PageSize

Corresponds to the parameter that indicates the maximum number of results per page in the response of a paging operation. Default value: PageSize.

TotalCount

Corresponds to the parameter that indicates the total number of results in the response of a paging operation. Default value: TotalCount.

path

You can manually specify the JMESPath path of the data of the ARRAY type to be aggregated due to the diversity of the results of API operations.

Note

By default, the --pager option automatically identifies the data of the ARRAY type in the result.

Scenario

  1. The DescribeInstances operation of Elastic Compute Service (ECS) is a paging operation. Run the following command. By default, only the results on the first page of the instance list are returned.

    aliyun ecs DescribeInstances
  2. Sample response (partially truncated):

    {
        "PageNumber": 1,
        "TotalCount": 4,
        "PageSize": 10,
        "RequestId": "6EA82E70-9750-4A97-A738-E021D8A57F07",
        "Instances": {
            "Instance": [
                {
                    "ImageId": "win2012r2_64_dtc_9600_zh-cn_40G_alibase_20190318.vhd",
                    "SerialNumber": "f06857e8-7f3c-443a-9f88-8e84eb5163c7",
                    "Cpu": 1,
                    "Memory": 2048,
                    "DeviceAvailable": true,
                    "SecurityGroupIds": {
                        "SecurityGroupId": [
                            "sg-bp1fgviwol82z8ap348n"
                        ]
                    }
                }
            ]
        }
    }

Sample commands

  1. Run the following command to return the instance information on all pages:

    aliyun ecs DescribeInstances --pager PageNumber=PageNumber PageSize=PageSize TotalCount=TotalCount path=Instances.Instance

    The response parameters of this operation are the same as the default values of the parameters contained in the --pager option. Therefore, you can omit the parameters in the --pager option when you use the --pager option. Simplified command:

    aliyun ecs DescribeInstances --pager
  2. Sample output (partially truncated):

    Note

    The aggregated output contains only aggregated parameters. If you use the filtering feature to extract the parameters that you need, the path of the extracted parameter is the JMESPath path after the aggregation. For more information, see Extract parameters and tabulate output.

    {
        "Instances": {
            "Instance": [
                {
                    "ImageId": "win2012r2_64_dtc_9600_zh-cn_40G_alibase_20190318.vhd",
                    "SerialNumber": "f06857e8-7f3c-443a-9f88-8e84eb5163c7",
                    "Cpu": 1,
                    "Memory": 2048,
                    "DeviceAvailable": true,
                    "SecurityGroupIds": {
                        "SecurityGroupId": [
                            "sg-bp1fgviwol82z8ap348n"
                        ]
                    }
                }
            ]
        }
    }