2017-04-25 96 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)