简体   繁体   English

查询Spring批处理中的批处理作业元数据

[英]Query batch job metadata in Spring batch

I want to fetch the 10 latest records from the BATCH_JOB_EXECUTION-table joined with the BATCH_JOB_INSTANCE-table. 我想从与BATCH_JOB_INSTANCE表连接的BATCH_JOB_EXECUTION表中获取10条最新记录。

So how can I access these tables? 那么如何访问这些表呢?

In this application I have used Spring Data JPA. 在这个应用程序中,我使用了Spring Data JPA。 It's another application which uses Spring Batch and created these tables. 这是另一个使用Spring Batch并创建这些表的应用程序。 In other words, I would just like to run a JOIN-query and map it directly to my custom object with just the necessary fields. 换句话说,我只想运行一个JOIN查询并将其直接映射到我的自定义对象,只需要必要的字段。 As far as it's possible, I would like to avoid making seperate models for the two tables. 尽可能,我想避免为这两个表制作单独的模型。 But I don't know the best approach here. 但我不知道这里最好的方法。

If you want to do it from Spring Batch code you need to use JobExplorer and apply filters on either START_TIME or END_TIME . 如果您想从Spring Batch代码中执行此操作,则需要使用JobExplorer并在START_TIMEEND_TIME上应用过滤器。 Alternatively, just send an SQL query with your desired JOIN to the DB using JDBC. 或者,只需使用JDBC将带有所需JOINSQL查询发送到数据库。 The DDLs of the metadata tables can be found here . 可以在此处找到元数据表的DDLs

EDIT 编辑

If you want to try to do it in SpringBatch, I guess you need to iterate through JobExecutions and find the ones that interest you, then do your thing )) someth. 如果你想在SpringBatch中尝试这样做,我想你需要迭代JobExecutions并找到你感兴趣的那些,然后做你的事情))。 like: 喜欢:

List<JobInstance> jobInstances = jobExplorer.getJobInstances(jobName);

for (JobInstance jobInstance : jobInstances) {
    List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
    for (JobExecution jobExecution : jobExecutions) {
        if (//jobExecution.getWhatever...)) {
        // do your thing...
        }
     }
} 

Good Luck! 祝好运!

Since JobExplorer doesn't have the interface .getJobInstances(jobName) anymore, I have done this (this example with BatchStatus as a condition) adapted with streams : 由于JobExplorer不再具有接口.getJobInstances(jobName) ,我已经完成了这个(使用BatchStatus作为条件的这个例子)适应了流:

List<JobInstance> lastExecutedJobs = jobExplorer.getJobInstances(jobName, 0, Integer.MAX_VALUE);

Optional<JobExecution> jobExecution = lastExecutedJobs
                                .stream()
                                .map(jobExplorer()::getJobExecutions)
                                .flatMap(jes -> jes.stream())
                                .filter(je -> BatchStatus.COMPLETED.equals(je.getStatus()))
                                .findFirst();

To return N elements, you could use others capacities of stream (limit, max, collectors, ...). 要返回N个元素,您可以使用其他流的容量(limit,max,collectors,...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM