ClouderaManager java api 有关查询api 使用。

首先pom中加上此依赖:

    <dependency>
       <groupId>com.cloudera.apigroupId>
       <artifactId>cloudera-manager-apiartifactId>
       <version>5.14.2version>

    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-classicartifactId>
      <version>1.0.6version>
    dependency>

    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.8version>
    dependency>
 dependency>

获取cloudera-manager中所有集群的信息:

public class ClouderaManagerCluster {
    static RootResourceV18 apiRoot;

    static {
        apiRoot = new ClouderaManagerClientBuilder().withHost(666.666.666.666).
                withPort(Integer.valueOf(7180))
                .withUsernamePassword(admin, admin).build().getRootV18();
    }

    private final static Logger LOGGER = LoggerFactory.getLogger(ClouderaManagerHost.class);


    public static void getAllCluster(){
        LOGGER.info("开始测试的时间为{},**************开始测试获取ClouderaManager集群信息**************",Utils.getCurrentTime());
        ApiClusterList apiClusterList = apiRoot.getClustersResource().readClusters(DataView.FULL);
        LOGGER.info("ClouderaManager 共管理了{}个集群",apiClusterList.getClusters().size());
        for(ApiCluster apiCluster : apiClusterList){
            ApiCluster apiCluster1 = apiRoot.getClustersResource().readCluster(apiCluster.getName());
            LOGGER.info("集群名称 {}",apiCluster1.getName());
            LOGGER.info("集群显示名称 {}",apiCluster1.getDisplayName());
            LOGGER.info("CDH 版本:{}-{}",apiCluster1.getVersion(),apiCluster.getFullVersion());
            LOGGER.info("ClusterUrl {}",apiCluster1.getClusterUrl());
            LOGGER.info("HostUrl {}",apiCluster1.getHostsUrl());
            LOGGER.info("Cluster Uuid {}",apiCluster1.getUuid());
            LOGGER.info("集群运行状态 {}",apiCluster1.getEntityStatus());
        }
        LOGGER.info("结束测试的时间为{},**************结束测试获取ClouderaManager集群信息**************",Utils.getCurrentTime());
    }

}

获取cloudera-manager中每个集群的每个主机的详细信息:

public class ClouderaManagerHost {

    static RootResourceV18 apiRoot;

    static {
        apiRoot = new ClouderaManagerClientBuilder().withHost(666.666.666.666).
                withPort(Integer.valueOf(7180))
                .withUsernamePassword(admin, admin).build().getRootV18();
    }

    private final static Logger LOGGER = LoggerFactory.getLogger(ClouderaManagerHost.class);

    public static void getAllHost(){
        LOGGER.info("开始测试的时间为{},**************开始测试集群主机运行状态**************",Utils.getCurrentTime());
        HostsResourceV10 hostsResourceV10 = apiRoot.getHostsResource();
        List hostList = hostsResourceV10.readHosts(DataView.SUMMARY).getHosts();
        LOGGER.info("总共有 {} 台主机组成集群",hostList.size());
        for(ApiHost apiHost:hostList){
            LOGGER.info("---------------------------------------------");
            Host host = formatHost(hostsResourceV10.readHost(apiHost.getHostId()));
            LOGGER.info("主机Id : {}",host.getHostId());
            LOGGER.info("主机名: {}",host.getHostName());
            LOGGER.info("主机IP: {}",host.getIpAddress());
            LOGGER.info("主机线程数:{}",host.getNumCores());
            LOGGER.info("上次上报心跳时间 :{}",host.getLastHeart());
            LOGGER.info("核心数:{}",host.getNumPhysicalCores());
            LOGGER.info("机架:{}",host.getRack());
            LOGGER.info("内存(G):{}",host.getTotalPhysMemBytes());
            LOGGER.info("进程:{}", JSON.toJSON(host.getServices()));
            LOGGER.info("---------------------------------------------");
        }
        LOGGER.info("结束测试的时间为{},**************结束测试集群主机运行状态**************",Utils.getCurrentTime());
    }

