Cluster
Create Clusters
The following code is used to create a cluster, which contains one master node and two core codes and has the Hive, Pig, and HBase applications installed. Notes: When referring to the following sample code, you need to modify the BOS path to make it suitable for your account, including the withLogUri function parameter and the withBackupLocation function parameter for HBase application.
When creating a cluster, you can ensure the idempotence of creating the request through the configuration of clientToken properties of the CreateClusterRequest object. The clientToken is an ASCII string with a length not exceeding 64 bits, and the configuration of clientToken for CreateClusterRequest object is createClusterRequest.withClientToken(clientToken)
.
The CreateClusterResponse object returned under request contains the new cluster ID and is obtained via response.getClusterId()
.
public void createCluster(BmrClient bmrClient) {
// send request for creating a cluster
String clusterId = null;
try {
CreateClusterResponse response = bmrClient.createCluster(
new CreateClusterRequest()
.withName("java-sdk-cluster")
.withImageType("hadoop")
.withImageVersion("0.1.0")
.withAutoTerminate(false)
.withLogUri("bos://path/to/logUri/")
.withServiceHaEnabled(false)
.withSafeModeEnabled(false)
.withInstanceGroup(new InstanceGroupConfig()
.withName("ig-master")
.withType("Master")
.withInstanceType("bmr.g1.2xlarge")
.withInstanceCount(1))
.withRootDiskSizeInGB(50)
.withRootDiskMediumType("ssd")
.withCds(new CdsItem()
.withSizeInGB(100)
.withMediumType(“ssd”));
.withInstanceGroup(new InstanceGroupConfig()
.withName("ig-core")
.withType("Core")
.withInstanceType("bmr.gh1.large")
.withInstanceCount(2))
.withRootDiskSizeInGB(50)
.withRootDiskMediumType("ssd")
.withApplication(new PigApplicationConfig().withVersion("0.11.0"))
.withApplication(new HiveApplicationConfig().withVersion("0.13.0").withMetastore("default"))
.withApplication(new HBaseApplicationConfig()
.withVersion("0.98.0")
.withBackupEnabled(true)
.withBackupLocation("bos://tester01/hbase_backup")
.withBackupIntervalInMinutes(300)
.withBackupStartDatetime("2015-08-18T23:00:00Z"))
.withStep(new JavaStepConfig()
.withName("init-step")
.withActionOnFailure("Continue")
.withJar("bos://bmr/samples/mapreduce/libs/hadoop-mapreduce-examples.jar")
.withMainClass("org.apache.hadoop.examples.WordCount")
.withArguments("bos://bmr/samples/mapreduce/wordcount/hamlet.txt bos://tester01/out"))
);
// obtain the ID of the new cluster.
clusterId = response.getClusterId();
} catch (BceServiceException e) {
System.out.println("Create cluster failed: " + e.getErrorMessage());
} catch (BceClientException e) {
System.out.println(e.getMessage());
}
}
Please refer to https://cloud.baidu.com/doc/BMR/s/6jwvxw85z.
List All Clusters
The following code is used to list all clusters of the requesting caller. The query parameter maxKeys is used to restrict the number of clusters returned under every request, and the valid query parameter marker is used to specify the initial position of query records. The marker parameter value is generated and returned by the BMR system. Instead of configuration in the initial request, such a parameter is subsequently used in the circular query requests.
public void listClusters(BmrClient bmrClient) {
int maxKeys = 10;
try {
// method 1. default query request
ListClustersResponse response1 = bmrClient.listClusters();
// method 2. single query request with configuration of maxKeys
ListClustersResponse response2 = bmrClient.listClusters(maxKeys);
// method 3. circular query requests with configuration of maxKeys and marker
boolean isTruncated = true;
int page = 0;
String marker = null;
while (isTruncated) {
ListClustersResponse response3 = bmrClient.listClusters(marker, maxKeys);
page++;
System.out.format("Page %d: cluster count: %d\n", page, response3.getClusters().size());
isTruncated = response3.isTruncated();
marker = response3.getNextMarker();
}
// method 4. query request with customization of ListClustersResquest object
ListClustersResponse response4 = bmrClient.listClusters(
new ListClustersRequest().withMaxKeys(maxKeys)
);
// output of cluster IDs
for (Cluster cluster : response4.getClusters()) {
System.out.format("cluster id: %s\n", cluster.getId());
}
} catch (BceServiceException e) {
System.out.println("List clusters failed: " + e.getErrorMessage());
}
}
The ListClustersResponse object returned under request contains the cluster object array List<Cluster>
, and the cluster object array is obtained via response.getClusters()
. The properties of the cluster object (i.e., Cluster) include the cluster configuration information, and every property has its getter accessor method.
public class Cluster {
private String id; // cluster ID
private String name; // cluster name
private String imageType; // image type of virtual machine instances for cluster
private String imageVersion; // image version of virtual machine instances for cluster
private String logUri; // BOS path for storage of cluster operating logs
private boolean autoTerminate; // position of boolean marker for automatic termination of cluster
private List<Application> applications; // information on installed applications of the cluster
private ClusterStatus status; // information on current status of the cluster
private boolean serviceHaEnabled; // whether the cluster enables high availability
private boolean safeModeEnabled; // whether the cluster enables security mode
}
public class ClusterStatus {
private String state; // cluster status field
private Date creationDateTime; // cluster creation time
private Date endDateTime; // cluster termination time
private Date readyDateTime; // cluster deployment completion time
}
Query Specified Clusters
After obtaining the cluster ID, you can use the following code to query the information on the specified cluster.
The GetClusterResponse object returned under request contains the getter accessor method to obtain cluster properties, and the information on the target cluster’s properties is obtained by directly calling the response accessor.
public void getCluster(BmrClient bmrClient, String clusterId) {
try {
// method 1. query cluster information based on ID
GetClusterResponse response1 = bmrClient.getCluster(clusterId);
// method 2. query request with customization of GetClusterRequest object
GetClusterResponse response2 = bmrClient.getCluster(
new GetClusterRequest().withClusterId(clusterId)
);
// image type of output cluster
System.out.println(response1.getImageType());
// current status of output cluster
System.out.println(response2.getStatus().getState());
} catch (BceServiceException e) {
System.out.println("Describe cluster failed: " + e.getErrorMessage());
}
}
Terminate Specified Clusters
The following code is used to terminate the specified cluster:
public void terminateCluster(BmrClient bmrClient, String clusterId) {
try {
// method 1. termination of cluster based on ID
bmrClient.terminateCluster(clusterId);
/*
method 2. termination request with customization of TerminateClusterRequest object
bmrClient.terminateCluster(
new TerminateClusterRequest().withClusterId(clusterId)
);
*/
} catch (BceServiceException e) {
System.out.println("Terminate cluster failed: " + e.getErrorMessage());
}
}