获取集群列表

接口

接口名称:listClusters

参数

参数

类型

是否必选

说明

marker

String

本页起始资源标识符。默认为空字符串。

maxItemCount

int

返回条数,最大取值200,默认200

返回值

list_clusters方法返回一个 ListResponse 对象。返回样式如下:

{
    "Date": "Wed, 07 Nov 2018 10:20:48 GMT", 
    "Items": [
    ], 
    "NextMarker": "cls-6ki3sg6sqlno7nt8fu000m", 
    "RequestId": "ACBB5876-888B-4336-AA72-BD532FE0F0F0", 
    "StatusCode": 200
}
说明

篇幅有限,我们已把 Items 内容省去。

例子

下面例子遍历出所有的集群,并把集群 ID和状态打印出来,并统计出总共的数量。

Java 源码:

import java.util.List;
import com.aliyuncs.batchcompute.main.v20151111.*;
import com.aliyuncs.batchcompute.model.v20151111.*;
import com.aliyuncs.batchcompute.pojo.v20151111.*;
import com.aliyuncs.exceptions.ClientException;

public class ListClusters {
    static String ACCESS_KEY_ID = "xxx";  //这里填写您的 AccessKeyId
    static String ACCESS_KEY_SECRET = "xxx"; //这里填写您的 AccessKeySecret
    static String REGION_ID = "cn-xxx";   //这里填写 region

    public static void main(String[] args) {
        BatchCompute client = new BatchComputeClient(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        try{
            ListClustersResponse response = client.listClusters();
            //成功
            List<Cluster> list = response.getItems();

            for (int i = 0; i < list.size(); i++){
                Cluster clster = list.get(i);
                String state = clster.getState();
                String name = clster.getName();

                System.out.println("{");
                System.out.println("\tId:" + clster.getId());
                System.out.println("\tName:" + name);
                System.out.println("\tState:" + state);
                System.out.println("},");
            }
        }catch(ClientException e){
            e.printStackTrace();
            //失败
        }
    }
}

执行结果

[
  {
    Id:cls-6ki3npokblk1091cbko00f
    Name:test-centos-vpc
    State:Deleting
  },
  {
    Id:cls-6ki3npokblk1091cbko00i
    Name:test-ubuntu-vpc
    State:Deleting
  },
  {
    Id:cls-6ki3npokblk1091cbko00k
    Name:test-windows-vpc
    State:Deleting
  },
  {
    Id:cls-6kie8e833lnov8gg50k00j
    Name:cluster_test
    State:Active
  },
  {
    Id:cls-6kildmf3sllvkra43i2007
    Name:Cluster_Test
    State:Active
  }
]