Java代碼監控Yarn頁面(包括各個Job的信息)

QueueMetrics

public class QueueMetrics {

    private String modelerType;
    private String Queue;
    private String Hostname;
    private Integer AppsSubmitted;
    private Integer AppsRunning;
    private Integer AppsPending;
    private Integer AppsCompleted;
    private Integer AppsKilled;
    private Integer AppsFailed;
    private Integer AllocatedMB;
    private Integer AllocatedVCores;
    private Integer AllocatedContainers;
    private Integer AvailableMB;
    private Integer AvailableVCores;
    private Integer PendingMB;
    private Integer PendingVCores;
    private Integer PendingContainers;
    private Integer ReservedMB;
    private Integer ReservedVCores;
    private Integer ReservedContainers;
    private Integer ActiveApplications;

    public String getModelerType() {
        return modelerType;
    }

    public void setModelerType(String modelerType) {
        this.modelerType = modelerType;
    }

    public String getQueue() {
        return Queue;
    }

    public void setQueue(String queue) {
        Queue = queue;
    }

    public String getHostname() {
        return Hostname;
    }

    public void setHostname(String hostname) {
        Hostname = hostname;
    }

    public Integer getAppsSubmitted() {
        return AppsSubmitted;
    }

    public void setAppsSubmitted(Integer appsSubmitted) {
        AppsSubmitted = appsSubmitted;
    }

    public Integer getAppsRunning() {
        return AppsRunning;
    }

    public void setAppsRunning(Integer appsRunning) {
        AppsRunning = appsRunning;
    }

    public Integer getAppsPending() {
        return AppsPending;
    }

    public void setAppsPending(Integer appsPending) {
        AppsPending = appsPending;
    }

    public Integer getAppsCompleted() {
        return AppsCompleted;
    }

    public void setAppsCompleted(Integer appsCompleted) {
        AppsCompleted = appsCompleted;
    }

    public Integer getAppsKilled() {
        return AppsKilled;
    }

    public void setAppsKilled(Integer appsKilled) {
        AppsKilled = appsKilled;
    }

    public Integer getAppsFailed() {
        return AppsFailed;
    }

    public void setAppsFailed(Integer appsFailed) {
        AppsFailed = appsFailed;
    }

    public Integer getAllocatedMB() {
        return AllocatedMB;
    }

    public void setAllocatedMB(Integer allocatedMB) {
        AllocatedMB = allocatedMB;
    }

    public Integer getAllocatedVCores() {
        return AllocatedVCores;
    }

    public void setAllocatedVCores(Integer allocatedVCores) {
        AllocatedVCores = allocatedVCores;
    }

    public Integer getAllocatedContainers() {
        return AllocatedContainers;
    }

    public void setAllocatedContainers(Integer allocatedContainers) {
        AllocatedContainers = allocatedContainers;
    }

    public Integer getAvailableMB() {
        return AvailableMB;
    }

    public void setAvailableMB(Integer availableMB) {
        AvailableMB = availableMB;
    }

    public Integer getAvailableVCores() {
        return AvailableVCores;
    }

    public void setAvailableVCores(Integer availableVCores) {
        AvailableVCores = availableVCores;
    }

    public Integer getPendingMB() {
        return PendingMB;
    }

    public void setPendingMB(Integer pendingMB) {
        PendingMB = pendingMB;
    }

    public Integer getPendingVCores() {
        return PendingVCores;
    }

    public void setPendingVCores(Integer pendingVCores) {
        PendingVCores = pendingVCores;
    }

    public Integer getPendingContainers() {
        return PendingContainers;
    }

    public void setPendingContainers(Integer pendingContainers) {
        PendingContainers = pendingContainers;
    }

    public Integer getReservedMB() {
        return ReservedMB;
    }

    public void setReservedMB(Integer reservedMB) {
        ReservedMB = reservedMB;
    }

    public Integer getReservedVCores() {
        return ReservedVCores;
    }

    public void setReservedVCores(Integer reservedVCores) {
        ReservedVCores = reservedVCores;
    }

    public Integer getReservedContainers() {
        return ReservedContainers;
    }

    public void setReservedContainers(Integer reservedContainers) {
        ReservedContainers = reservedContainers;
    }

    public Integer getActiveApplications() {
        return ActiveApplications;
    }

    public void setActiveApplications(Integer activeApplications) {
        ActiveApplications = activeApplications;
    }
}

YarnClusterMetrics

public class YarnClusterMetrics {

    private String modelerType;
    private String Hostname;
    private Integer NumActiveNMs;
    private Integer NumUnhealthyNMs;
    private Integer NumLostNMs;
    private Integer NumRebootedNMs;

