如果无效则跳过该文件

如果无效则跳过该文件

本文介绍了Spring Batch-验证输入的csv文件中的标题行,如果无效则跳过该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的工作,如下:

I have a simple job as below:

<batch:step id="step">
 <batch:tasklet>
  <batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter" commit-    interval="5000" />
 </batch:tasklet>
</batch:step>

itemReader如下:

itemReader is as below:

<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
 <property name="linesToSkip" value="1"></property>
 <property name="skippedLinesCallback" ref="skippedLinesCallback" ></property>

 <property name="lineMapper">
    <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer" ref="lineTokenizer">
                <property name="delimiter" value="," />
            </bean>
        </property>
        <property name="fieldSetMapper">
            <bean
                class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />
        </property>
    </bean>
</property>
<property name="resource" value="#{stepExecutionContext['inputKeyName']}" />
</bean>

<bean id"lineTokenizer"                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

<bean id="skippedLinesCallback" class="com.test.IteMReaderHeader" >
<property name="lineTokenizer" ref="lineTokenizer">
</bean>

我通过在其中插入"lineTokenizer"来设置"com.test.IteMReaderHeader"类中输入字段的名称".

I am setting the "names" of the input fields in "com.test.IteMReaderHeader" class by injecting "lineTokenizer" in it.

我需要使用固定的标题值验证标题csv,它是输入csv文件中的第一行,如果标题行无效,则在这种情况下,我需要使步骤失败并跳过整个文件,以便下一个文件可用于阅读.

I need to validate the header lines which is the 1st line in the input csv file with a fixed header value and if the header line invalidates then in that case I need to fail the step and skip the entire file so that the next file can be used for reading.

请提出实现此目标的合适方法.非常感谢您的宝贵时间和宝贵的意见.

Please suggest a suitable way of achieving it.I would really appreciate your time and valuable inputs.

谢谢!!

推荐答案

管理FlatFileItemReader文件停止条件的查找代码;

Looking code of FlatFileItemReader file stop condition is managed;

  1. 具有专用字段boolean noInput
  2. 在受保护的doRead()
  3. 中使用了私有function readLine()

恕我直言,最好的解决方案是从skippedLineCallback抛出运行时异常,并将错误作为读取器耗尽的条件进行管理.

IMHO the best solution is to throw a runtime exception from your skippedLineCallback and manage error as reader exhaustion condition.

以这种方式编写代理的敌人示例

Foe example writing your delegate in this way

class SkippableItemReader<T> implements ItemStreamReader<T> {
  private ItemStreamReader<T> flatFileItemReader;
  private boolean headerError = false;

  void open(ExecutionContext executionContext) throws ItemStreamException {
    try {
      flatFileItemReader.open(executionContext);
    } catch(MyCustomExceptionHeaderErrorException e) {
      headerError = true;
    }
  }

  public T read() {
    if(headerError)
      return null;
    return flatFileItemReader.read();
  }

  // Other functions delegation
}

(当然,您必须手动将代表注册为流)
或将FlatFileItemReader扩展为

(you have to register delegate as stream manually,of course)
or extending FlatFileItemReader as

class SkippableItemReader<T> extends FlatFileItemReader<T> {
  private boolean headerError = false;

  protected void doOpen() throws Exception {
    try {
      super.doOpen();
    } catch(MyCustomExceptionHeaderErrorException e) {
      headerError = true;
    }
  }

  protected T doRead() throws Exception {
    if(headerError)
      return null;
    return super.doRead();
  }
}

该代码未经测试直接编写,因此可能会出现错误,但是希望您能理解我的观点.
希望可以解决您的问题

The code has been written directly without test so there can be errors, but I hope you can understand my point.
Hope can solve your problem

这篇关于Spring Batch-验证输入的csv文件中的标题行,如果无效则跳过该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:28