簡體   English   中英

Spring 批處理 FlatFileItemReader 令牌錯誤

[英]Spring batch FlatFileItemReader token error

我的文件系統中有兩個 csv 文件,我需要根據某些請求類型使用 spring 批處理進行處理。

請求類型的文件:EOD(無標題)

12345,BBG
23232,BBG

請求類型的另一個文件:ANCIENT(無標題)

12345,BBG,20201115
23232,BBG,20201115

這兩個文件都有強制性的前兩個字段, idsource ANCIENT 文件可以選擇具有第 3 和第 4 字段startDateendDate

如何創建適用於這兩個文件的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();
}

字段映射器:

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

我想簡單地處理行中的任何內容並分配給 object 變量。 StartDate 和 endDate 可以是 null。

DelimitedLineTokenizer內部有字段strict (默認值:true),該屬性正是您要查找的屬性。 您應該將該字段設置為 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;
    }

不幸的是,在builder中設置嚴格字段不是很好的方法,否則我們可以通過以下方式設置:

@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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM