英文:
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<? extends Orders> reader() {
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;
}
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("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);
}
}
});
}
答案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<? 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;
}
You can find more details in the reference documentation here: Late Binding of Job and Step Attributes
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论