开发者社区> 问答> 正文

AliyunCLI 过滤功能使用说明有哪些




AliyunCLI支持使用filter参数对指令返回的结果进行过滤输出。从开放API调用返回数据, 默认采用Json格式,所以用户在使用AliyunCLI时, 可以根据Json的特点, 使用过滤功能直接获取所需子节点数据。
下面举例说明:

原始数据:


# aliyuncli ecs DescribeImages   --ImageOwnerAlias system --output json --PageSize 1 --PageNumber 1
{
      "PageSize": 1,
      "RegionId": "cn-hangzhou",
      "TotalCount": 39,
      "PageNumber": 1,
      "RequestId":   "A0F91B21-BD8D-48D9-9EC8-36FB7E1D387E",
      "Images": {
          "Image": [
            {
                "Status":   "Available",
                "ProductCode":   "",
                "Platform":   "Freebsd",
                "Description":   "freebsd1001_64_20G_aliaegis_20150527.vhd",
                "IsCopied": false,
                "Tags": {
                    "Tag": []
                },
                "IsSubscribed":   false,
                "IsSelfShared":   "",
                "CreationTime":   "2015-06-19T10:45:56Z",
                "OSName":   "FreeBSD  10.1 64\u4f4d",
                  "DiskDeviceMappings": {
                    "DiskDeviceMapping":   [
                        {
                              "Format": "",
                              "ImportOSSBucket": "",
                              "Device": "/dev/xvda",
                              "SnapshotId": "",
                            "ImportOSSObject":   "",
                            "Size":   "20"
                        }
                    ]
                },
                "ImageId":   "freebsd1001_64_20G_aliaegis_20150527.vhd",
                "Usage":   "instance",
                  "ImageName":   "freebsd1001_64_20G_aliaegis_20150527.vhd",
                "Architecture":   "x86_64",
                "ImageOwnerAlias":   "system",
                "OSType":   "linux",
                "Progress":   "100%",
                "ImageVersion":   "1.0.0",
                "Size": 20
            }
          ]
      }
}


用法示例:
l   示例1:指定key值进行过滤,获取对应value
如果通过filter传入的key值是最末节点,则会返回其对应的value。
n   过滤方法示例
只过滤输出返回结果中的TotalCount字段对应值:

# aliyuncli ecs DescribeImages   --ImageOwnerAlias system --output json --PageSize 1 --PageNumber 1 --filter   TotalCount
39


l   示例2:指定key值进行过滤,获取所有子节点数据
如果通过filter传入的key值非最末节点,则会返回其下所有子节点数据。
n   过滤方法示例
过滤输出返回结果中所有Image相关信息:

# aliyuncli ecs DescribeImages   --ImageOwnerAlias system --output json --PageSize 1 --PageNumber 1   --filter Images
{
      "Image": [
          {
            "Status":   "Available",
            "ProductCode":   "",
            "Platform":   "Freebsd",
            "Description":   "freebsd1001_64_20G_aliaegis_20150527.vhd",
            "IsCopied": false,
            "Tags": {
                "Tag": []
            },
            "IsSubscribed": false,
            "IsSelfShared":   "",
            "CreationTime":   "2015-06-19T10:45:56Z",
            "OSName":   "FreeBSD  10.1 64\u4f4d",
            "DiskDeviceMappings": {
                  "DiskDeviceMapping": [
                    {
                        "Format":   "",
                          "ImportOSSBucket": "",
                        "Device":   "/dev/xvda",
                          "SnapshotId": "",
                        "ImportOSSObject":   "",
                        "Size":   "20"
                    }
                ]
            },
            "ImageId":   "freebsd1001_64_20G_aliaegis_20150527.vhd",
            "Usage":   "instance",
            "ImageName":   "freebsd1001_64_20G_aliaegis_20150527.vhd",
            "Architecture":   "x86_64",
            "ImageOwnerAlias":   "system",
            "OSType":   "linux",
            "Progress":   "100%",
            "ImageVersion":   "1.0.0",
            "Size": 20
          }
      ]
}


l    示例3:通过数组下标获取返回数组(array)中指定子项值
如果返回结果中包含数组(array)数据,则可以通过filter传入指定序列编号获取相应子项值。
注意:数组下标从0开始计数。
n   过滤方法示例
过滤输出返回结果中第1个Image的所有相关信息:

# aliyuncli ecs DescribeImages   --ImageOwnerAlias system --output json --PageSize 1 --PageNumber 1   --filter Images.Image[0]
{
      "Status": "Available",
      "ProductCode": "",
      "Platform": "Freebsd",
      "Description":   "freebsd1001_64_20G_aliaegis_20150527.vhd",
      "IsCopied": false,
      "Tags": {
          "Tag": []
      },
      "IsSubscribed": false,
      "IsSelfShared": "",
      "CreationTime": "2015-06-19T10:45:56Z",
      "OSName": "FreeBSD    10.1 64\u4f4d",
      "DiskDeviceMappings": {
          "DiskDeviceMapping": [
            {
                "Format":   "",
                "ImportOSSBucket": "",  
                "Device":   "/dev/xvda",
                "SnapshotId":   "",
                "ImportOSSObject":   "",
                "Size":   "20"
            }
          ]
      },
      "ImageId": "freebsd1001_64_20G_aliaegis_20150527.vhd",  
      "Usage": "instance",
      "ImageName":   "freebsd1001_64_20G_aliaegis_20150527.vhd",
      "Architecture": "x86_64",
      "ImageOwnerAlias": "system",
      "OSType": "linux",
      "Progress": "100%",
      "ImageVersion": "1.0.0",
      "Size": 20
}


l   示例4:通过数组下标获取返回数组(array)子项下指定字段对应值
如果返回结果中包含数组(array)数据,则可以通过filter传入指定序列编号和所需字段,获取相应子项下所需字段值。
注意:数组下标从0开始计数。
n   过滤方法示例
过滤输出返回结果中第1个Image的ImageId值:

# aliyuncli ecs DescribeImages   --ImageOwnerAlias system --output json --PageSize 1 --PageNumber 1   --filter Images.Image[0].ImageId
"freebsd1001_64_20G_aliaegis_20150527.vhd"


l   示例5:通过*号获取返回数组(array)中所有子项下指定字段对应值
如果返回结果中包含数组(array)数据,则可以通过filter传入*号和所需字段,获取所有子项下所需字段值。
n   过滤方法示例
过滤输出返回结果中所有ImageId值:

# aliyuncli ecs DescribeImages   --ImageOwnerAlias system --output json --PageSize 1 --PageNumber 1   --filter Images.Image[*].ImageId
[
      "freebsd1001_64_20G_aliaegis_20150527.vhd"
]


l   示例6:嵌套过滤
如果返回结果中包含的数组(array)数据的子节点也是数组,则可以通过嵌套过滤获取所需子项下的所需字段值。
n   过滤方法示例
过滤输出返回结果中指定编号Image的所生成磁盘的Device信息:

# aliyuncli ecs DescribeImages   --ImageOwnerAlias system --output json --PageSize 1 --PageNumber 1 --filter   Images.Image[0].DiskDeviceMappings.DiskDeviceMapping[0].Device
"/dev/xvda"


如问题还未解决,请联系 售后技术支持

展开
收起
boxti 2017-10-23 10:43:23 2101 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
JCLI使用说明文档 立即下载
MaxCompute Logview参数详解和问题排查(废弃) 立即下载
MaxCompute Logview参数详解和问题排查 立即下载