c语言调用rest api,调用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

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值