Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve yarn's logs programmatically using java

I actually retrieve my spark application's logs on a linux shell with :

yarn logs -applicationId applicationid

Is there any way to retrieve it programmatically using java ?

like image 499
Tremo Avatar asked Feb 05 '23 14:02

Tremo


1 Answers

I wanted to do it programmatically using java, so i finally took a look at the code behind the command :

yarn logs -applicationId applicationid

which is in :

src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java

I now retrieve the logs in a string (content). The code is:

String applicationId = "application_1492795815045_3940";
ApplicationId appId = appId = ConverterUtils.toApplicationId(applicationId);
LogCLIHelpers logCliHelper = new LogCLIHelpers();
Configuration config = new Configuration();
logCliHelper.setConf(config);
String appOwner = UserGroupInformation.getCurrentUser().getShortUserName();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
// Function to retrieve logs
logCliHelper.dumpAllContainersLogs(appId, appOwner, ps);
String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);
System.out.println(content)
like image 113
Tremo Avatar answered Feb 07 '23 18:02

Tremo