通过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); }
}
}