調用rest api殺死yarn上的應用


調用rest api殺死yarn上的應用

調用yarn reat api,通過app name 獲取application id

    public static String getApplicationID(String appName){
        String getAppsURL = "http://rm:8088/ws/v1/cluster/apps?queue=default";
        String apps = HttpClient.doGet(getAppsURL);
        JSONObject appsJsonObject = JSONObject.parseObject(apps);
        JSONArray appJsonArray = appsJsonObject.getJSONObject("apps").getJSONArray("app");
        JSONObject resultApp = null;
        for(int i=0;i<appJsonArray.size();i++){
            JSONObject tmpApp = appJsonArray.getJSONObject(i);
            String[] str = tmpApp.getString("name").split("\\.");
            if(str[str.length-1].equals(appName)){
                resultApp = tmpApp;
            }
        }
        if(resultApp != null){
            String applicationID = resultApp.getString("id");
            return applicationID;
        }
        return null;
    }

ssh 連接集群內一節點,用yarn application --kill 命令殺死應用

   public static boolean killApplication(String appid,String host,String username,String password) throws IOException{
        Connection conn= new Connection(host);
        Session ssh = null;
        conn.connect();
        boolean isconn=conn.authenticateWithPassword(username, password);
        if(!isconn){
            System.out.println("用戶名稱或者是密碼不正確");
        }else{
            System.out.println("已經連接OK");
            ssh=conn.openSession();
            String command = "yarn application --kill "+appid;
            ssh.execCommand(command);
        }
        ssh.close();
        conn.close();
        return true;
    }

http get請求

   public static String doGet(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;// 返回結果字符串
        try {
            // 創建遠程url連接對象
            URL url = new URL(httpurl);
            // 通過遠程url連接對象打開一個連接,強轉成httpURLConnection類
            connection = (HttpURLConnection) url.openConnection();
            // 設置連接方式:get
            connection.setRequestMethod("GET");
            // 設置連接主機服務器的超時時間:15000毫秒
            connection.setConnectTimeout(15000);
            // 設置讀取遠程返回的數據時間:60000毫秒
            connection.setReadTimeout(60000);
            // 發送請求
            connection.connect();
            // 通過connection連接,獲取輸入流
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                // 封裝輸入流is,並指定字符集
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                // 存放數據
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 關閉資源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            connection.disconnect();// 關閉遠程連接
        }

        return result;
    }

main方法

    public static void main(String[] args) {
        String host = "";
        String username = "";
        String password = "";
        String appName= "";
        String appID=getApplicationID(appName);
        try {
            killApplication(appID,host,username,password);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM