gpt4 book ai didi

spring batch ItemReader FlatFileItemReader 将光标设置为从特定行开始读取或动态设置行跳过

转载 作者:行者123 更新时间:2023-12-05 01:05:21 24 4
gpt4 key购买 nike

在我的 springbatch+quartz 设置中,我正在使用 FlatFileItemReader 读取一个 CSV 文件。我想为读者设置光标以使用给定的读者参数启动下一个作业实例。是否可以?

<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<!-- Read a csv file -->
<property name="resource" value="classpath:cvs/input/report.csv" />

<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="id,impressions" />
</bean>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="report" />
</bean>
</property>
</bean>
</property>

</bean>

这个想法是在下一次执行中继续读取上次发生故障的文件。我为 customWriter 中写入的每一行添加了一个整数“writecursor”。
public void write(List<? extends Report> items) throws Exception {

System.out.println("writer..." + items.size() + " > ");
for(Report item : items){
System.out.println("writing item id: " + item.getId());
System.out.println(item);

}
//getting stepExecution by implementing StepExecutionListener
this.stepExecution.getExecutionContext().putInt("writecursor", ++writecursor);

}

现在,在 customItemReadListener 中,我想获取更新的 writecursor 值,然后从顶部跳过行开始从 writecursor 读取
public class CustomItemReaderListener implements ItemReadListener<Report>, StepExecutionListener {

ApplicationContext context = ApplicationContextUtils.getApplicationContext();
private StepExecution stepExecution;
@Override
public void beforeRead() {
//Skip lines somehow
}

我认为可能的解决方案的另一件事是在 itemreader 中动态设置linestoskip。这里有一个线程 http://thisisurl.com/dynamic-value-for-linestoskip-in-itemreader但还没有回答。和这里,
http://forum.spring.io/forum/spring-projects/batch/100950-accessing-job-execution-context-from-itemwriter

最佳答案

使用FlatFileItemReader.linesToSkip属性设置注入(inject)作业参数值。

<bean id="myReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="linesToSkip" value="file:#{jobParameters['cursor']}" />
</bean>

关于spring batch ItemReader FlatFileItemReader 将光标设置为从特定行开始读取或动态设置行跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21477314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com