gpt4 book ai didi

java - Spring Batch ItemReader 在第一行获取 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 09:42:59 24 4
gpt4 key购买 nike

当我尝试读取 csv 文件时,Spring 批处理遇到了这个问题。

我必须读取具有如下结构的CSV:

valueHeader;valueHeader;valueHeader;valueHeader;valueHeader;valueHeader
conventValue;conventValue;conventValue;conventValue;conventValue
conventValue;conventValue;conventValue;conventValue;conventValue
conventValue;conventValue;conventValue;conventValue;conventValue
valueFooter;valueFooter;valueFooter;valueFooter;valueFooter;valueFooter

我有一个 MultiResourceItemReader ,它委托(delegate) String 类型的 FlatFileItemReader ,以便轻松地将这些行映射到之后执行的自定义项处理器中的自定义 DTO。

在我的 Tasklet 中,我像这样注入(inject)了 MultiResourceItemReader bean:

@Autowired
private MultiResourceItemReader<String> resourceItemReader;

然后我这样调用它(设置资源和上下文):

    resourceItemReader.setStrict(Boolean.FALSE);
resourceItemReader.setResources(resources);
resourceItemReader.open(context.getStepContext().getStepExecution().getExecutionContext());

String currentContent = resourceItemReader.read();
while(currentContent != null) {
linesReaded.add(currentContent);
currentContent = resourceItemReader.read();
}

But I am getting a **NullPointerException** in the first Line and I don't know why:

20190705 19:53:30.041 INFO com.mycomp.mycustomp.tasklet.MyCustomTasklet [[ # ]] - [MyCustomTasklet:EXECUTE]
20190705 19:53:41.560 ERROR org.springframework.batch.core.step.AbstractStep [[ # ]] - Encountered an error executing step processRebateASLIStep in job MyProyectJob
org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[file [C:\Pruebas\MyProyectJob\destino\MyFile-100-20151130143000-000001.csv]], input=[HEADER;PEP;FacteTRY;100; 20151130143000;000125;100000000000000000000000000000000025874]
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:183)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
at org.springframework.batch.item.file.MultiResourceItemReader.readFromDelegate(MultiResourceItemReader.java:140)
at org.springframework.batch.item.file.MultiResourceItemReader.readNextItem(MultiResourceItemReader.java:119)
at org.springframework.batch.item.file.MultiResourceItemReader.read(MultiResourceItemReader.java:108)
....
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.NullPointerException: null
at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43)
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180)
... 59 common frames omitted

我也会附加我的 readerItems beans:

@Bean
MultiResourceItemReader<String> resourceItemReader(FlatFileItemReader<String> flatFileItemReader){
MultiResourceItemReader<String> resourceItemReader = new MultiResourceItemReader<>();
resourceItemReader.setDelegate(flatFileItemReader);
resourceItemReader.setStrict(false);
return resourceItemReader;
}

@Bean
FlatFileItemReader<String> flatFileItemReader() {

FlatFileItemReader<String> reader = new FlatFileItemReader<String>();
reader.setStrict(false);
reader.setLineMapper(new DefaultLineMapper<String>() {
{
//Setea los valores en la clase indicada
setFieldSetMapper(new FieldSetMapper<String>() {

@Override
public String mapFieldSet(FieldSet fieldSet) throws BindException {
String[] values = fieldSet.getValues();
return String.join(";", values);
}

});

}
});

return reader;
}

最佳答案

您没有在 LineMapper 上设置线路标记器。添加方法如下:

    reader.setLineMapper(new DefaultLineMapper<String>() {
{
//Setea los valores en la clase indicada
setFieldSetMapper(new FieldSetMapper<String>() {

@Override
public String mapFieldSet(FieldSet fieldSet) throws BindException {
String[] values = fieldSet.getValues();
return String.join(";", values);
}

});

setLineTokenizer(new DelimitedLineTokenizer(";"));

}
});

我建议使用 FlatFileItemReaderBuilder,由于其流畅的 API,它更加直观。这是一个例子:

@Bean
public FlatFileItemReader<Pojo> flatFileItemReader() {
return new FlatFileItemReaderBuilder<Pojo>()
.name("itemReader")
.resource(new FileSystemResource("/path/to/flat/file"))
.targetType(Pojo.class)
.delimited()
.delimiter(";")
.names("column1", "column2")
.build();
}

关于java - Spring Batch ItemReader 在第一行获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56907588/

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