spring 使用 mapFieldSet 读取只有一列的 txt 文件时,批处理作业抛出异常
spring batch job throws exception when using mapFieldSet for reading a txt file with just one column
我有一个只有一列的 txt 文件:ids 和 ids 用换行符分隔。
我想用 reader 读取此文件,但我认为我不应该使用 DelimitedLineTokenizer,因为我的文件没有多列。这是代码:
<bean id="idsReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<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="ids" />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="IdsMapper" />
</property>
</bean>
</property>
<property name="resource" value="#{stepExecutionContext['fileResource']}" />
<property name="encoding" value="UTF-8" />
</bean>
public class IdsMapper implements FieldSetMapper<String> {
@Override
public String mapFieldSet(FieldSet fs) throws BindException {
if (fs == null) {
return null;
}
return fs.readString("ids");
}
这是我收到的异常:
[jobTaskExecutor-15] ERROR Encountered an error executing step loadIds
Message: IdsMapper.mapFieldSet(Lorg/springframework/batch/item/file/transform/FieldSet;)Ljava/lang/String;
Line | Method
->> -1 | mapFieldSet in batch.model.IdsMapper
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 7 | mapFieldSet in batch.model.IdsMapper$$ET3XDpZ2
| -1 | mapFieldSet . . . . . . . . . in batch.model.IdsMapper$$DT3XDpZ2
| 43 | mapLine in org.springframework.batch.item.file.mapping.DefaultLineMapper
| 180 | doRead . . . . . . . . . . . . in org.springframework.batch.item.file.FlatFileItemReader
| 88 | read in org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
您可以使用自己的 linemapper 实现:
<bean id="idsReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="lineMapper">
<bean class="com.example.TextFileLineMapper"/>
</property>
<property name="resource" value="file:#{stepExecutionContext['resource']}"/>
<property name="encoding" value="UTF-8" />
</bean>
public class TextFileLineMapper implements LineMapper<TextFileDto> {
@Override
public TextFileDto mapLine(String line, int lineNumber) {
TextFileDto textFileDto = new TextFileDto();
textFileDto.setId(line);
return textFileDto;
}
}
public class TextFileDto {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我有一个只有一列的 txt 文件:ids 和 ids 用换行符分隔。 我想用 reader 读取此文件,但我认为我不应该使用 DelimitedLineTokenizer,因为我的文件没有多列。这是代码:
<bean id="idsReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<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="ids" />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="IdsMapper" />
</property>
</bean>
</property>
<property name="resource" value="#{stepExecutionContext['fileResource']}" />
<property name="encoding" value="UTF-8" />
</bean>
public class IdsMapper implements FieldSetMapper<String> {
@Override
public String mapFieldSet(FieldSet fs) throws BindException {
if (fs == null) {
return null;
}
return fs.readString("ids");
}
这是我收到的异常:
[jobTaskExecutor-15] ERROR Encountered an error executing step loadIds
Message: IdsMapper.mapFieldSet(Lorg/springframework/batch/item/file/transform/FieldSet;)Ljava/lang/String;
Line | Method
->> -1 | mapFieldSet in batch.model.IdsMapper
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 7 | mapFieldSet in batch.model.IdsMapper$$ET3XDpZ2
| -1 | mapFieldSet . . . . . . . . . in batch.model.IdsMapper$$DT3XDpZ2
| 43 | mapLine in org.springframework.batch.item.file.mapping.DefaultLineMapper
| 180 | doRead . . . . . . . . . . . . in org.springframework.batch.item.file.FlatFileItemReader
| 88 | read in org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
您可以使用自己的 linemapper 实现:
<bean id="idsReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="lineMapper">
<bean class="com.example.TextFileLineMapper"/>
</property>
<property name="resource" value="file:#{stepExecutionContext['resource']}"/>
<property name="encoding" value="UTF-8" />
</bean>
public class TextFileLineMapper implements LineMapper<TextFileDto> {
@Override
public TextFileDto mapLine(String line, int lineNumber) {
TextFileDto textFileDto = new TextFileDto();
textFileDto.setId(line);
return textFileDto;
}
}
public class TextFileDto {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}