Flink任务获取yarn上的运行日志

flink任务在yarn失败,需要从yarn的归档服务器当中获取运行日志用来排查异常原因,正在运行中的任务日志从当前运行的Node节点中读取,方便监控任务运行信息。

非运行状态的任务日志获取
//构建日志请求对象
ContainerLogsRequest request = new ContainerLogsRequest();
request.setAppId(report.getApplicationId());
request.setAppFinished(report.getYarnApplicationState().equals(YarnApplicationState.FINISHED));
Set<String> logs = new HashSet<>();
request.setLogTypes(logs);
request.setBytes(Long.MAX_VALUE);
request.setAppOwner(report.getUser());
 
//构建log聚合日志工厂类
LogAggregationFileControllerFactory fileControllerFactory = new LogAggregationFileControllerFactory(configuration);
LogAggregationFileController fileController = fileControllerFactory.getFileControllerForRead(report.getApplicationId(), report.getUser());
//获取日志信息
List<ContainerLogMeta> logMetas = fileController.readAggregatedLogsMeta(request);
 
//打印容器日志信息,并且将容器所有内容放入其中
logMetas.forEach(containerLogMeta -> {
    System.out.println(String.format("容器id:%s 节点id:%s", containerLogMeta.getContainerId(), containerLogMeta.getNodeId()));
    containerLogMeta.getContainerLogMeta().forEach(fileInfo -> {
        System.out.println(String.format("文件名称:%s,文件大小:%s bytes", fileInfo.getFileName(), fileInfo.getFileSize()));
        logs.add(fileInfo.getFileName());
    });
});
//读取日志
fileController.readAggregatedLogs(request, System.out);
运行中状态的任务日志获取
YarnConfiguration configuration = YarnClientUtil.buildConfig();
        YarnClientImpl client = YarnClientUtil.client(configuration);
        //获取yarn上app的状态
        ApplicationReport report = client.getApplicationReport(ApplicationId.fromString(appId));
 
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setConnectTimeout(3, TimeUnit.SECONDS);
        okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);
 
        //获取当前任务的集群状态
        List<ApplicationAttemptReport> attempts = client.getApplicationAttempts(report.getApplicationId());
 
        //遍历集群,获取集群下当前容器的报告
        for (ApplicationAttemptReport attempt : attempts) {
            List<ContainerReport> containers = client.getContainers(attempt.getApplicationAttemptId());
            //获取当前容器下的日志列表
            for (ContainerReport containerReport : containers) {
                //构造获取容器下的日志文件请求
                Request logReq = new Request.Builder().url(String.join("/", new String[]{containerReport.getNodeHttpAddress(), "ws", "v1", "node", "containers"
                                , containerReport.getContainerId().toString(), "logs"}))
                        .get().header("Accept", "application/json").build();
                Call call = okHttpClient.newCall(logReq);
                Response response = call.execute();
                Map<String,List<ContainerLogFileInfo>> logType = new HashMap<>();
                if (response.isSuccessful()) {
                    String resString = response.body().string();
                    //反序列化日志对象
                    JSONObject obj = JSONObject.parseObject(resString);
                    JSONObject info = obj.getJSONObject("containerLogsInfo");
                    if (Objects.nonNull(info)) {
                        String containerId = info.getString("containerId");
                        List<ContainerLogFileInfo> logFileInfos = info.getObject("containerLogInfo",new TypeReference<List<ContainerLogFileInfo>>(){});
                        //System.out.println(String.format("containerLog %s on %s",info.getContainerId(),info.getNodeId()));
                        logType.put(containerId,logFileInfos);
                        //遍历打印容器日志文件信息
                        logFileInfos.forEach(log -> {
                            System.out.println(String.format("容器id:%s 文件名:%s 大小:%s (bytes)",
                                    containerId, log.getFileName(), log.getFileSize()));
                        });
                    }
                } else {
                    throw new RuntimeException("获取日志信息出错");
                }
 
                //遍历日志文件信息,并且日志内容
                logType.entrySet().forEach(entry->{
                    String containerId = entry.getKey();
                    List<ContainerLogFileInfo> fileInfoList = entry.getValue();
                    fileInfoList.forEach(fileInfo -> {
 
                        Request logContentReq = new Request.Builder().url(String.join("/", new String[]{containerReport.getNodeHttpAddress(), "ws", "v1", "node", "containers"
                                , containerId, "logs",fileInfo.getFileName()})).build();
                        try {
                            Response fileResp = okHttpClient.newCall(logContentReq).execute();
                            if(fileResp.isSuccessful()){
                                IOUtils.copy(fileResp.body().byteStream(), System.out);
                            }
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    });
                });
            }
        }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风卷残尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值