“Spring Batch使用FlatFileItemReader解析空文件xML”

huangapple go评论125阅读模式
英文:

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:

&lt;!-- READERS --&gt;
	&lt;bean id=&quot;mgbfItemReader&quot; class=&quot;org.springframework.batch.item.file.FlatFileItemReader&quot;&gt;
		&lt;property name=&quot;strict&quot; value=&quot;false&quot; /&gt;
		&lt;!-- Read a file --&gt;
		&lt;property name=&quot;resource&quot; value=&quot;file:///fichtemcomp/datent/#{processDataClass.fileName}&quot; /&gt;
		&lt;property name=&quot;linesToSkip&quot; value=&quot;10&quot; /&gt;
		&lt;property name=&quot;encoding&quot; value=&quot;UTF-8&quot; /&gt;

		&lt;!-- Mapper --&gt;
		&lt;property name=&quot;lineMapper&quot;&gt;
			&lt;bean class=&quot;org.springframework.batch.item.file.mapping.DefaultLineMapper&quot;&gt;
				&lt;!-- split it --&gt;
				&lt;property name=&quot;lineTokenizer&quot;&gt;
					&lt;bean
						class=&quot;org.springframework.batch.item.file.transform.DelimitedLineTokenizer&quot;&gt;
						&lt;property name=&quot;delimiter&quot; value=&quot;|&quot; /&gt;
						&lt;!-- Valores del layout --&gt;
						&lt;property name=&quot;names&quot; value=&quot;name,secondName,lastName&quot; /&gt;
					&lt;/bean&gt;
				&lt;/property&gt;

				&lt;!-- Wrapper --&gt;
				&lt;property name=&quot;fieldSetMapper&quot;&gt;
					&lt;bean class=&quot;com.bbva.mgbf.batch.listeners.ScholarItemReader&quot;&gt;
					&lt;/bean&gt;
				&lt;/property&gt;
			&lt;/bean&gt;
		&lt;/property&gt;
	&lt;/bean&gt;
	&lt;!-- END READERS --&gt;

In my class Scholar ItemReader, I have something like:

@Override
	public Map&lt;String, Object&gt; mapFieldSet(FieldSet fieldSet) throws BindException {
		LOGGER.info(&quot;Reading all items \t {}&quot;, fieldSet);
		LOGGER.info(&quot;FieldCount \t {}&quot;, fieldSet.getFieldCount());

		Map&lt;String, Object&gt; dataIn = new HashMap&lt;&gt;(65);
		if (fieldSet.getFieldCount() &gt; 0 &amp;&amp; !fieldSet.readString(0).trim().isEmpty()) {
			LOGGER.info(&quot;in if &quot;);

			for (int i = 0; i &lt; 65; i++) {
				String key = &quot;&quot; + i;
				String value = fieldSet.readString(i);
				dataIn.put(key, value);
			}
		}else{
			dataIn.put(&quot;empty&quot;, &quot;emptyFile&quot;);
		}
		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.

huangapple
  • 本文由 发表于 2020年4月11日 02:02:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61146025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定