    public String getModelerType() {
        return modelerType;
    }

    public void setModelerType(String modelerType) {
        this.modelerType = modelerType;
    }

    public String getHostname() {
        return Hostname;
    }

    public void setHostname(String hostname) {
        Hostname = hostname;
    }

    public Integer getNumActiveNMs() {
        return NumActiveNMs;
    }

    public void setNumActiveNMs(Integer numActiveNMs) {
        NumActiveNMs = numActiveNMs;
    }

    public Integer getNumUnhealthyNMs() {
        return NumUnhealthyNMs;
    }

    public void setNumUnhealthyNMs(Integer numUnhealthyNMs) {
        NumUnhealthyNMs = numUnhealthyNMs;
    }

    public Integer getNumLostNMs() {
        return NumLostNMs;
    }

    public void setNumLostNMs(Integer numLostNMs) {
        NumLostNMs = numLostNMs;
    }

    public Integer getNumRebootedNMs() {
        return NumRebootedNMs;
    }

    public void setNumRebootedNMs(Integer numRebootedNMs) {
        NumRebootedNMs = numRebootedNMs;
    }

    @Override
    public String toString() {
        return "YarnClusterMetrics{" +
                "modelerType='" + modelerType + '\'' +
                ", Hostname='" + Hostname + '\'' +
                ", NumActiveNMs=" + NumActiveNMs +
                ", NumUnhealthyNMs=" + NumUnhealthyNMs +
                ", NumLostNMs=" + NumLostNMs +
                ", NumRebootedNMs=" + NumRebootedNMs +
                '}';
    }
}

YarnApplication

public class YarnApplication {

    private String id;
    private String user;
    private String name;
    private String queue;
    private String state;
    private String finalStatus;
    private int progress;
    private String trackingUI;
    private String diagnostics;
    private Long clusterId;
    private String applicationType;
    private String applicationTags;
    private Long startedTime;
    private Long finishedTime;
    private Long elapsedTime;
    private String amContainerLogs;
    private String amHostHttpAddress;
    private int allocatedMB;
    private int allocatedVCores;
    private int reservedMB;
    private int reservedVCores;
    private int runningContainers;
    private int memorySeconds;
    private int vcoreSeconds;
    private int preemptedResourceMB;
    private int preemptedResourceVCores;
    private int numNonAMContainerPreempted;
    private int numAMContainerPreempted;
    private String logAggregationStatus;

