博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yarn logs -applicationId命令java版本简单实现
阅读量:4695 次
发布时间:2019-06-09

本文共 4743 字,大约阅读时间需要 15 分钟。

1 import java.io.DataInputStream;  2 import java.io.EOFException;  3 import java.io.FileNotFoundException;  4 import java.io.PrintStream;  5   6 import org.apache.commons.lang.StringUtils;  7 import org.apache.hadoop.conf.Configuration;  8 import org.apache.hadoop.fs.FileContext;  9 import org.apache.hadoop.fs.FileStatus; 10 import org.apache.hadoop.fs.Path; 11 import org.apache.hadoop.fs.RemoteIterator; 12 import org.apache.hadoop.security.UserGroupInformation; 13 import org.apache.hadoop.yarn.api.records.ApplicationId; 14 import org.apache.hadoop.yarn.conf.YarnConfiguration; 15 import org.apache.hadoop.yarn.logaggregation.AggregatedLogFormat; 16 import org.apache.hadoop.yarn.logaggregation.LogAggregationUtils; 17 import org.apache.hadoop.yarn.util.ConverterUtils; 18  19 public class GetYarnLog { 20     public static void main(String[] args) { 21         run("application_1535700682133_0496"); 22     } 23      24     public static int run(String appIdStr) throws Throwable{ 25   26      27          Configuration conf = new YarnConfiguration(); 28          conf.addResource(new Path("/etc/hadoop/conf.cloudera.yarn/core-site.xml")); 29          conf.addResource(new Path("/etc/hadoop/conf.cloudera.yarn/yarn-site.xml")); 30          conf.addResource(new Path("/etc/hadoop/conf.cloudera.yarn/hdfs-site.xml")); 31          if(appIdStr == null || appIdStr.equals("")) 32           { 33              System.out.println("appId is null!"); 34              return -1; 35           } 36          PrintStream out=new PrintStream(appIdStr);  37          ApplicationId appId = null; 38          appId = ConverterUtils.toApplicationId(appIdStr); 39           40          Path remoteRootLogDir = new Path(conf.get("yarn.nodemanager.remote-app-log-dir", "/tmp/logs")); 41  42          String user =  UserGroupInformation.getCurrentUser().getShortUserName();; 43          String logDirSuffix = LogAggregationUtils.getRemoteNodeLogDirSuffix(conf); 44           45          Path remoteAppLogDir = LogAggregationUtils.getRemoteAppLogDir(remoteRootLogDir, appId, user, logDirSuffix); 46          RemoteIterator
nodeFiles; 47 try 48 { 49 Path qualifiedLogDir = FileContext.getFileContext(conf).makeQualified(remoteAppLogDir); 50 nodeFiles = FileContext.getFileContext(qualifiedLogDir.toUri(), conf).listStatus(remoteAppLogDir); 51 } 52 catch (FileNotFoundException fnf) 53 { 54 logDirNotExist(remoteAppLogDir.toString()); 55 return -1; 56 } 57 58 boolean foundAnyLogs = false; 59 while (nodeFiles.hasNext()) 60 { 61 FileStatus thisNodeFile = (FileStatus)nodeFiles.next(); 62 if (!thisNodeFile.getPath().getName().endsWith(".tmp")) 63 { 64 System.out.println("NodeFileName = "+thisNodeFile.getPath().getName()); 65 AggregatedLogFormat.LogReader reader = new AggregatedLogFormat.LogReader(conf, thisNodeFile.getPath()); 66 try 67 { 68 AggregatedLogFormat.LogKey key = new AggregatedLogFormat.LogKey(); 69 DataInputStream valueStream = reader.next(key); 70 for (;;) 71 { 72 if (valueStream != null) 73 { 74 String containerString = "\n\nContainer: " + key + " on " + thisNodeFile.getPath().getName(); 75 76 out.println(containerString); 77 out.println(StringUtils.repeat("=", containerString.length())); 78 try 79 { 80 for (;;) 81 { 82 AggregatedLogFormat.LogReader.readAContainerLogsForALogType(valueStream, out, thisNodeFile.getModificationTime()); 83 84 foundAnyLogs = true; 85 } 86 87 } 88 catch (EOFException eof) 89 { 90 key = new AggregatedLogFormat.LogKey(); 91 valueStream = reader.next(key); 92 93 } 94 95 }else{ 96 break; 97 } 98 } 99 }100 finally101 {102 reader.close();103 }104 }105 }106 if (!foundAnyLogs)107 {108 emptyLogDir(remoteAppLogDir.toString());109 return -1;110 }111 return 0;112 }113 }

 

转载于:https://www.cnblogs.com/lyy-blog/p/9635601.html

你可能感兴趣的文章
python--生成器协程运算
查看>>
php中文字符串截取乱码问题解决
查看>>
INFT 3030 Concurrent Programming
查看>>
小心了,这个设置会导致你的vm重启时被强制重装系统!
查看>>
邮票面值设计 (动态规划+DFS)
查看>>
解决INSTALL_FAILED_MISSING_SHARED_LIBRARY (转载)
查看>>
Linux内核高端内存
查看>>
HTML列表
查看>>
JAVA插入数据到MySql少了8小时
查看>>
【Objective-C学习记录】01-基础概念
查看>>
诗词十四首
查看>>
回望之五:贺兰山与金沙岛
查看>>
两种js获得下一个结点(元素结点)的方法
查看>>
GitHub的使用
查看>>
九度OJ 1078 二叉树遍历(已知前中序求后序)
查看>>
Android 隐式意图的配置
查看>>
2018年2月计划
查看>>
Effective Java 02 Consider a builder when faced with many constructor parameters
查看>>
Python------网络编程2
查看>>
第四章-开心餐厅
查看>>