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

发布时间:2026/10/11 4:16:51
Flink任务获取yarn上的运行日志 flink任务在yarn失败需要从yarn的归档服务器当中获取运行日志用来排查异常原因正在运行中的任务日志从当前运行的Node节点中读取方便监控任务运行信息。非运行状态的任务日志获取//构建日志请求对象 ContainerLogsRequest request new ContainerLogsRequest(); request.setAppId(report.getApplicationId()); request.setAppFinished(report.getYarnApplicationState().equals(YarnApplicationState.FINISHED)); SetString logs new HashSet(); request.setLogTypes(logs); request.setBytes(Long.MAX_VALUE); request.setAppOwner(report.getUser()); ListApplicationAttemptReport attempts client.getApplicationAttempts(report.getApplicationId()); request.setContainerId(attempts.getLast().getAMContainerId().toString()); //构建log聚合日志工厂类 LogAggregationFileControllerFactory fileControllerFactory new LogAggregationFileControllerFactory(configuration); LogAggregationFileController fileController fileControllerFactory.getFileControllerForRead(report.getApplicationId(), report.getUser()); //获取日志信息 ListContainerLogMeta 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); //获取当前任务的集群状态 ListApplicationAttemptReport attempts client.getApplicationAttempts(report.getApplicationId()); //遍历集群获取集群下当前容器的报告 for (ApplicationAttemptReport attempt : attempts) { ListContainerReport 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(); MapString,ListContainerLogFileInfo 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); ListContainerLogFileInfo logFileInfos info.getObject(containerLogInfo,new TypeReferenceListContainerLogFileInfo(){}); //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(); ListContainerLogFileInfo 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); } }); }); } }

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询