自定义项目读取器,使用 JdbcPagingItemReader?

huangapple go评论58阅读模式
英文:

Custom Item Reader that uses JdbcPagingItemReader?

问题

以下是您要翻译的内容:

我可以在批处理配置中找到以下代码段时从我的数据库中读取数据:

@Bean
public ItemReader<? extends Orders> reader() {

    JdbcPagingItemReader<Orders> reader = new JdbcPagingItemReader<>();
    final SqlPagingQueryProviderFactoryBean sqlPagingQueryProviderFactoryBean = new SqlPagingQueryProviderFactoryBean();
    sqlPagingQueryProviderFactoryBean.setDataSource(dataSource);
    sqlPagingQueryProviderFactoryBean.setSelectClause("select *");
    sqlPagingQueryProviderFactoryBean.setFromClause("from orders");
    sqlPagingQueryProviderFactoryBean.setWhereClause(
        "where order_status=var1 and date < var2");
    sqlPagingQueryProviderFactoryBean.setSortKey("date");

    try {
        reader.setQueryProvider(sqlPagingQueryProviderFactoryBean.getObject());
    } catch (Exception e) {
        e.printStackTrace();
    }

    reader.setDataSource(dataSource);
    reader.setPageSize(3);
    reader.setRowMapper(new BeanPropertyRowMapper<Orders>(Orders.class));
    return reader;
}

然而,我无法从我的Restful API 动态地将变量传递给此 ItemReader。根据我所了解,我应该创建一个自定义的 ItemReader,但我不确定如何利用 JdbcPagingItemReader 来创建它。是否有人能帮我解决这个问题?谢谢!

public void orderProcess(String var1) {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    JobParameters jobParameters = new JobParametersBuilder()
                        .addString("var1", var1)
                        .addString("var", "sampleValue1")
                        .addString("time",
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")
                                .format(Calendar.getInstance().getTime()))
                        .addDate("date", new Date()).toJobParameters();
                    jobLauncher.run(job, jobParameters);
                } catch (Exception e) {
                    LogUtil.logError(LOGGER, "ERROR IN STARTING JOB", e);
                }
            }
        });
    }
英文:

I am able to read from my db when this code snippet is in my batchConfig

 @Bean
public ItemReader&lt;? extends Orders&gt; reader() {

    JdbcPagingItemReader&lt;Orders&gt; reader = new JdbcPagingItemReader&lt;Orders&gt;();
    final SqlPagingQueryProviderFactoryBean sqlPagingQueryProviderFactoryBean = new SqlPagingQueryProviderFactoryBean();
    sqlPagingQueryProviderFactoryBean.setDataSource(dataSource);
    sqlPagingQueryProviderFactoryBean.setSelectClause(&quot;select *&quot;);
    sqlPagingQueryProviderFactoryBean.setFromClause(&quot;from orders&quot;);
    sqlPagingQueryProviderFactoryBean.setWhereClause(
        &quot;where order_status=var1 and date &lt; var2&quot;);
    sqlPagingQueryProviderFactoryBean.setSortKey(&quot;date&quot;);

    try {
        reader.setQueryProvider(sqlPagingQueryProviderFactoryBean.getObject());
    } catch (Exception e) {
        e.printStackTrace();
    }

    reader.setDataSource(dataSource);
    reader.setPageSize(3);
    reader.setRowMapper(new BeanPropertyRowMapper&lt;Orders&gt;(Orders.class));
    return reader;
}

However, I am not able to pass variables to this ItemReader dynamically from my Restful API. From what I read, I should create a custom item reader however I am not sure how to create that leveraging JdbcPagingItemReader.

Would any of you help me on this one? Thanks!

public void orderProcess(String var1) {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    JobParameters jobParameters = new JobParametersBuilder()
                        .addString(&quot;var1&quot;, var1)
                        .addString(&quot;var&quot;, &quot;sampleValue1&quot;)
                        .addString(&quot;time&quot;,
                            new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss.S&quot;)
                                .format(Calendar.getInstance().getTime()))
                        .addDate(&quot;date&quot;, new Date()).toJobParameters();
                    jobLauncher.run(job, jobParameters);
                } catch (Exception e) {
                    LogUtil.logError(LOGGER, &quot;ERROR IN STARTING JOB&quot;, e);
                }
            }
        });
    }

答案1

得分: 0

在这种情况下,您应该将您的项目读取器(Item Reader)设置为步骤范围(step-scoped),以便动态地将作业参数注入其配置中。以下是一个示例:

@Bean
public ItemReader<? extends Orders> reader(
                    @Value("#{jobParameters['var1']}") String var1,
                    @Value("#{jobParameters['var2']}") String var2) {

    JdbcPagingItemReader<Orders> reader = new JdbcPagingItemReader<Orders>();
    final SqlPagingQueryProviderFactoryBean sqlPagingQueryProviderFactoryBean = new SqlPagingQueryProviderFactoryBean();
    sqlPagingQueryProviderFactoryBean.setDataSource(dataSource);
    sqlPagingQueryProviderFactoryBean.setSelectClause("select *");
    sqlPagingQueryProviderFactoryBean.setFromClause("from orders");
    sqlPagingQueryProviderFactoryBean.setWhereClause(
        "where order_status=" + var1 + " and date < " + var2);
    sqlPagingQueryProviderFactoryBean.setSortKey("date");

    try {
        reader.setQueryProvider(sqlPagingQueryProviderFactoryBean.getObject());
    } catch (Exception e) {
        e.printStackTrace();
    }

    reader.setDataSource(dataSource);
    reader.setPageSize(3);
    reader.setRowMapper(new BeanPropertyRowMapper<Orders>(Orders.class));
    return reader;
}

您可以在参考文档中找到更多详细信息:作业和步骤属性的后期绑定

英文:

> Hi @MahmoudBenHassine, i want to pass it using job parameters, i added in the question

In that case, you should make your item reader step-scoped in order to inject job parameters in its configuration dynamically. Here is an example:

 @Bean
public ItemReader&lt;? extends Orders&gt; reader(
                    @Value(&quot;#{jobParameters[&#39;var1&#39;]}&quot;) String var1,
                    @Value(&quot;#{jobParameters[&#39;var2&#39;]}&quot;) String var2) {

    JdbcPagingItemReader&lt;Orders&gt; reader = new JdbcPagingItemReader&lt;Orders&gt;();
    final SqlPagingQueryProviderFactoryBean sqlPagingQueryProviderFactoryBean = new SqlPagingQueryProviderFactoryBean();
    sqlPagingQueryProviderFactoryBean.setDataSource(dataSource);
    sqlPagingQueryProviderFactoryBean.setSelectClause(&quot;select *&quot;);
    sqlPagingQueryProviderFactoryBean.setFromClause(&quot;from orders&quot;);
    sqlPagingQueryProviderFactoryBean.setWhereClause(
        &quot;where order_status=&quot; + var1 + &quot; and date &lt; &quot; + var2);
    sqlPagingQueryProviderFactoryBean.setSortKey(&quot;date&quot;);

    try {
        reader.setQueryProvider(sqlPagingQueryProviderFactoryBean.getObject());
    } catch (Exception e) {
        e.printStackTrace();
    }

    reader.setDataSource(dataSource);
    reader.setPageSize(3);
    reader.setRowMapper(new BeanPropertyRowMapper&lt;Orders&gt;(Orders.class));
    return reader;
}

You can find more details in the reference documentation here: Late Binding of Job and Step Attributes

huangapple
  • 本文由 发表于 2023年5月25日 16:26:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定