调用rest api杀死yarn上的应用

HJWZYY 2019-12-13

调用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();
        }
    }

相关推荐

Notzuonotdied / 0评论 2020-04-20
硅步至千里 / 0评论 2020-04-19
tomli / 0评论 2020-03-04