    public YarnApplication() {
        this.id = id;
        this.user = user;
        this.name = name;
        this.queue = queue;
        this.state = state;
        this.finalStatus = finalStatus;
        this.progress = progress;
        this.trackingUI = trackingUI;
        this.diagnostics = diagnostics;
        this.clusterId = clusterId;
        this.applicationType = applicationType;
        this.applicationTags = applicationTags;
        this.startedTime = startedTime;
        this.finishedTime = finishedTime;
        this.elapsedTime = elapsedTime;
        this.amContainerLogs = amContainerLogs;
        this.amHostHttpAddress = amHostHttpAddress;
        this.allocatedMB = allocatedMB;
        this.allocatedVCores = allocatedVCores;
        this.reservedMB = reservedMB;
        this.reservedVCores = reservedVCores;
        this.runningContainers = runningContainers;
        this.memorySeconds = memorySeconds;
        this.vcoreSeconds = vcoreSeconds;
        this.preemptedResourceMB = preemptedResourceMB;
        this.preemptedResourceVCores = preemptedResourceVCores;
        this.numNonAMContainerPreempted = numNonAMContainerPreempted;
        this.numAMContainerPreempted = numAMContainerPreempted;
        this.logAggregationStatus = logAggregationStatus;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

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

    public String getQueue() {
        return queue;
    }

    public void setQueue(String queue) {
        this.queue = queue;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getFinalStatus() {
        return finalStatus;
    }

    public void setFinalStatus(String finalStatus) {
        this.finalStatus = finalStatus;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public String getTrackingUI() {
        return trackingUI;
    }

    public void setTrackingUI(String trackingUI) {
        this.trackingUI = trackingUI;
    }

    public String getDiagnostics() {
        return diagnostics;
    }

    public void setDiagnostics(String diagnostics) {
        this.diagnostics = diagnostics;
    }

    public Long getClusterId() {
        return clusterId;
    }

    public void setClusterId(Long clusterId) {
        this.clusterId = clusterId;
    }

    public String getApplicationType() {
        return applicationType;
    }

    public void setApplicationType(String applicationType) {
        this.applicationType = applicationType;
    }

    public String getApplicationTags() {
        return applicationTags;
    }

    public void setApplicationTags(String applicationTags) {
        this.applicationTags = applicationTags;
    }

    public Long getStartedTime() {
        return startedTime;
    }

    public void setStartedTime(Long startedTime) {
        this.startedTime = startedTime;
    }

    public Long getFinishedTime() {
        return finishedTime;
    }

    public void setFinishedTime(Long finishedTime) {
        this.finishedTime = finishedTime;
    }

    public Long getElapsedTime() {
        return elapsedTime;
    }

    public void setElapsedTime(Long elapsedTime) {
        this.elapsedTime = elapsedTime;
    }

    public String getAmContainerLogs() {
        return amContainerLogs;
    }

    public void setAmContainerLogs(String amContainerLogs) {
        this.amContainerLogs = amContainerLogs;
    }

    public String getAmHostHttpAddress() {
        return amHostHttpAddress;
    }

    public void setAmHostHttpAddress(String amHostHttpAddress) {
        this.amHostHttpAddress = amHostHttpAddress;
    }

    public int getAllocatedMB() {
        return allocatedMB;
    }

    public void setAllocatedMB(int allocatedMB) {
        this.allocatedMB = allocatedMB;
    }

    public int getAllocatedVCores() {
        return allocatedVCores;
    }

    public void setAllocatedVCores(int allocatedVCores) {
        this.allocatedVCores = allocatedVCores;
    }

    public int getReservedMB() {
        return reservedMB;
    }

    public void setReservedMB(int reservedMB) {
        this.reservedMB = reservedMB;
    }

    public int getReservedVCores() {
        return reservedVCores;
    }

    public void setReservedVCores(int reservedVCores) {
        this.reservedVCores = reservedVCores;
    }

    public int getRunningContainers() {
        return runningContainers;
    }

    public void setRunningContainers(int runningContainers) {
        this.runningContainers = runningContainers;
    }

    public int getMemorySeconds() {
        return memorySeconds;
    }

    public void setMemorySeconds(int memorySeconds) {
        this.memorySeconds = memorySeconds;
    }

    public int getVcoreSeconds() {
        return vcoreSeconds;
    }

    public void setVcoreSeconds(int vcoreSeconds) {
        this.vcoreSeconds = vcoreSeconds;
    }

    public int getPreemptedResourceMB() {
        return preemptedResourceMB;
    }

    public void setPreemptedResourceMB(int preemptedResourceMB) {
        this.preemptedResourceMB = preemptedResourceMB;
    }

    public int getPreemptedResourceVCores() {
        return preemptedResourceVCores;
    }

    public void setPreemptedResourceVCores(int preemptedResourceVCores) {
        this.preemptedResourceVCores = preemptedResourceVCores;
    }

    public int getNumNonAMContainerPreempted() {
        return numNonAMContainerPreempted;
    }

    public void setNumNonAMContainerPreempted(int numNonAMContainerPreempted) {
        this.numNonAMContainerPreempted = numNonAMContainerPreempted;
    }

    public int getNumAMContainerPreempted() {
        return numAMContainerPreempted;
    }

    public void setNumAMContainerPreempted(int numAMContainerPreempted) {
        this.numAMContainerPreempted = numAMContainerPreempted;
    }

    public String getLogAggregationStatus() {
        return logAggregationStatus;
    }

    public void setLogAggregationStatus(String logAggregationStatus) {
        this.logAggregationStatus = logAggregationStatus;
    }

    @Override
    public String toString() {
        return "YarnApplication{" +
                "id='" + id + '\'' +
                ", user='" + user + '\'' +
                ", name='" + name + '\'' +
                ", queue='" + queue + '\'' +
                ", state='" + state + '\'' +
                ", finalStatus='" + finalStatus + '\'' +
                ", progress=" + progress +
                ", trackingUI='" + trackingUI + '\'' +
                ", diagnostics='" + diagnostics + '\'' +
                ", clusterId=" + clusterId +
                ", applicationType='" + applicationType + '\'' +
                ", applicationTags='" + applicationTags + '\'' +
                ", startedTime=" + startedTime +
                ", finishedTime=" + finishedTime +
                ", elapsedTime=" + elapsedTime +
                ", amContainerLogs='" + amContainerLogs + '\'' +
                ", amHostHttpAddress='" + amHostHttpAddress + '\'' +
                ", allocatedMB=" + allocatedMB +
                ", allocatedVCores=" + allocatedVCores +
                ", reservedMB=" + reservedMB +
                ", reservedVCores=" + reservedVCores +
                ", runningContainers=" + runningContainers +
                ", memorySeconds=" + memorySeconds +
                ", vcoreSeconds=" + vcoreSeconds +
                ", preemptedResourceMB=" + preemptedResourceMB +
                ", preemptedResourceVCores=" + preemptedResourceVCores +
                ", numNonAMContainerPreempted=" + numNonAMContainerPreempted +
                ", numAMContainerPreempted=" + numAMContainerPreempted +
                ", logAggregationStatus='" + logAggregationStatus + '\'' +
                '}';
    }
}

YarnUtils

public class YarnUtils {

    private static final String YarnUri = "http://192.168.72.141:8088";
    private static final String FETCH_Application = "/ws/v1/cluster/apps/";
    private static final String FETCH_Yarn_Info = "%s/jmx?qry=%s";
    private static final String ClusterMetrics = "Hadoop:service=ResourceManager,name=ClusterMetrics";
    private static final String QueueMetrics = "Hadoop:service=ResourceManager,name=QueueMetrics,q0=root";
    private static final Logger LOGGER = LoggerFactory.getLogger(YarnUtils.class);

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    /**
     * 查看集羣狀態信息
     * @param client
     * @throws IOException
     */
    public static YarnClusterMetrics fetchClusterMetrics(StatefulHttpClient client) throws IOException {
        YarnClusterMetrics yarnClusterMetrics = new YarnClusterMetrics();
        String namenodeUrl = String.format(FETCH_Yarn_Info, YarnUri, ClusterMetrics);
        MonitorMetrics monitorMetrics = client.get(MonitorMetrics.class, namenodeUrl, null, null);
        yarnClusterMetrics.setHostname(monitorMetrics.getMetricsValue("tag.Hostname").toString());
        yarnClusterMetrics.setModelerType(monitorMetrics.getMetricsValue("modelerType").toString());
        yarnClusterMetrics.setNumActiveNMs((int)monitorMetrics.getMetricsValue("NumActiveNMs"));
        yarnClusterMetrics.setNumUnhealthyNMs((int)monitorMetrics.getMetricsValue("NumUnhealthyNMs"));
        yarnClusterMetrics.setNumLostNMs((int)monitorMetrics.getMetricsValue("NumLostNMs"));
        yarnClusterMetrics.setNumRebootedNMs((int)monitorMetrics.getMetricsValue("NumRebootedNMs"));

        return yarnClusterMetrics;
    }

    /**
     * 查詢Queue資源使用情況
     * @param client
     * @return
     * @throws IOException
     */
    public static QueueMetrics fetchQueueMetrics(StatefulHttpClient client) throws IOException {
        QueueMetrics queueMetrics = new QueueMetrics();
        String namenodeUrl = String.format(FETCH_Yarn_Info, YarnUri, QueueMetrics);
        MonitorMetrics monitorMetrics = client.get(MonitorMetrics.class, namenodeUrl, null, null);

        queueMetrics.setModelerType(monitorMetrics.getMetricsValue("modelerType").toString());
        queueMetrics.setQueue(monitorMetrics.getMetricsValue("tag.Queue").toString());
        queueMetrics.setHostname(monitorMetrics.getMetricsValue("tag.Hostname").toString());
        queueMetrics.setAppsSubmitted((int)monitorMetrics.getMetricsValue("AppsSubmitted"));
        queueMetrics.setAppsRunning((int)monitorMetrics.getMetricsValue("AppsRunning"));
        queueMetrics.setAppsPending((int)monitorMetrics.getMetricsValue("AppsPending"));
        queueMetrics.setAppsCompleted((int)monitorMetrics.getMetricsValue("AppsCompleted"));
        queueMetrics.setAppsKilled((int)monitorMetrics.getMetricsValue("AppsKilled"));
        queueMetrics.setAppsFailed((int)monitorMetrics.getMetricsValue("AppsFailed"));
        queueMetrics.setAllocatedMB((int)monitorMetrics.getMetricsValue("AllocatedMB"));
        queueMetrics.setAllocatedVCores((int)monitorMetrics.getMetricsValue("AllocatedVCores"));
        queueMetrics.setAllocatedContainers((int)monitorMetrics.getMetricsValue("AllocatedContainers"));
        queueMetrics.setAvailableMB((int)monitorMetrics.getMetricsValue("AvailableMB"));
        queueMetrics.setAvailableVCores((int)monitorMetrics.getMetricsValue("AvailableVCores"));
        queueMetrics.setPendingMB((int)monitorMetrics.getMetricsValue("PendingMB"));
        queueMetrics.setPendingVCores((int)monitorMetrics.getMetricsValue("PendingVCores"));
        queueMetrics.setPendingContainers((int)monitorMetrics.getMetricsValue("PendingContainers"));
        queueMetrics.setReservedMB((int)monitorMetrics.getMetricsValue("ReservedMB"));
        queueMetrics.setReservedVCores((int)monitorMetrics.getMetricsValue("ReservedVCores"));
        queueMetrics.setReservedContainers((int)monitorMetrics.getMetricsValue("ReservedContainers"));
        queueMetrics.setActiveApplications((int)monitorMetrics.getMetricsValue("ActiveApplications"));

        return queueMetrics;
    }

    /**
     * 查看job信息
     * @return
     * @throws IOException
     * @throws JSONException
     */
    public static List<YarnApplication> fetchApp() throws IOException, JSONException {
        ArrayList<YarnApplication> list = new ArrayList<>();
        JSONObject json = readJsonFromUrl(YarnUri+FETCH_Application);
        //System.out.println(json.toString());
        if (json.toString().equals("{\"apps\":null}")){
            LOGGER.error("無job信息,請檢查yarn頁面");
            System.exit(1);
        }
        JSONObject apps=json.getJSONObject("apps");
        JSONArray app=apps.getJSONArray("app");
        for(int i=0;i<app.length();i++) {
            JSONObject info = app.getJSONObject(i);
            YarnApplication yarnApplication = new YarnApplication();
            yarnApplication.setId(info.getString("id"));
            yarnApplication.setUser(info.getString("user"));
            yarnApplication.setName(info.getString("name"));
            yarnApplication.setQueue(info.getString("queue"));
            yarnApplication.setState(info.getString("state"));
            yarnApplication.setFinalStatus(info.getString("finalStatus"));
            yarnApplication.setProgress(info.getInt("progress"));
            yarnApplication.setTrackingUI(info.getString("trackingUI"));
            yarnApplication.setDiagnostics(info.getString("diagnostics"));
            yarnApplication.setClusterId(info.getLong("clusterId"));
            yarnApplication.setApplicationType(info.getString("applicationType"));
            yarnApplication.setApplicationTags(info.getString("applicationTags"));
            yarnApplication.setStartedTime(info.getLong("startedTime"));
            yarnApplication.setFinishedTime(info.getLong("finishedTime"));
            yarnApplication.setElapsedTime(info.getLong("elapsedTime"));
            yarnApplication.setAmContainerLogs(info.getString("amContainerLogs"));
            yarnApplication.setAmHostHttpAddress(info.getString("amHostHttpAddress"));
            yarnApplication.setAllocatedMB(info.getInt("allocatedMB"));
            yarnApplication.setAllocatedVCores(info.getInt("allocatedVCores"));
            yarnApplication.setReservedMB(info.getInt("reservedMB"));
            yarnApplication.setReservedVCores(info.getInt("reservedVCores"));
            yarnApplication.setRunningContainers(info.getInt("runningContainers"));
            yarnApplication.setMemorySeconds(info.getInt("memorySeconds"));
            yarnApplication.setVcoreSeconds(info.getInt("vcoreSeconds"));
            yarnApplication.setPreemptedResourceMB(info.getInt("preemptedResourceMB"));
            yarnApplication.setPreemptedResourceVCores(info.getInt("preemptedResourceVCores"));
            yarnApplication.setNumNonAMContainerPreempted(info.getInt("numNonAMContainerPreempted"));
            yarnApplication.setNumAMContainerPreempted(info.getInt("numAMContainerPreempted"));
            yarnApplication.setLogAggregationStatus(info.getString("logAggregationStatus"));

            list.add(yarnApplication);
        }

        return list;
    }
}

JsonUtil

public class JsonUtil {

    private static ObjectMapper objMapper = new ObjectMapper();

    static {
        objMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
    }

    public static String toJson(Object obj) throws IOException {
        return objMapper.writeValueAsString(obj);
    }

    public static String toJson(Object obj, boolean prettyFormat) throws IOException {
        String json = objMapper.writeValueAsString(obj);
        if (prettyFormat) {
            Object jsonObj = objMapper.readValue(json, Object.class);
            json = objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj);
        }
        return json;
    }

    public static <T> T fromJson(Class<T> clazz, String json) throws IOException {
        return objMapper.readValue(json, clazz);
    }

    public static <T> List<T> fromJsonList(Class<T> clazz, String json) throws IOException {
        return objMapper.readValue(json, objMapper.getTypeFactory()
                .constructCollectionType(List.class, clazz));
    }

    public static <K, V> Map<K, V> fromJsonMap(Class<K> keyClazz, Class<V> valueClazz, String json)
            throws IOException{
        return objMapper.readValue(json, objMapper.getTypeFactory()
                .constructMapType(HashMap.class, keyClazz, valueClazz));
    }

}

MonitorMetrics

public class MonitorMetrics {
    // beans爲通過jmx所返回的json串中最起始的key
    // 結構爲{"beans":[{"":"","":"",...}]}
    List<Map<String, Object>> beans = new ArrayList<>();

    public List<Map<String, Object>> getBeans() {
        return beans;
    }

    public void setBeans(List<Map<String, Object>> beans) {
        this.beans = beans;
    }

    public Object getMetricsValue(String name) {
        if (beans.isEmpty()) {
            return null;
        }
        return beans.get(0).getOrDefault(name, null);
    }
}
public class StatefulHttpClient {

    private HttpClientContext context;
    private CloseableHttpClient httpclient;
    private int requestTimeOut = 15;
    private int sessionTimeOut = 60;

    public StatefulHttpClient(int sessionTimeOut, int requestTimeOut,
                              HttpHost proxy) {
        initCookieStore();
        this.sessionTimeOut = sessionTimeOut;
        this.requestTimeOut = requestTimeOut;
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
                .setConnectTimeout(this.requestTimeOut * 1000).setSocketTimeout(this.requestTimeOut * 1000);
        if (proxy != null) {
            requestConfigBuilder.setProxy(proxy);
        }
        httpclient = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfigBuilder.build()).build();
    }

    public StatefulHttpClient(HttpHost proxy) {
        initCookieStore();
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
                .setConnectTimeout(10000).setSocketTimeout(this.requestTimeOut * 1000);
        if (proxy != null) {
            requestConfigBuilder.setProxy(proxy);
        }
        httpclient = HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfigBuilder.build()).build();
    }

