spring批处理flatfileitemreader令牌错误

dwthyt8l  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(303)

我的文件系统中有两个csv文件,我需要根据某些请求类型使用spring批处理。
请求类型的文件:eod(不带标头)

12345,BBG
23232,BBG

请求类型的另一个文件:古文(没有标题)

12345,BBG,20201115
23232,BBG,20201115

两个文件都必须有前两个字段, id 以及 source . 古文件可以有第3和第4字段 startDate 以及 endDate 如何创建 FlatFileItemReader 对两个文件都有效?目前我有一些类似于:
我的itemreader实现读取csv文件:

@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
public FlatFileItemReader<FixingDTO> fileReader(@Value("#{jobExecutionContext['type']}") String type) {
    SomeType type = forName(type);
    return new FlatFileItemReaderBuilder<MyDTO>()
            .name("fileReader")
            .resource(new ClassPathResource(MAP.get(type)))
            .delimited()
            .delimiter(",")
            .names("id", "source", "startDate", "endDate")
            .fieldSetMapper(myDTOMapper())
            .build();
}

字段Map器:

public class MyDTOFieldMapper implements FieldSetMapper<MyDTO> {

    @Override
    public MyDTO  mapFieldSet(FieldSet fieldSet) throws BindException {
        MyDTO dto = new MyDTO();

        dto.setId(fieldSet.readString("id"));
        dto.setSource(fieldSet.readString("source"));
        dto.setStartDate(formatDate(fieldSet.readString("startDate")));
        dto.setEndDate(formatDate(fieldSet.readString("endDate")));

        return dto;
    }

    private LocalDate formatDate(String dateString) {
        return LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
    }
}

运行作业时出现令牌异常:

Caused by: org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record: expected 4 actual 2

我只想处理行中的任何内容,并将其分配给对象变量。startdate和enddate可以为空。

8yparm6h

8yparm6h1#

这是一个领域 strict (默认值:true)内部 DelimitedLineTokenizer ,那正是你要找的。你应该把那个字段设置为false。
行为描述:

/**
     * Public setter for the strict flag. If true (the default) then number of 
     * tokens in line must match the number of tokens defined 
     * (by {@link Range}, columns, etc.) in {@link LineTokenizer}. 
     * If false then lines with less tokens will be tolerated and padded with 
     * empty columns, and lines with more tokens will 
     * simply be truncated.
     * 
     * @param strict the strict flag to set
     */
    public void setStrict(boolean strict) {
        this.strict = strict;
    }

遗憾的是,在生成器中没有很好的方法来设置严格字段,否则我们可以按以下方式设置:

@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
public FlatFileItemReader<FixingDTO> fileReader(@Value("#{jobExecutionContext['type']}") String type) {
    SomeType type = forName(type);
    return new FlatFileItemReaderBuilder<MyDTO>()
                .name("fileReader")
                .fieldSetMapper(myDTOMapper())
                .lineTokenizer(new DelimitedLineTokenizer() {
                    {
                        setDelimiter(",");
                        setNames("id", "source", "startDate", "endDate");
                        setStrict(false);
                    }
                })
                .build();
    }

相关问题