• 调用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("
    ");
                    }
                    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();
            }
        }
    
  • 相关阅读:
    关于软件设计中遇到的问题
    关于power shell
    如果TChart的发生异常
    重温gof版《设计模式》中的创建型模式
    如何更好的使用cvs
    存储过程返回临时表的问题
    关于bugzilla与svn结合的配置注意事项
    VC知识点:如何用vc6调试CGI程序
    如何让应用程序托盘化
    符号表
  • 原文地址:https://www.cnblogs.com/xiaodf/p/12036476.html
Copyright © 2020-2023  润新知