如何通过yarnclient在代码中获得日志聚合

sqserrrh  于 2021-05-31  发布在  Hadoop
关注(0)|答案(1)|浏览(581)

下面是关于用javaapi创建yarnapp的例子。
https://github.com/hortonworks/simple-yarn-app
工作正常,但是,日志只存在于执行之后,日志就消失了。
我怎么能用密码抓到这个?或者启用一个选项?

bxpogfeg

bxpogfeg1#

应用程序完成后,您可以使用logclihelpers按应用程序id查找日志:

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext;
import org.apache.hadoop.yarn.client.api.YarnClientApplication;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.exceptions.YarnException;
import org.apache.hadoop.yarn.logaggregation.LogCLIHelpers;

import java.io.IOException;
import java.io.PrintStream;

public static void getLogs(YarnConfiguration conf, YarnClientApplication app) throws IOException, YarnException {
    ApplicationSubmissionContext appContext =
            app.getApplicationSubmissionContext();
    ApplicationId appId = appContext.getApplicationId();
    LogCLIHelpers logCLIHelpers = new LogCLIHelpers();
    logCLIHelpers.setConf(conf);
    FileSystem fs = FileSystem.get(conf);
    Path logFile = new Path("/path/to/log/file.log");
    fs.create(logFile, false);
    try (PrintStream printStream = new PrintStream(logFile.toString())) {
      logCLIHelpers.dumpAllContainersLogs(appId, UserGroupInformation.getCurrentUser().getShortUserName(), printStream);
    }
}

相关问题