简体   繁体   English

如何向Spring Batch项目阅读器添加参数?

[英]How do I add parameters to a Spring Batch item reader?

I feel like this is a really basic thing, but I can't find docs on how to do it. 我觉得这是一个非常基本的东西,但我找不到关于如何做的文档。 All the documentation/examples I'm finding assumes static queries. 我发现的所有文档/示例都假定为静态查询。 I can do this as a static query, but I want to know how to do it with variables. 我可以将其作为静态查询,但我想知道如何使用变量。 I'm trying to use Spring Batch with Postgres. 我正在尝试使用Postgres的Spring Batch。

What I'm looking to do is a query like this: 我想要做的是这样的查询:

SELECT * from SOME_TABLE WHERE SOURCE = ? AND (EXPIRES BETWEEN ? AND ?)

I've tried various ways of write the query, eg replacing question marks with variables like :source . 我已经尝试了各种编写查询的方法,例如用变量替换问号,例如:source I'm not even sure if I'm using the correct ItemReader classes or if I need to write my own. 我甚至不确定我是否正在使用正确的ItemReader类或者我是否需要编写自己的类。 This is my config: 这是我的配置:

@Bean
protected JdbcPagingItemReader<JpaEntitlement> itemReader(DataSource dataSource)
        throws Exception {
    JdbcPagingItemReader<JpaEntitlement> pagingItemReader = new JdbcPagingItemReader<>();
    pagingItemReader.setDataSource(dataSource);
    pagingItemReader.setPageSize(1);

    PagingQueryProvider pagingQueryProvider = createQueryProvider(dataSource);
    pagingItemReader.setQueryProvider(pagingQueryProvider);
    pagingItemReader.setRowMapper(new BeanPropertyRowMapper<>(JpaClass.class));
    return pagingItemReader;
}

private PagingQueryProvider createQueryProvider(DataSource dataSource) throws Exception {
    SqlPagingQueryProviderFactoryBean pagingQueryProvider =
            new SqlPagingQueryProviderFactoryBean();
    pagingQueryProvider.setSelectClause("*");
    pagingQueryProvider.setFromClause("FROM SOME_TABLE");
    pagingQueryProvider.setWhereClause("WHERE SOURCE = ? AND (EXPIRES between ? AND ?)");
    pagingQueryProvider.setDataSource(dataSource);
    return pagingQueryProvider.getObject();
}

I guess the ultimate question is this: Is there something included in Spring Batch to do this? 我想最终的问题是:Spring Batch中是否包含了这样的内容? If not, what should I override to add this functionality? 如果没有,我应该覆盖什么才能添加此功能?

To add, this is something that need to process in batches, as it's going to be potentially thousands of records. 要添加,这需要批量处理,因为它可能会有数千条记录。

If you know the parameters at start of batch then pass parameters as Jobparamters . 如果您知道批处理开始时的参数, Jobparamters参数作为Jobparamters传递。 You can now access Jobparamters in Reader using @ StepScope . 您现在可以使用StepScope访问Reader中的StepScope Below is sample code for accessing job parameters in reader 以下是用于访问阅读器中的作业参数的示例代码

@Bean
protected JdbcPagingItemReader<JpaEntitlement> itemReader(@Value("#{jobParameters['someparameter']}") String someparameter DataSource dataSource)
        throws Exception {
    JdbcPagingItemReader<JpaEntitlement> pagingItemReader = new JdbcPagingItemReader<>();
    pagingItemReader.setDataSource(dataSource);
    pagingItemReader.setPageSize(1);

    PagingQueryProvider pagingQueryProvider = createQueryProvider(dataSource);
    pagingItemReader.setQueryProvider(pagingQueryProvider);
    pagingItemReader.setRowMapper(new BeanPropertyRowMapper<>(JpaClass.class));
    return pagingItemReader;
}

Hope this helps 希望这可以帮助

This is similar to this question 这与此问题类似

I feel like this is a really basic thing, but I can't find docs on how to do it. 我觉得这是一个非常基本的东西,但我找不到关于如何做的文档。

The relevant section from the reference documentation is Late Binding of Job and Step Attributes which provides code examples of how to use the JobScope and StepScope . 参考文档的相关部分是作业和步骤属性的后期绑定,它提供了如何使用JobScopeStepScope代码示例。 The idea is that you can dynamically bind your query attributes in your reader lately at runtime (instead of eagerly at configuration time) either from job parameters or from the job/step execution context. 我们的想法是,您可以在作业参数或作业/步骤执行上下文中最近在运行时(而不是急切地在配置时)动态地将查询属性绑定到读取器中。

Hope this helps. 希望这可以帮助。

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

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