    public int getRequestTimeOut() {
        return requestTimeOut;
    }

    public void setRequestTimeOut(int requestTimeOut) {
        this.requestTimeOut = requestTimeOut;
    }

    public int getSessionTimeOut() {
        return sessionTimeOut;
    }

    public void setSessionTimeOut(int sessionTimeOut) {
        this.sessionTimeOut = sessionTimeOut;
    }

    private void initCookieStore() {
        PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
        Registry<CookieSpecProvider> cookieSpecReg = RegistryBuilder.<CookieSpecProvider>create()
                .register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider(publicSuffixMatcher))
                .register(CookieSpecs.STANDARD, new RFC6265CookieSpecProvider(publicSuffixMatcher)).build();
        CookieStore cookieStore = new BasicCookieStore();

        context = HttpClientContext.create();
        context.setCookieSpecRegistry(cookieSpecReg);
        context.setCookieStore(cookieStore);
    }

    /**
     * http get.
     *
     * @param clazz clazz
     * @param url url
     * @param queryParam queryParam
     * @param headers headers
     * @param <T> T
     * @return T
     * @throws IOException e
     */
    @SuppressWarnings("unchecked")
    public <T> T get(Class<T> clazz, String url,
                     Map<String, String> queryParam, Map<String, String> headers) throws IOException {
        HttpGet get = new HttpGet(url);
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                get.setHeader(key, headers.get(key));
            }
        }
        try {
            if (queryParam != null && queryParam.size() > 0) {
                URIBuilder builder = new URIBuilder(get.getURI());
                for (String key : queryParam.keySet()) {
                    builder.addParameter(key, queryParam.get(key));
                }
                get.setURI(builder.build());
            }

        } catch (Exception exception) {
            exception.printStackTrace();

        }
        HttpEntity entity = null;
        try {
            HttpResponse response = httpclient.execute(get, context);
            int statusCode = response.getStatusLine().getStatusCode() / 100;
            entity = response.getEntity();
            String result = EntityUtils.toString(response.getEntity());
            if (statusCode == 4 || statusCode == 5) {
                throw new IOException(result);
            }

            if (String.class.equals(clazz)) {
                return (T) result;
            }
            if (result == null || result.length() == 0) {
                return null;
            }

            return JsonUtil.fromJson(clazz, result);
        } finally {
            if (entity != null) {
                EntityUtils.consume(entity);
            }
        }
    }


    /**
     * http post.
     *
     * @param clazz clazz
     * @param url url
     * @param queryParam queryParam
     * @param headers headers
     * @param <T> T
     * @return T
     * @throws IOException e
     */
    @SuppressWarnings("unchecked")
    public <T> T post(Class<T> clazz, String url,
                      Map<String, String> queryParam, Map<String, String> headers,
                      String body) throws IOException {

        HttpPost post = new HttpPost(url);
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                post.setHeader(key, headers.get(key));
            }
        }
        try {
            if (queryParam != null && queryParam.size() > 0) {
                URIBuilder builder = new URIBuilder(post.getURI());
                for (String key : queryParam.keySet()) {
                    builder.addParameter(key, queryParam.get(key));
                }
                post.setURI(builder.build());
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        if (body != null) {
            HttpEntity entity = new StringEntity(body,
                    ContentType.APPLICATION_JSON);
            post.setEntity(entity);
        }
        HttpEntity entity = null;
        try {
            HttpResponse response = httpclient.execute(post, context);
            int statusCode = response.getStatusLine().getStatusCode() / 100;
            entity = response.getEntity();
            String result = EntityUtils.toString(response.getEntity());
            if (statusCode == 4 || statusCode == 5) {
                throw new IOException(result);
            }
            if (Void.class.equals(clazz)) {
                return null;
            }
            if (String.class.equals(clazz)) {
                return (T) result;
            }
            if (result == null || result.length() == 0) {
                return null;
            }

            return JsonUtil.fromJson(clazz, result);
        } finally {
            if (entity != null) {
                EntityUtils.consume(entity);
            }
        }
    }

    /**
     * http upload.
     *
     * @param clazz clazz
     * @param url url
     * @param queryParam queryParam
     * @param headers headers
     * @param filePaths filepaths
     * @param <T> T
     * @return T
     * @throws IOException e
     */
    @SuppressWarnings("unchecked")
    public <T> T upload(Class<T> clazz, String url,
                        Map<String, String> queryParam, Map<String, String> headers, List<String> filePaths)
            throws IOException {

        HttpPost post = new HttpPost(url);
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                post.setHeader(key, headers.get(key));
            }
        }
        try {
            if (queryParam != null && queryParam.size() > 0) {
                URIBuilder builder = new URIBuilder(post.getURI());
                for (String key : queryParam.keySet()) {
                    builder.addParameter(key, queryParam.get(key));
                }
                post.setURI(builder.build());
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        if (filePaths != null) {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            for (String path : filePaths) {
                File file = new File(path);
                builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName());
            }
            HttpEntity entity = builder.build();
            post.setEntity(entity);
        }

        HttpEntity entity = null;
        try {
            HttpResponse response = httpclient.execute(post, context);
            int statusCode = response.getStatusLine().getStatusCode() / 100;
            entity = response.getEntity();
            String result = EntityUtils.toString(response.getEntity());
            if (statusCode == 4 || statusCode == 5) {
                throw new IOException(result);
            }
            if (String.class.equals(clazz)) {
                return (T) result;
            }
            if (result == null || result.length() == 0) {
                return null;
            }

            return JsonUtil.fromJson(clazz, result);
        } finally {
            if (entity != null) {
                EntityUtils.consume(entity);
            }
        }
    }

    /**
     * http download.
     *
     * @param url url
     * @param queryParam queryParam
     * @param headers headers
     * @param filePath filepath
     * @throws IOException e
     */
    @SuppressWarnings("unchecked")
    public void download(String url,
                         Map<String, String> queryParam, Map<String, String> headers, String filePath)
            throws IOException {

        HttpPost post = new HttpPost(url);
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                post.setHeader(key, headers.get(key));
            }
        }
        try {
            if (queryParam != null && queryParam.size() > 0) {
                URIBuilder builder = new URIBuilder(post.getURI());
                for (String key : queryParam.keySet()) {
                    builder.addParameter(key, queryParam.get(key));
                }
                post.setURI(builder.build());
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        HttpEntity entity = null;
        try {
            HttpResponse response = httpclient.execute(post, context);
            int statusCode = response.getStatusLine().getStatusCode() / 100;
            entity = response.getEntity();
            if (statusCode == 4 || statusCode == 5) {
                throw new IOException("");
            }
            BufferedInputStream bis = new BufferedInputStream(entity.getContent());
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
            int inByte;
            while ((inByte = bis.read()) != -1) {
                bos.write(inByte);
            }
            bis.close();
            bos.close();
        } finally {
            if (entity != null) {
                EntityUtils.consume(entity);
            }
        }
    }

    /**
     * http downloadzip.
     *
     * @param url url
     * @param queryParam queryParam
     * @param headers headers
     * @param body body
     * @param filePath filepath
     * @throws IOException e
     */
    @SuppressWarnings("unchecked")
    public void downloadzip(String url,
                            Map<String, String> queryParam, Map<String, String> headers, String body, String filePath)
            throws IOException {

        HttpPost post = new HttpPost(url);
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                post.setHeader(key, headers.get(key));
            }
        }
        try {
            if (queryParam != null && queryParam.size() > 0) {
                URIBuilder builder = new URIBuilder(post.getURI());
                for (String key : queryParam.keySet()) {
                    builder.addParameter(key, queryParam.get(key));
                }
                post.setURI(builder.build());
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        if (body != null) {
            HttpEntity entity = new StringEntity(body,
                    ContentType.APPLICATION_JSON);
            post.setEntity(entity);
        }
        HttpEntity entity = null;
        try {
            HttpResponse response = httpclient.execute(post, context);
            int statusCode = response.getStatusLine().getStatusCode() / 100;
            entity = response.getEntity();
            if (statusCode == 4 || statusCode == 5) {
                throw new IOException("");
            }
            BufferedInputStream bis = new BufferedInputStream(entity.getContent());
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
            int inByte;
            while ((inByte = bis.read()) != -1) {
                bos.write(inByte);
            }
            bis.close();
            bos.close();
        } finally {
            if (entity != null) {
                EntityUtils.consume(entity);
            }
        }
    }

    /**
     * http delete.
     *
     * @param clazz clazz
     * @param url url
     * @param queryParam queryParam
     * @param headers headers
     * @param <T> T
     * @return T
     * @throws IOException e
     */
    @SuppressWarnings("unchecked")
    public <T> T delete(Class<T> clazz, String url,
                        Map<String, String> queryParam, Map<String, String> headers,
                        String body) throws IOException {

        GavialHttpDelete delete = new GavialHttpDelete(url);
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                delete.setHeader(key, headers.get(key));
            }
        }
        try {
            if (queryParam != null && queryParam.size() > 0) {
                URIBuilder builder = new URIBuilder(delete.getURI());
                for (String key : queryParam.keySet()) {
                    builder.addParameter(key, queryParam.get(key));
                }
                delete.setURI(builder.build());
            }

        } catch (Exception exception) {
            exception.printStackTrace();

        }
        if (body != null) {
            HttpEntity entity = new StringEntity(body,
                    ContentType.APPLICATION_JSON);
            delete.setEntity(entity);
        }
        HttpEntity entity = null;
        try {
            HttpResponse response = httpclient.execute(delete, context);
            int statusCode = response.getStatusLine().getStatusCode() / 100;
            entity = response.getEntity();
            String result = EntityUtils.toString(response.getEntity());
            if (statusCode == 4 || statusCode == 5) {
                throw new IOException(result);
            }

            if (String.class.equals(clazz)) {
                return (T) result;
            }
            if (result == null || result.length() == 0) {
                return null;
            }

            return JsonUtil.fromJson(clazz, result);
        } finally {
            if (entity != null) {
                EntityUtils.consume(entity);
            }
        }
    }

    private static class GavialHttpDelete extends HttpEntityEnclosingRequestBase {

        static final String METHOD_NAME = "DELETE";

        GavialHttpDelete(final String uri) {
            super();
            setURI(URI.create(uri));
        }

        public GavialHttpDelete(final URI uri) {
            super();
            setURI(uri);
        }

        public GavialHttpDelete() {
            super();
        }

        public String getMethod() {
            return METHOD_NAME;
        }
    }

}

測試:

public class MonitorApp {

    public static void main(String[] args) throws IOException, JSONException {

        //查詢Hadoop信息
        StatefulHttpClient client = new StatefulHttpClient(null);
//        HdfsSummary hdfsSummary = HadoopUtil.getHdfsSummary(client);
//        hdfsSummary.printInfo();

        //查詢HBASE信息
//        HbaseSummary hbaseSummary = HBaseUtil.getHbaseSummary(client);
//        hbaseSummary.printInfo();

        //查詢application的信息
        List<YarnApplication> list = YarnUtils.fetchApp();
        for (YarnApplication yarnApplication:list){
            System.out.println(yarnApplication.toString());
        }

        //查詢yarnClusterMetrics信息
        YarnClusterMetrics yarnClusterMetrics = YarnUtils.fetchClusterMetrics(client);
        Integer numActiveNMs = yarnClusterMetrics.getNumActiveNMs();
        System.out.println(numActiveNMs);

        //查詢隊列資源使用情況
        QueueMetrics queueMetrics = YarnUtils.fetchQueueMetrics(client);
        Integer availableMB = queueMetrics.getAvailableMB();
        System.out.println(availableMB);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章