要想实现CDH自动化部署,第一步就是创建集群(Cluster),这篇文章讲的就是如何通过cm java api创建一个cluster。
第一步,先要配置好POM文件,引入CM API
<!-- CDH官方的REPO -->
<repositories>
<repository>
<id>cdh.repo</id>
<url>https://repository.cloudera.com/content/groups/cloudera-repos</url>
<name>Cloudera Repository</name>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>cloudera-releases</id>
<url>https://repository.cloudera.com/artifactory/libs-release-local</url>
<name>Cloudera Releases Repository</name>
</repository>
<snapshotRepository>
<id>cloudera-snapshots</id>
<url>https://repository.cloudera.com/artifactory/libs-snapshot-local</url>
<name>Cloudera Snapshots Repository</name>
</snapshotRepository>
</distributionManagement>
我们的CDH版本是5.11.0,根据不同的CDH版本匹配CM API 的版本,这块一定要对应起来,避免后面出现不必要的错误
<!-- cm.version 5.11.0 -->
<dependency>
<groupId>com.cloudera.api</groupId>
<artifactId>cloudera-manager-api</artifactId>
<version>${cm.version}</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</exclusion>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
第二步,创建集群
String host = "CDH MANAGER IP";
int port = 7180;
String user = "admin";
String password = "admin";
String clusterName = "automation-demo-cluster";
//获得api
ApiRootResource apiResource = new ClouderaManagerClientBuilder().withHost(host).withPort(port).withUsernamePassword(user, password).build();
//根资源
RootResourceV16 apiRoot = apiResource.getRootV16();
//集群资源管理
ClustersResourceV16 clustersResource = apiRoot.getClustersResource();
ApiClusterList apiClusterList = new ApiClusterList();
ApiCluster cluster = new ApiCluster();
//设置集群的名称
cluster.setName(clusterName);
//设置集群的版本
cluster.setVersion(ApiClusterVersion.CDH5);
apiClusterList.add(cluster);
//创建集群
clustersResource.createClusters(apiClusterList);
登录CDH管理页面查看集群
1542961262(1).jpg
查看集群和删除集群
//查看集群
ApiCluster apiCluster = clustersResource.readCluster(clusterName);
System.out.println("Cluster name is " + apiCluster.getName() + ", version is " + apiCluster.getVersion());
//输出结果
//Cluster name is automation-demo-cluster, version is CDH5
//删除集群
clustersResource.deleteCluster(clusterName);