使用hadoop api获取任务日志(续)

使用hadoop api获取任务日志
在上一篇文章写到了如何使用hadoop api获取任务日志,只是最后贴出了日志输出的内容,没有写清楚到底如何输出日志。最近有小伙伴私信我如何输出日志,通过这篇文章进行一个补充,有不明白的地方可以私信或者留言。如果没有及时回复,可以添加我的微信。

@Test
    public void printLog() throws IOException {
        //假设我们已经通过其他的方式拿到了applicationId 一般都是通过livy 提供的rest api接口获取
        String applicationId = "application_1587284642166_0001";

        /**
         * 根据applicationId 获取containerId 和 nodeId 其中appOwner可能是租户,
         * 也可能是代理用户,根据业务自行设置,本地测试的时候我并没有创建hadoop user,
         * 所以直接给root
         */
        Map<String, String> map = HadoopLogUtils.getContaines(applicationId, "root");
        map.forEach((k, v) ->{
            System.out.println("containeId is: " + k + "nodeId is: " + v);
        });

        /**
         * 根据 containeId 和 nodeId 拿到日志 输出到控制台或者写入文件
         */
        
        AtomicInteger count = new AtomicInteger(0);
        map.forEach((containeId, nodeId) -> {
            OutputStream in = null;
            try {
                File file = new File("E:\\openSource\\hadoop_learning\\target\\" + containeId + "_" + count.get() + ".txt");
                if (!file.exists()) {
                    file.createNewFile();
                }
                in = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //如果不写入文件 直接使用 PrintStream printStream = System.out
            PrintStream printStream = new PrintStream(in);
            List<String> logType = new ArrayList<>(1);
            //hadoop 2.X 日志类型有 stdout stderr 3.X 有stdout gc.current
            logType.add("stderr");
            try {
                HadoopLogUtils.dumpAContainersLogs(applicationId,
                        containeId,
                        nodeId,
                        "root", printStream ,logType);
            } catch (IOException e) {
                e.printStackTrace();
            }
            count.getAndIncrement();
        });

    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值