英文:
Spring Batch empty file using FlatFileItemReader xML
问题
我正在尝试验证输入文件是否为空或是否包含数据。
我正在通过XML beans配置使用Spring Batch,我的实际配置如下:
<!-- 读取器 -->
<bean id="mgbfItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="strict" value="false" />
<!-- 读取文件 -->
<property name="resource" value="file:///fichtemcomp/datent/#{processDataClass.fileName}" />
<property name="linesToSkip" value="10" />
<property name="encoding" value="UTF-8" />
<!-- 映射器 -->
<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="delimiter" value="|" />
<!-- 布局值 -->
<property name="names" value="name,secondName,lastName" />
</bean>
</property>
<!-- 包装器 -->
<property name="fieldSetMapper">
<bean class="com.bbva.mgbf.batch.listeners.ScholarItemReader">
</bean>
</property>
</bean>
</property>
</bean>
<!-- 结束读取器 -->
在我的Scholar ItemReader类中,我有类似以下的内容:
@Override
public Map<String, Object> mapFieldSet(FieldSet fieldSet) throws BindException {
LOGGER.info("Reading all items \t {}", fieldSet);
LOGGER.info("FieldCount \t {}", fieldSet.getFieldCount());
Map<String, Object> dataIn = new HashMap<>(65);
if (fieldSet.getFieldCount() > 0 && !fieldSet.readString(0).trim().isEmpty()) {
LOGGER.info("in if ");
for (int i = 0; i < 65; i++) {
String key = "" + i;
String value = fieldSet.readString(i);
dataIn.put(key, value);
}
} else {
dataIn.put("empty", "emptyFile");
}
return dataIn;
}
但它从未进入“else”声明,它只是结束了进程。
有人知道是否有另一种验证文件是否为空的方法吗?
英文:
I'm trying to validate if an input file is empty or has data.
I'm working with spring batch thru XML beans config, my actual config is like:
<!-- READERS -->
<bean id="mgbfItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="strict" value="false" />
<!-- Read a file -->
<property name="resource" value="file:///fichtemcomp/datent/#{processDataClass.fileName}" />
<property name="linesToSkip" value="10" />
<property name="encoding" value="UTF-8" />
<!-- Mapper -->
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<!-- split it -->
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="delimiter" value="|" />
<!-- Valores del layout -->
<property name="names" value="name,secondName,lastName" />
</bean>
</property>
<!-- Wrapper -->
<property name="fieldSetMapper">
<bean class="com.bbva.mgbf.batch.listeners.ScholarItemReader">
</bean>
</property>
</bean>
</property>
</bean>
<!-- END READERS -->
In my class Scholar ItemReader, I have something like:
@Override
public Map<String, Object> mapFieldSet(FieldSet fieldSet) throws BindException {
LOGGER.info("Reading all items \t {}", fieldSet);
LOGGER.info("FieldCount \t {}", fieldSet.getFieldCount());
Map<String, Object> dataIn = new HashMap<>(65);
if (fieldSet.getFieldCount() > 0 && !fieldSet.readString(0).trim().isEmpty()) {
LOGGER.info("in if ");
for (int i = 0; i < 65; i++) {
String key = "" + i;
String value = fieldSet.readString(i);
dataIn.put(key, value);
}
}else{
dataIn.put("empty", "emptyFile");
}
return dataIn;
}
But it never entered in the "else " declaration, it just ends the process.
Anyone knows if there are another way to validate if file is empty or not?
答案1
得分: 2
fieldSetMapper
在读取器已经读取完一行后被调用,因此在映射器中尝试检查文件是否为空来得太迟了。
我正在尝试验证输入文件是否为空或包含数据。
我会使用预验证的任务步骤来检查文件是否为空。
英文:
fieldSetMapper
is called when the reader has already read a line, so trying to check if the file is empty in the mapper is too late in the process.
> I'm trying to validate if an input file is empty or has data
I would use a pre-validation tasklet step that checks if the file is empty or not.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论