Cluster(集群)
新建cluster
如下代码可以新建一个集群,集群包含1个master节点和2个core节点,且安装了Hive、Pig、HBase应用。请注意:参考下面样例代码时,需要修改相关的BOS路径为您的账户可用的BOS路径,包括withLogUri函数参数、HBase应用配置的withBackupLocation函数参数。
新建集群可以通过配置CreateClusterRequest对象的clientToken属性来保证创建请求的幂等性。clientToken是一个长度不超过64位的ASCII字符串,配置CreateClusterRequest对象的clientToken方法是:createClusterRequest.withClientToken(clientToken)
。
请求返回的CreateClusterResponse对象包含了新创建出集群的集群ID,获取方法为response.getClusterId()
。
1public void createCluster(BmrClient bmrClient) {
2 // 发送创建集群请求
3 String clusterId = null;
4 try {
5 CreateClusterResponse response = bmrClient.createCluster(
6 new CreateClusterRequest()
7 .withName("java-sdk-cluster")
8 .withImageType("hadoop")
9 .withImageVersion("0.1.0")
10 .withAutoTerminate(false)
11 .withLogUri("bos://path/to/logUri/")
12 .withServiceHaEnabled(false)
13 .withSafeModeEnabled(false)
14 .withInstanceGroup(new InstanceGroupConfig()
15 .withName("ig-master")
16 .withType("Master")
17 .withInstanceType("bmr.g1.2xlarge")
18 .withInstanceCount(1)
19 .withRootDiskSizeInGB(50)
20 .withRootDiskMediumType("ssd")
21 .withCds(new CdsItem()
22 .withSizeInGB(100)
23 .withMediumType("ssd")))
24 .withInstanceGroup(new InstanceGroupConfig()
25 .withName("ig-core")
26 .withType("Core")
27 .withInstanceType("bmr.gh1.large")
28 .withInstanceCount(2)
29 .withRootDiskSizeInGB(50)
30 .withRootDiskMediumType("ssd"))
31 .withApplication(new PigApplicationConfig().withVersion("0.11.0"))
32 .withApplication(new HiveApplicationConfig().withVersion("0.13.0").withMetastore("default"))
33 .withApplication(new HBaseApplicationConfig()
34 .withVersion("0.98.0")
35 .withBackupEnabled(true)
36 .withBackupLocation("bos://tester01/hbase_backup")
37 .withBackupIntervalInMinutes(300)
38 .withBackupStartDatetime("2015-08-18T23:00:00Z"))
39 .withStep(new JavaStepConfig()
40 .withName("init-step")
41 .withActionOnFailure("Continue")
42 .withJar("bos://bmr/samples/mapreduce/libs/hadoop-mapreduce-examples.jar")
43 .withMainClass("org.apache.hadoop.examples.WordCount")
44 .withArguments("bos://bmr/samples/mapreduce/wordcount/hamlet.txt bos://tester01/out"))
45 );
46 // 获取新创建集群的集群ID。
47 clusterId = response.getClusterId();
48 } catch (BceServiceException e) {
49 System.out.println("Create cluster failed: " + e.getErrorMessage());
50 } catch (BceClientException e) {
51 System.out.println(e.getMessage());
52 }
53}
售卖的套餐类型请参考https://cloud.baidu.com/doc/BMR/s/6jwvxw85z
列出全部cluster
如下代码可以罗列出属于请求调用者的所有集群,用户可以通过配置查询参数maxKeys来限制每次请求返回的集群数目,并且通过配置有效的查询参数marker来指定查询记录的起点。marker参数值是由BMR系统生成并返回的,因而初次查询请求中不需要配置该参数,它是在多次循环查询请求的后继请求中进行使用的。
1public void listClusters(BmrClient bmrClient) {
2 int maxKeys = 10;
3 try {
4 // 方法 1. 默认的查询请求
5 ListClustersResponse response1 = bmrClient.listClusters();
6
7 // 方法 2. 配置maxKeys的单次查询请求
8 ListClustersResponse response2 = bmrClient.listClusters(maxKeys);
9 // 方法 3. 配置maxKeys和marker参数的循环查询请求
10 boolean isTruncated = true;
11 int page = 0;
12 String marker = null;
13 while (isTruncated) {
14 ListClustersResponse response3 = bmrClient.listClusters(marker, maxKeys);
15 page++;
16 System.out.format("Page %d: cluster count: %d\n", page, response3.getClusters().size());
17 isTruncated = response3.isTruncated();
18 marker = response3.getNextMarker();
19 }
20
21 // 方法 4. 自定义ListClustersResquest对象的查询请求
22 ListClustersResponse response4 = bmrClient.listClusters(
23 new ListClustersRequest().withMaxKeys(maxKeys)
24 );
25
26 // 输出各个集群的ID
27 for (Cluster cluster : response4.getClusters()) {
28 System.out.format("cluster id: %s\n", cluster.getId());
29 }
30 } catch (BceServiceException e) {
31 System.out.println("List clusters failed: " + e.getErrorMessage());
32 }
33}
请求返回的ListClustersResponse对象包含了相关的集群对象数组List<Cluster>
, 获取集群对象数组的方法为response.getClusters()
。集群对象Cluster的属性包括了集群相关的配置信息,每个属性均有对应的getter访问器方法。
1public class Cluster {
2 private String payType; //集群支付方式
3 private boolean enableAutoScale; //集群是否支持自动扩缩容
4 private Date createTime; //集群创建时间
5 private List<Tag> tags; // 标签
6 private List<Application> applications; // 集群已安装的应用信息
7 private String id; // 集群ID
8 private String name; // 集群名称
9 private String imageType; // 集群虚拟机实例的镜像类型
10 private String imageVersion; // 集群虚拟机实例的镜像版本
11 private ClusterStatus status; // 集群当前的状态信息
12}
13
14public class ClusterStatus {
15private String code; // 集群创建失败的错误码
16private String message; // 集群创建失败的错误信息
17private String orderStatus; // 订单状态
18private Date expireDateTime; // 过期时间
19private int expireDates; // 过期日期
20
21 private String state; // 集群状态字段
22 private Date creationDateTime; // 集群创建时间
23 private Date endDateTime; // 集群终止时间
24 private Date readyDateTime; // 集群完成部署时间
25}
查询指定的cluster
获取了集群ID后,可用如下代码查询指定集群的信息。
请求返回的GetClusterResponse对象包含了获取集群属性的getter访问器方法,可以直接调用response的访问器方法来获得目标集群的属性信息。
1public void getCluster(BmrClient bmrClient, String clusterId) {
2 try {
3 // 方法 1. 查询对应ID的集群信息
4 GetClusterResponse response1 = bmrClient.getCluster(clusterId);
5
6 // 方法 2. 自定义GetClusterRequest对象的查询请求
7 GetClusterResponse response2 = bmrClient.getCluster(
8 new GetClusterRequest().withClusterId(clusterId)
9 );
10 // 输出集群的镜像类型
11 System.out.println(response1.getImageType());
12 // 输出集群当前的状态
13 System.out.println(response2.getStatus().getState());
14 } catch (BceServiceException e) {
15 System.out.println("Describe cluster failed: " + e.getErrorMessage());
16 }
17}
终止指定的cluster
如下代码可以终止指定的集群:
1public void terminateCluster(BmrClient bmrClient, String clusterId) {
2 try {
3 // 方法 1. 终止对应ID的集群
4 bmrClient.terminateCluster(clusterId);
5
6 /*
7 方法 2. 自定义TerminateClusterRequest对象的终止请求
8 bmrClient.terminateCluster(
9 new TerminateClusterRequest().withClusterId(clusterId)
10 );
11 */
12 } catch (BceServiceException e) {
13 System.out.println("Terminate cluster failed: " + e.getErrorMessage());
14 }
15}