文章目录

  • 系列文章目录
  • 前言
  • 一、偷懒,百度方案
  • 二、优雅高效方案
  • 总结



前言

提交一个flink作业,希望获取这个作业的jobId,以便后续程序监控,例如获取checkpoint路径,从checkpoint点重启


一、偷懒,百度方案

查回来的是,通过jobname去查,或者从提交的日志文件中解析出jobId,这种方案,实在看不上!

二、优雅高效方案

方法1.直接从env中获取:

JobClient jobClient = env.executeAsync();
      JobID jobID = jobClient.getJobID();
      System.out.println("-----拿到jobId了:"+jobID);

方法2:执行sql的话, 或者id的方法:

TableResult tableResult = tableEnv.executeSql(temple.sourceToIcebergSinkSql());
//        tableEnv.executeSql("select * from "+ temple.mysqlTableName()).print();
        //TODO 6.执行任务

        Optional<JobClient> jobClient1 = tableResult.getJobClient();
        if(jobClient1.isPresent()){
            System.out.println("-----拿到jobId了:"+jobClient1.get().getJobID());
        }else{
            System.out.println("jobClient不存在");
        }

如何知道这个方法?

  1. 提交任务会返回: Job has been submitted with JobID bfc603a0404f9bb91f2b6bd14657fb09
  2. 把 Job has been submitted with JobID 到源码中找一下
@Override
    public JobClient executeAsync(StreamGraph streamGraph) throws Exception {
        validateAllowedExecution();
        final JobClient jobClient = super.executeAsync(streamGraph);

        if (!suppressSysout) {
            System.out.println("Job has been submitted with JobID " + jobClient.getJobID());
        }

        return jobClient;
    }

上面源码中,不就jobClient.getJobId嘛,哈哈


总结

百度不好用时,还是要看真本领!