    public static Host formatHost(ApiHost apiHost){
        Host host = new Host();
        List services = new ArrayList<>();
        host.setHostId(apiHost.getHostId());
        host.setHostName(apiHost.getHostname());
        host.setIpAddress(apiHost.getIpAddress());
        host.setNumCores(apiHost.getNumCores());
        host.setNumPhysicalCores(apiHost.getNumPhysicalCores());
        host.setLastHeart(apiHost.getLastHeartbeat().toString());
        host.setRack(apiHost.getRackId());
        host.setTotalPhysMemBytes(apiHost.getTotalPhysMemBytes()/1073741824);
        for(ApiRoleRef apiRoleRef:apiHost.getRoleRefs()){
            services.add(apiRoleRef.getRoleName());
        }
        host.setServices(services);
        return host;
    }
}



public class Host {
    private String hostId;

    private String hostName;

    private String ipAddress;

    private String rack;

    private String lastHeart;

    private List services = new ArrayList<>();

    private long numCores;

    private long numPhysicalCores;

    private long totalPhysMemBytes;

    public void setHostId(String hostId) {
        this.hostId = hostId;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public void setRack(String rack) {
        this.rack = rack;
    }

    public void setLastHeart(String lastHeart) {
        this.lastHeart = lastHeart;
    }

    public void setServices(List services) {
        this.services = services;
    }

    public void setNumCores(long numCores) {
        this.numCores = numCores;
    }

    public void setNumPhysicalCores(long numPhysicalCores) {
        this.numPhysicalCores = numPhysicalCores;
    }

    public void setTotalPhysMemBytes(long totalPhysMemBytes) {
        this.totalPhysMemBytes = totalPhysMemBytes;
    }

    public String getHostId() {
        return hostId;
    }

    public String getHostName() {
        return hostName;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public String getRack() {
        return rack;
    }

    public String getLastHeart() {
        return lastHeart;
    }

    public List getServices() {
        return services;
    }

    public long getNumCores() {
        return numCores;
    }

    public long getNumPhysicalCores() {
        return numPhysicalCores;
    }

    public long getTotalPhysMemBytes() {
        return totalPhysMemBytes;
    }
}

获取cloudera-manager上所有集群的所有服务的状态(这里只是粗略的,不涉及时间点位的):

public class ClouderaManagerService {

    private final static Logger LOGGER = LoggerFactory.getLogger(ClouderaManagerService.class);

    static RootResourceV18 apiRoot;

    static {
        apiRoot = new ClouderaManagerClientBuilder().withHost(666.666.666.666).
                withPort(Integer.valueOf(7180))
                .withUsernamePassword(admin, admin).build().getRootV18();
    }

    public static void getAllService(){
        LOGGER.info("开始测试的时间为{},**************开始测试集群服务运行状态**************",Utils.getCurrentTime());
        ApiClusterList apiClusterList = apiRoot.getClustersResource().readClusters(DataView.SUMMARY);
        for(ApiCluster apiCluster:apiClusterList){
            LOGGER.info("集群名称:{}",apiCluster.getDisplayName());
            LOGGER.info("CDH 版本:{}-{}",apiCluster.getVersion(),apiCluster.getFullVersion());
            ServicesResourceV10 servicesResourceV10 = apiRoot.getClustersResource().getServicesResource(apiCluster.getName());
            List apiServices = servicesResourceV10.readServices(DataView.FULL).getServices();
            LOGGER.info("集群总共有:{} 个service 在运行",apiServices.size());
            for(ApiService apiService:apiServices){
                Service service = formatService(apiService);
                LOGGER.info("***********************************");
                LOGGER.info("service 名称 {}",service.getName());
                LOGGER.info("service 类型 {}",service.getType());
                for(Agent agent:service.getAgentList()) {
                    LOGGER.info("节点名称 {}", agent.getName());
                    LOGGER.info("节点状态 {}", agent.getStatus());
                }
                LOGGER.info("***********************************");
            }
        }
        LOGGER.info("结束测试的时间为{},**************结束测试集群服务运行状态**************",Utils.getCurrentTime());
    }

    public static Service formatService(ApiService apiService){
        Service service = new Service();
        List agents = new ArrayList<>();
        service.setName(apiService.getName());
        service.setType(apiService.getType());
        for(ApiHealthCheck apiHealthCheck:apiService.getHealthChecks()){
            Agent agent =new Agent();
            agent.setName(apiHealthCheck.getName());
            agent.setStatus(apiHealthCheck.getSummary());
            agents.add(agent);
        }
        service.setAgentList(agents);
        return service;
    }

    public static void getAllServiceRoles(){
        LOGGER.info("开始测试的时间为{},**************开始测试集群各个服务的roles运行状态**************",Utils.getCurrentTime());
        ApiClusterList apiClusterList = apiRoot.getClustersResource().readClusters(DataView.SUMMARY);
        for(ApiCluster apiCluster:apiClusterList){
            LOGGER.info("集群名称:{}",apiCluster.getDisplayName());
            LOGGER.info("CDH 版本:{}-{}",apiCluster.getVersion(),apiCluster.getFullVersion());
            ServicesResourceV18 servicesResourceV18 = apiRoot.getClustersResource().getServicesResource(apiCluster.getName());
            List apiServices = servicesResourceV18.readServices(DataView.FULL).getServices();
            LOGGER.info("集群总共有:{} 个service 在运行",apiServices.size());
            for(ApiService apiService:apiServices){
                RolesResourceV11 rolesResourceV11 = servicesResourceV18.getRolesResource(apiService.getName());
                LOGGER.info("---------------------服务名称是{}---------------------",apiService.getName());
                for(ApiRole apiRole :rolesResourceV11.readRoles()){
                    LOGGER.info("***************************",apiRole.getName());
                    LOGGER.info("role名称 {}",apiRole.getName());
                    LOGGER.info("role类型 {}",apiRole.getType());
                    LOGGER.info("所属集群 {}",apiRole.getServiceRef().getClusterName());
                    LOGGER.info("所属服务 {}",apiRole.getServiceRef().getServiceName());
                    LOGGER.info("主机ID {}",apiRole.getHostRef().getHostId());
                    LOGGER.info("roleUrl {}",apiRole.getRoleUrl());
                    LOGGER.info("role状态 {}",apiRole.getRoleState());
                    LOGGER.info("运行状态总结 {}",apiRole.getHealthSummary());
                    LOGGER.info("entityStatus {}",apiRole.getEntityStatus());
                    LOGGER.info("roleConfigGroupName {}",apiRole.getRoleConfigGroupRef().getRoleConfigGroupName());
                    LOGGER.info("configStalenessStatus {}",apiRole.getConfigStalenessStatus());
                    LOGGER.info("haStatus {}",apiRole.getHaStatus());
                    for(ApiHealthCheck apiHealthCheck:apiRole.getHealthChecks()){
                        LOGGER.info("health check name {}",apiHealthCheck.getName());
                        LOGGER.info("health check summary {}",apiHealthCheck.getSummary());
                        LOGGER.info("health check suppressed {}",apiHealthCheck.getSuppressed());
                    }
                    LOGGER.info("***************************");
                }
                LOGGER.info("--------------------------------------------------------",apiService.getName());
            }
        }
        LOGGER.info("结束测试的时间为{},**************结束测试集群各个服务的roles运行状态**************",Utils.getCurrentTime());
    }

}

public class Service {
    private String name;

    private String type;

    private List agentList = new ArrayList<>();

    public void setName(String name) {
        this.name = name;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setAgentList(List agentList) {
        this.agentList = agentList;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public List getAgentList() {
        return agentList;
    }
}


public class Agent {
    private String name;

    private ApiHealthSummary status;

    public void setName(String name) {
        this.name = name;
    }

    public void setStatus(ApiHealthSummary status) {
        this.status = status;
    }

    public String getName() {
        return name;
    }

    public ApiHealthSummary getStatus() {
        return status;
    }
}

获取cloudera-manager中某个服务的时间序列点位:

public class ClouderaManagerServiceMetrics {

    private final static Logger LOGGER = LoggerFactory.getLogger(ClouderaManagerServiceMetrics.class);

    static RootResourceV18 apiRoot;

    static {
        apiRoot = new ClouderaManagerClientBuilder().withHost(666.666.666.666).
                withPort(Integer.valueOf(7180))
                .withUsernamePassword(admin, admin).build().getRootV18();
    }

    public static List formatApiTimeSeriesResponse(List apiTimeSeriesResponseList){
        List metrics = new ArrayList<>();
        for(ApiTimeSeriesResponse apiTimeSeriesResponse:apiTimeSeriesResponseList) {
            List dataList = new ArrayList<>();
            List apiTimeSeriesList = apiTimeSeriesResponse.getTimeSeries();
            for (ApiTimeSeries apiTimeSeries : apiTimeSeriesList) {
                Metric metric = new Metric();
                metric.setMetricName(apiTimeSeries.getMetadata().getMetricName());
                metric.setEntityName(apiTimeSeries.getMetadata().getEntityName());
                metric.setStartTime(apiTimeSeries.getMetadata().getStartTime().toString());
                metric.setEndTime(apiTimeSeries.getMetadata().getEndTime().toString());
                for (ApiTimeSeriesData apiTimeSeriesData : apiTimeSeries.getData()) {
                    Data data = new Data();
                    data.setTimestamp(apiTimeSeriesData.getTimestamp().toString());
                    data.setType(apiTimeSeriesData.getType());
                    data.setValue(apiTimeSeriesData.getValue());
                    dataList.add(data);
                }
                metric.setData(dataList);
                metrics.add(metric);
            }
        }
        return metrics;
    }

    public static List getServiceMetrics(String query,String startTime , String endTime){
        TimeSeriesResourceV6 timeSeriesResourceV6 = apiRoot.getTimeSeriesResource();
        String[] params = new String []{query,startTime,endTime};
        LOGGER.info("query sql is {} ,startTime is {} ,endTime is now",params);

        LOGGER.info("开始测试的时间为{},**************开始查询某个服务点位状态**************",Utils.getCurrentTime());
        ApiTimeSeriesResponseList response = timeSeriesResourceV6.queryTimeSeries(query,startTime,endTime);
        List apiTimeSeriesResponseList = response.getResponses();
        List metrics = formatApiTimeSeriesResponse(apiTimeSeriesResponseList);
        LOGGER.info("查询时间序列点位:{}", JSON.toJSON(metrics));
        LOGGER.info("结束测试的时间为{},**************结束查询某个服务点位状态**************",Utils.getCurrentTime());
        return metrics;
    }

}

public class Metric {
    private String metricName;

    private String entityName;

    private String startTime;

    private String endTime;

    List data = new ArrayList<>();

    public void setMetricName(String metricName) {
        this.metricName = metricName;
    }

    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public void setData(List data) {
        this.data = data;
    }

    public String getMetricName() {
        return metricName;
    }

    public String getEntityName() {
        return entityName;
    }

    public String getStartTime() {
        return startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public List getData() {
        return data;
    }
}

public class Data {
    private String timestamp;

    private Double value;

    private String type;

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public void setValue(Double value) {
        this.value = value;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public Double getValue() {
        return value;
    }

    public String getType() {
        return type;
    }
}

这个时间点位可能比较抽象,举个例子:
ClouderaManager java api 有关查询api 使用。_第1张图片

这种以时间为横轴,点位为纵轴的我们统称为时间序列点位。

让我们看看这个怎么用:

ClouderaManagerServiceMetrics.getServiceMetrics("SELECT health_good_rate * 100 AS \"good health\" WHERE entityName = \"hbase\" AND category = SERVICE"
                ,"2018-05-09","now");

我们可能会说,沃日,这个sql语句我怎么知道。。
ClouderaManager java api 有关查询api 使用。_第2张图片

ClouderaManager java api 有关查询api 使用。_第3张图片

ClouderaManager java api 有关查询api 使用。_第4张图片

这样不就可以获取sql语句了嘛。

最后贴上调用方法,放在main方法里面,即可让程序跑起来:

ClouderaManagerCluster.getAllCluster();

ClouderaManagerHost.getAllHost();

ClouderaManagerService.getAllService();

ClouderaManagerService.getAllServiceRoles();

ClouderaManagerServiceMetrics.getServiceMetrics("SELECT health_good_rate * 100 AS \"good health\" WHERE entityName = \"hbase\" AND category = SERVICE"
                ,"2018-05-09","now");

我自己整理了一分api简单文档,可以看哈~:

1。cloudera manager api 入口:
用来创建cloudera manager 的入口,需要输入地址以及账号密码来创建一个根资源的实例:

RootResourceV10 apiRoot = new ClouderaManagerClientBuilder().withHost(“xxx.xxx.xxx.xx”)
.withUsernamePassword(“123”, “123”).build().getRootV10();

RootResourceV10这个对象不是必须的,它还有V9,V8等等许多,只不过越向后的版本兼容前面的并且增加了新功能。

2。获取主机信息:
HostsResourceV10 hostsResourceV10 = apiRoot.getHostsResource();
通过cloudera manager 的入口api,获取一个主机资源的实例,V10的意思同上。
List hostList = hostsResourceV10.readHosts(DataView.SUMMARY).getHosts();
这样就可以得到一个apihost的一个列表对象,这里面可能获得的不全面,可以通过Apihost开面的hostId传入到
hostsResourceV10.readHost(apiHost.getHostId())
当中,来获取某个主机的信息。

3。获取服务状态
还是要借助cloudera manager的公共入口RootResourceV10来进行,首先要获取服务状态,你要先知道你的cloudera manager 管理了几个集群。

ApiClusterList apiClusterList = apiRoot.getClustersResource().readClusters(DataView.SUMMARY);
ApiClusterList 是ApiCluster的列表,可以遍历ApiCluster来获取每个ApiCluster,ApiCluster里面包含了集群的详细信息,包含CDH版本,集群名称等等。。

ServicesResourceV10 servicesResourceV10 = apiRoot.getClustersResource().getServicesResource(apiCluster.getName());

通过获取的集群名称,你可以获得当前集群下的服务实例

List  apiServices = servicesResourceV10.readServices(DataView.FULL).getServices();

然后将所有服务资源转化为ApiService的一个列报表,这样我们就可以通过遍历ApiService来获取每个服务的状态了

4。 获取服务中各个主节点和从节点详细状态

ServicesResourceV10 servicesResourceV10 = apiRoot.getClustersResource().getServicesResource(apiCluster.getName());

通过获取的集群名称,你可以获得当前集群下的服务实例

List  apiServices = servicesResourceV10.readServices(DataView.FULL).getServices();

通过遍历获取每个服务。

RolesResourceV11 rolesResourceV11 = servicesResourceV18.getRolesResource(apiService.getName());

然后通过RolesResourceV11获得一个service下面得所有role,然后通过
rolesResourceV11.readRoles()
获取一个ApiRole的链表。遍历即可获取一个service下的所有role。

5。获取服务或者主机点位时间序列
TimeSeriesResourceV6 timeSeriesResourceV6 = apiRoot.getTimeSeriesResource();
也是依赖于cloudera manager 的公共入口RootResourceV10,获得一个TimeSeriesResource 用来查询时间序列点位,
ApiTimeSeriesResponseList response = timeSeriesResourceV6.queryTimeSeries(query,startTime,endTime);
可以采用queryTimeSeries方法,来进行查询,query是一个sql语句,然后是开始时间,结束时间,下面给出两个示例查询。

“SELECT health_good_rate * 100 AS \”good health\” WHERE entityName = \”hbase\” AND category = SERVICE” ,”2018-05-09”,”now”

然后获取查询结果

List apiTimeSeriesResponseList=response.getResponses();

获取一个ApiTimeSeriesResponse的一个列表,来获取查询结果即可。

好啦好啦,就介绍到这里,溜了溜了~

你可能感兴趣的:(cloudera,manager,CDH,cloudera,manager,api)