通过yarn的rest接口获取文件信息或者任务app的日志信息

通过yarn的rest接口获取文件信息或者任务app的日志信息,即可以打开查看文件内容,也可以查看应用日志返回json格式。

import java.io.IOException;

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;

/**

  • This example demonstrates the use of the {@link HttpClientResponseHandler} to simplify

  • the process of processing the HTTP response and releasing associated resources.
    */
    public class ClientWithResponseHandler {

    public static void main(final String[] args) throws Exception {
    try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
    //final HttpGet httpget = new HttpGet(“http://linux01:14000/webhdfs/v1/words.txt?op=OPEN&user.name=admin”);
    //final HttpGet httpget = new HttpGet(“http://linux01:50070/webhdfs/v1/words.txt?op=OPEN”);
    final HttpGet httpget = new HttpGet(“http://linux01:8088/ws/v1/cluster/apps/application_1600303805098_0002/state”);
    //final HttpGet httpget = new HttpGet(“http://linux01:8088/ws/v1/cluster/apps”);
    //final HttpGet httpget = new HttpGet(“http://linux01:50070/webhdfs/v1/?op=liststatus&user.name=admin”);
    //final HttpGet httpget = new HttpGet(“http://linux01:14000/webhdfs/v1/output?op=LISTSTATUS&user.name=admin”);
    System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());

         // Create a custom response handler
         final HttpClientResponseHandler<String> responseHandler = new HttpClientResponseHandler<String>() {
    
             @Override
             public String handleResponse(
                     final ClassicHttpResponse response) throws IOException {
                 final int status = response.getCode();
                 if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
                     final HttpEntity entity = response.getEntity();
                     try {
                         return entity != null ? EntityUtils.toString(entity) : null;
                     } catch (final ParseException ex) {
                         throw new ClientProtocolException(ex);
                     }
                 } else {
                     throw new ClientProtocolException("Unexpected response status: " + status);
                 }
             }
    
         };
         final String responseBody = httpclient.execute(httpget, responseHandler);
         System.out.println("----------------------------------------");
         System.out.println(responseBody);
     }
    

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取YARN资源池各节点信息,可以使用YARN的Java API。 以下是获取YARN资源池各节点信息的示例代码: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.yarn.api.records.NodeReport; import org.apache.hadoop.yarn.client.api.YarnClient; import org.apache.hadoop.yarn.client.api.YarnClientFactory; import org.apache.hadoop.yarn.conf.YarnConfiguration; import java.util.List; public class YarnNodeReport { public static void main(String[] args) throws Exception { // 创建Yarn客户端 Configuration conf = new YarnConfiguration(); YarnClient yarnClient = YarnClientFactory.createYarnClient(); yarnClient.init(conf); yarnClient.start(); // 获取节点报告 List<NodeReport> nodeReports = yarnClient.getNodeReports(); // 输出各节点信息 for (NodeReport nodeReport : nodeReports) { System.out.println("Node ID: " + nodeReport.getNodeId().toString()); System.out.println("Node HTTP Address: " + nodeReport.getHttpAddress()); System.out.println("Node Health Report: " + nodeReport.getHealthReport()); System.out.println("Node Labels: " + nodeReport.getNodeLabels()); System.out.println("Node State: " + nodeReport.getNodeState().toString()); System.out.println("Node Total Resource: " + nodeReport.getTotalCapability().toString()); } // 关闭Yarn客户端 yarnClient.stop(); } } ``` 这段代码使用YARN的Java API获取节点报告,输出各节点的信息,包括节点ID、HTTP地址、健康报告、标签、状态和总资源等信息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值