2017-04-25 85 views

回答

3

是的,你可以。您可以通过YarnClient获取有关应用程序的大部分关键信息,并可以拨打Spark History Server API。您正在寻找这里的终点是

/applications/[base-app-id]/logs 
+0

'YarnClient'让您检索有关应用程序的一些信息,但遗憾的是没有日志:(你可以用放电的REST API方法检索日志'/应用/ [基础应用ID ]/logs“,但是至少从我的应用程序中得到的stdout似乎从这些日志中遗漏了,即使你使用'yarn logs -applicationId -log_files stdout'时你可以看到stdout,我真的需要一种方法从Java以编程方式检索stdout或者我可能需要我的应用程序才能登录到Spark而不是... – snark

2

你在shell环境的方法是正确的!

在我看来,因为纱线已经是一个可执行程序在你的系统中。

使当前java进程(即当前jvm)能够访问并使用它。你可以启动一个新的子进程来帮助你完成这项工作。

也许随后的代码将帮助你。

public class YarnLog { 
    // 
    public static void getYarnLog(String appid) throws IOException { 
     BufferedReader br = null; 
     try { 
      Process p = Runtime.getRuntime().exec(String.format("yarn logs -applicationId %s", appid)); 
      br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String line; 
      while((line = br.readLine()) != null) { 
       System.out.println(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if(br != null) { 
       br.close(); 
      } 
     } 
    } 
} 

该子进程成功终止后,您可以使用您的具体日志为正常文件在你当前的工作目录

1

我想这样做编程使用Java,所以我最后看了一眼在命令后面的代码:

yarn logs -applicationId applicationid 

它是:

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

我现在在检索日志一个字符串(内容)。该代码是:

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)